Hello, I am following one of the tutorials in the Arduino beginners book: Keyboard Instrument.
I have followed the tutorial and it is successful to a fault.
One of the lines of codes is this.
if(keyVal >= 0 && keyVal <= 1{
tone(8, notes[1])}
The problem arises here.
The piezo constantly creates a note but I want to make it so that it'll only play a note once pressed.
Here is the full code with a picture of my board attached.
I can't think of a way that'd work with it.
I am still new to all of this.
Thank you.
const int buttonPin = 9;
const int buttonPin1 = 10;
const int buttonPin2 = 11;
const int buttonPin3 = 12;
// Assigning buttons pins:
int notes[] = {262, 294, 330, 349};
// Creates an array of frequencies to make notes:
//Chaning variables:
int ledState = HIGH;
int buttonState;
int lastButtonState = LOW;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
const int buttonPin = 4;
Serial.begin(9600);
// Being Serial Communication:
}
void loop() {
int keyVal = analogRead(A0);
Serial.println(keyVal);
// Read the analog value, sends to monitor:
if(keyVal >= 0 && keyVal <= 1){
tone(8, notes[0]);
}
else if(keyVal >= 977 && keyVal <= 1022){
tone(8, notes[1]);
}
else if(keyVal >= 509 && keyVal <= 915){
tone(8, notes[2]);
}
else if(keyVal >= 10 && keyVal <= 508){
tone(8, notes[3]);
}
else{
noTone(8);
}
}


