Created
August 10, 2025 10:37
-
-
Save EDISON-SCIENCE-CORNER/f64f1c3a6d34fb63165f642ade908cf5 to your computer and use it in GitHub Desktop.
Music with fruits and esp32
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| ESP32 DAC - Musical Fruit | |
| espdac-touch-music.ino | |
| ESP32 DAC & Touch Switch Demo | |
| Uses DacESP32 Library - https://github.com/yellobyte/DacESP32 | |
| DroneBot Workshop 2022 | |
| https://dronebotworkshop.com | |
| */ | |
| // Include DacESP32 Library | |
| #include <DacESP32.h> | |
| // Create DAC object for Channel 1 | |
| DacESP32 dac1(GPIO_NUM_25); | |
| // Define the touch pins (you can add more as desired) | |
| #define TOUCH_1 32 | |
| #define TOUCH_2 33 | |
| #define TOUCH_3 27 | |
| #define TOUCH_4 14 | |
| #define TOUCH_5 12 | |
| #define TOUCH_6 13 | |
| #define TOUCH_7 15 | |
| #define TOUCH_8 4 | |
| // Variables to hold the touch pin values | |
| int tvalue_1; | |
| int tvalue_2; | |
| int tvalue_3; | |
| int tvalue_4; | |
| int tvalue_5; | |
| int tvalue_6; | |
| int tvalue_7; | |
| int tvalue_8; | |
| // Define the threshold levels for each touch pin (adjust as required) | |
| const int threshold_1 = 200; | |
| const int threshold_2 = 200; | |
| const int threshold_3 = 200; | |
| const int threshold_4 = 200; | |
| const int threshold_5 = 200; | |
| const int threshold_6 = 200; | |
| const int threshold_7 = 200; | |
| const int threshold_8 = 200; | |
| // Define the frequencies for our "musical notes" - https://mixbutton.com/mixing-articles/music-note-to-frequency-chart/ | |
| const int freq_1 = 523; //C - Octave 5 | |
| const int freq_2 = 587; //D - Octave 5 | |
| const int freq_3 = 659; //E - Octave 5 | |
| const int freq_4 = 698; //F - Octave 5 | |
| const int freq_5 = 784; //G - Octave 5 | |
| const int freq_6 = 880; //A - Octave 5 | |
| const int freq_7 = 988; //B - Octave 5 | |
| const int freq_8 = 1046; //C - Octave 6 | |
| void setup() { | |
| // Setup serial monitor to check touch thresholds | |
| Serial.begin(115200); | |
| // Disable DAC to stop sound | |
| dac1.disable(); | |
| } | |
| void loop() { | |
| //Check status of touch switches | |
| tvalue_1 = touchRead(TOUCH_1); | |
| tvalue_2 = touchRead(TOUCH_2); | |
| tvalue_3 = touchRead(TOUCH_3); | |
| tvalue_4 = touchRead(TOUCH_4); | |
| tvalue_5 = touchRead(TOUCH_5); | |
| tvalue_6 = touchRead(TOUCH_6); | |
| tvalue_7 = touchRead(TOUCH_7); | |
| tvalue_8 = touchRead(TOUCH_8); | |
| // Print values (useful for adjusting threshold levels) | |
| Serial.print("S1 = "); | |
| Serial.print(tvalue_1); | |
| Serial.print(" S2 = "); | |
| Serial.print(tvalue_2); | |
| Serial.print(" S3 = "); | |
| Serial.print(tvalue_3); | |
| Serial.print(" S4 = "); | |
| Serial.print(tvalue_4); | |
| Serial.print(" S5 = "); | |
| Serial.print(tvalue_5); | |
| Serial.print(" S6 = "); | |
| Serial.print(tvalue_6); | |
| Serial.print(" S7 = "); | |
| Serial.print(tvalue_7); | |
| Serial.print(" S8 = "); | |
| Serial.println(tvalue_8); | |
| // If touch values exceed threshold then play associated note3 | |
| if (tvalue_1 < threshold_1) { | |
| dac1.enable(); | |
| dac1.outputCW(freq_1); | |
| } else if (tvalue_2 < threshold_2) { | |
| dac1.enable(); | |
| dac1.outputCW(freq_2); | |
| } else if (tvalue_3 < threshold_3) { | |
| dac1.enable(); | |
| dac1.outputCW(freq_3); | |
| } else if (tvalue_4 < threshold_4) { | |
| dac1.enable(); | |
| dac1.outputCW(freq_4); | |
| } else if (tvalue_5 < threshold_5) { | |
| dac1.enable(); | |
| dac1.outputCW(freq_5); | |
| } else if (tvalue_6 < threshold_6) { | |
| dac1.enable(); | |
| dac1.outputCW(freq_6); | |
| } else if (tvalue_7 < threshold_7) { | |
| dac1.enable(); | |
| dac1.outputCW(freq_7); | |
| } else if (tvalue_8 < threshold_8) { | |
| dac1.enable(); | |
| dac1.outputCW(freq_8); | |
| } else { | |
| // Disable DAC to stop sound | |
| dac1.disable(); | |
| } | |
| // Short delay (adjust as desired) | |
| delay(200); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment