Hello!
First of all I hope it's fine that this is related to an ESP32 project and not an Arduino board. I am really new to all of this so I don't really know if this is a competitor thing or not.
Quick introduction, I'm in an school based IT apprenticeship and we currently are working with an ESP32 in electronics.
While fiddling around at home I've set my goal on creating a touch based "piano" basically, using a passive buzzer and some thumbtacks as touch based inputs.
I have been able to get it to work but ran into some - for me at least - strange issues that I couldn't find any help for while googling or searching these forums specifically.
Therefor I'm hoping someone could help me out around here - but now to the issue!
So, I'm attaching a picture of my setup:
And here is my code:
#include "pitches.h"
void setup() {
//pinMode(16, OUTPUT);
Serial.begin(115200);
}
void loop() {
Serial.println(touchRead(4));
while (touchRead(15) > 40 && touchRead(2) > 40 && touchRead(4) > 50 && touchRead(12) > 50 && touchRead(14) > 20 && touchRead(27) > 40 && touchRead(33) > 50 && touchRead(32) > 50) {
noTone(16);
}
playTone(15,NOTE_C4);
playTone(2,NOTE_D4);
playTone(4,NOTE_E4);
playTone(12,NOTE_F4);
playTone(14,NOTE_G4);
playTone(27,NOTE_A4);
playTone(33,NOTE_B4);
playTone(32,NOTE_C5);
}
void playTone(int read, int note){
if (touchRead(read) < 10) {
tone(16, note);
}
}
(notes are defined in another file which I don't need to post reall, just to clarify)
While learning all kinds of stuff about how a buzzer works, it being a passive buzzer and how to utilize tone(), I've read up on how to get some touch input working and worked my way towards some thresholds that I am utilizing in my code.
Now, this code does work - When i start it up I can play all of the defined touch buttons - but - the buzzer sounds.. How would I phrase this.. cranky? Like it's slightly vibrating? It definitely does not sound like it's supposed to.
I found out through some trial and error that apparently the Serial.println() is the perpetrator here, since it has a builtin wait time or similar - so I removed it and then the whole code simply does not remotely work anymore. Sounds only are played basically for the fraction of a second when letting go of the touch button, and not at all while touching it. But the sounds play correctly and not "cranky".
Since I don't know why this would be the case and I can't see if it might "just" be that the threshold for the touch input has changed after removing the Serial output, I've tried around but to no luck in the end. I just cannot get it to work without using the Serial.println in there.
Since I'm a complete beginner at this, this might be a very easy question, or I might very well be just doing things the wrong way.. So I'm looking for some guidance on if this issue is solvable even, or what I could do to achieve what I'm looking for.
Any help is really appreciated!
