Can you help me with my project? Simple sound circuit does not work as published

I am a beginner with Arduino. I did the project attached, very simple keyboard instrument project. However, it does not work as supposed to. I want to know why if anyone can tell me.

The code is this:
int notes = {262,294,330,349};

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
int keyVal = analogRead(A0);
Serial.println(keyVal);

if(keyVal == 1023) {
tone(8, notes[0]);
} else if (keyVal >= 990 && keyVal <= 1010) {
tone(8, notes[1]);
} else if (keyVal >= 505 && keyVal <= 515) {
tone(8, notes[2]);
} else if(keyVal >= 5 && keyVal <=10) {
tone(8,notes[3]);
} else {
noTone(8);
}
}

The instructions say every switch will play a sound. The actual experience is that only the switch not connected to a resistance works fine. The switches connected to 10 Kohm and 220 ohm do not play any sound and the switch connected to 1 Mohm plays an unclear tone. I wonder if the resistances are of too high. Any hint may help me.

Thank you,

Miguel

Image from Original Post so we don’t have to download it. See this Image Guide

34b9e9d988e45dcb52166b911318a9f2aebee818.jpg

…R

What value is printed to the Serial Monitor for each key?

Maybe the values in your IF statements are not correct.

In any case I suggest simplifying the IF statements like this

  if(keyVal > 1010) {
    tone(8, notes[0]);
  } else if (keyVal > 990) {
      tone(8, notes[1]);
  } else if (keyVal > 505) {
        tone(8, notes[2]);
  } else if(keyVal > 5) {
          tone(8,notes[3]);
  } else {
            noTone(8);
  }

…R

Now, let's see a schematic of HIS connections! Paul

Hi,
Poorly drawn schematic, make sure you have the junction of the resistors as I have highlighted.
As originally drawn, that would be a cross wire, not a junction.
2016E.jpg

Tom… :slight_smile:

Thank you all. I updated the code as Robin2 suggested, but the results are the same.

The keyVal values printed are:
for switch with no resistance keyVal = 1023
for switches connected to 10 Kohm and 220 ohm keyVal = 0 and
for switch connected to 1 Mohm keyVal = among {7,8,10,11,14,6,…}.

Based on those values I suspect that something is wrong with the value reading from A0.

As Paul questioned, I connected the switches in a junction. Picture of my circuit is attached below.

While it isn't necessary to declare pinMode for inputs, is there a reason pin 8 hasn't been declared as an OUTPUT in setup?

Hi, With no key pressed you should read 0.

Do you have a DMM? Can you post a picture of your project please. Did you check the two connections I highlighted in my previous post?

Thanks.. Tom.. :) Sorry your picture has not appeared. It must be less than 1M in size.

macisonline:
The keyVal values printed are:
for switch with no resistance keyVal = 1023
for switches connected to 10 Kohm and 220 ohm keyVal = 0 and
for switch connected to 1 Mohm keyVal = among {7,8,10,11,14,6,…}.

Based on those values I suspect that something is wrong with the value reading from A0.

You should treat the value for the 1Meg resistor as < 20 (say). The slight variation is normal.

You must have poor connections to the 10k amd 220ohm resitors. Actually the 220 ohm may be working properly but is just a poor choice of value - too close to zero - I suggest you use 1k or 2k in place of 220ohms.

When each button give different values you need to adjust the test values in your IF statements to match the values. Make sure to allow a margin for natural variation in values.

…R

We had this one a few weeks ago. It is a crap schematic both in the way it is drawn and the component values.