esp32 יצרית מוסיקה DAC יצירת תדר
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <Arduino.h> const int DAC_PIN = 25; // DAC output pin (GPIO 25) const int SINE_FREQ = 1200; // Frequency of the sine wave in Hz const int SAMPLE_RATE = 10000; // Sample rate in Hz void setup() { // Nothing to set up } void loop() { float delta = 2 * PI * SINE_FREQ / SAMPLE_RATE; float angle = 0; while (true) { // Calculate the next sample value in the sine wave uint8_t value = 127 + 127 * sin(angle); // Write the sample to the DAC dacWrite(DAC_PIN, value); // Increment the angle for the next sample angle += delta; // Delay to control the output frequency delayMicroseconds(1000000 / SAMPLE_RATE); } } |
פאשרות מדוייקטת יותר
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/* ESP32 DAC - Simple Waveform Experiment espdac-wave.ino ESP32 DAC Waveform Demo Outputs a Sine Wave DroneBot Workshop 2022 https://dronebotworkshop.com */ // Define DAC pins #define DAC_CH1 25 #define DAC_CH2 26 void setup() { // Nothing here! } void loop() { // Generate a Sine Wave // Step one degree at a time for (int deg = 0; deg < 360; deg = deg + 1) { // Calculate sine and write to DAC dacWrite(DAC_CH1, int(128 + 64 * sin(deg * PI / 180))); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
/* ESP32 DAC - Waveform Library Experiment espdac-wave-library.ino ESP32 DAC Waveform Library Demo Outputs a Cosine Wave Uses DacESP32 Library - https://github.com/yellobyte/DacESP32 DroneBot Workshop 2022 https://dronebotworkshop.com */ // Include DacESP32 Library #include <DacESP32.h> // Create DAC object DacESP32 dac1(GPIO_NUM_25); void setup() { // Output a Cosine Wave with frequency of 1000Hz and max. amplitude (default) dac1.outputCW(1000); // Wait 5 seconds before changing amplitude delay(5000); } void loop() { // Change signal amplitude every second for (uint8_t i = 0; i < 4; i++) { delay(1000); if (i == 0) dac1.setCwScale(DAC_CW_SCALE_1); else if (i == 1) dac1.setCwScale(DAC_CW_SCALE_2); else if (i == 2) dac1.setCwScale(DAC_CW_SCALE_4); else if (i == 3) dac1.setCwScale(DAC_CW_SCALE_8); } } |