Keyboard with piezo

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);
}
}

No picture attached. The code you posted isn't the code you have running. It doesn't even compile.

I don't have your beginner's book but that code isn't right. I think you've mangled up parts of more than one example.

Among other problems you declare 4 buttons and variables buttonState and lastbuttonState but they are then never used. You have buttonPin declared as both pins 9 and 4. You try to set pinMode for ledPin which doesn't exist. You read from pin A0 with no indication of what is connected to it. Your if statements don't cover the full range of values from the analogRead().

Try again with the correct code and put it in code tags </> as described in "How to use this forum - please read"...which I guess you didn't read.

Steve

I have created a piezo keyboard, it works with a fault.
Whenever I press a button I get a different note but, the Piezo is constantly creating a note regardless of what switch I press.
This then also affects the already producing note.

The code compiles are there are no errors but that doesn't solve the logic loop.
I am trying to restrict when the Piezo produces the note.

Attached is an image of my breadboard and Arduino Uno and the code is attached.

int switchState = 0;
int notes[] = {262, 294, 330, 349};

void setup() {
  pinMode(9, INPUT);
  pinMode(10, INPUT);
  pinMode(11, INPUT);
  pinMode(12, INPUT);
  Serial.begin(9600);
  // Being Serial Communication:

}

void loop() {

  switchState = digitalRead(9);
  switchState = digitalRead(10);
  switchState = digitalRead(11);
  switchState = digitalRead(12);
  int keyVal = analogRead(A0);
  Serial.println(keyVal);
  // Read the analog value, sends to monitor:
  if(switchState == LOW);{
    noTone(8);}
  if(switchState == HIGH); // If push button attached to pin 12 is pressed, make noise.:
        (keyVal >= 0 && keyVal <= 1);{
          digitalWrite(12, HIGH);
          digitalWrite(11, LOW);
          digitalWrite(10, LOW);
          digitalWrite(9, LOW);
          tone(8, notes[0]);
  }
  if(switchState == HIGH); // If push button attached to pin 11 is pressed, make noise.:
            (keyVal >= 977 && keyVal <= 1022);{
              digitalWrite(12, LOW);
              digitalWrite(11, HIGH);
              digitalWrite(10, LOW);
              digitalWrite(9, LOW);
              tone(8, notes[1]);
  }
  if(switchState == HIGH); // If push button attached to Pin 10 is pressed, make noise:
            (keyVal >= 509 && keyVal <= 915);{
              digitalWrite(12, LOW);
              digitalWrite(11, LOW);
              digitalWrite(10, HIGH);
              digitalWrite(9, LOW);
              tone(8, notes[2]);
  }
  if(switchState == HIGH); // If push button attached to pin 9 is pressed, make noise:
            (keyVal >= 10 && keyVal <= 508);{
              digitalWrite(12, LOW);
              digitalWrite(11, LOW);
              digitalWrite(10, LOW);
              digitalWrite(9, HIGH);
              tone(8, notes[3]);
 }
}

47191416_262673547732080_1833760725391114240_n.jpg

Why didn't you add this in your original post instead of starting a new one?

That tiny picture is useless. A circuit diagram showing all components and connections would be more useful.

Why are you reading from 4 different pins into a single variable switchState? That will just tell you the state of the last switch that you read!

And why do you have 4 input pins which you keep writing to? And no output pin so where is the buzzer connected?

What is connected to A0?

Steve

I was unsure of what to do, so I created a new post.

A new picture has been uploaded.

A new switch state for each pin?
The book that I have used from the Arduino Starter Kit if a single variable is doing the same thing, as long as it is identified that it's from a different port and that port has been specified just one variable would be ok.
That is why I have only used a single switchState variable.

Maybe that is where the problem is coming from, the output should be the A0, which is connected to the ground of the last push button.

Following the Arduino start book exactly, with the slight deviation to make the Piezo create noise when a single push button is pressed.

Also, the second image is a photograph from the Arduino starter kit book.

And that picture shows completely different wiring from what you have. The buzzer is on pin 8. There are not 4 wires from the buttons to digital pins.

Basically I have no idea why you have messed with the design in that way. Good luck getting that to work.

Steve