Three button arduino piano

  • Thread starter Deleted member 750607
  • Start date

Thread Starter

Deleted member 750607

Joined Dec 31, 1969
0
so my wiring is all correct, but my code is off somehow, and im wondering if y'all know why

I got example code from the internet, and it worked with my wiring, so the wiring isn't the problem

the example code works for a little three button piano, declaring and using real notes instead of the frequency values I learned to use in the arduino "tone" function

but my own code, which I will post below, won't work properly

I would love to know why


MY (not working) version of the three note piano:
const int buzzPin = 11;
const int buttonOne = 10;
const int buttonTwo = 9;
const int buttonThree = 8;


void setup()
{
pinMode (buzzPin, OUTPUT);
pinMode (buttonOne, INPUT);
pinMode (buttonTwo, INPUT);
pinMode (buttonThree, INPUT);
noTone (11);
}



void loop() {

while (digitalRead (buttonOne == HIGH))
  {
  tone (11, 100);
  }

while (digitalRead (buttonOne == LOW))
   {
  noTone (11);
   }

while (digitalRead (buttonTwo == HIGH))
  {
   tone (11, 500);
  }
while (digitalRead (buttonTwo == LOW))
   {
   noTone (11);
   }
 
while (digitalRead (buttonThree == HIGH))
  {
   tone (11, 1000);
  }

while (digitalRead (buttonThree == LOW))
  {
  noTone (11);
  }

}
 

KeithWalker

Joined Jul 10, 2017
3,063
You did not state exactly what the problem is.
The "While" statement pauses the program until the condition is true. I'm not sure if that is what you want your program to do. If ButtonOne never goes high, the program will never go past line 20.
 

trebla

Joined Jun 29, 2019
542
If you want use while() statements then check only if buttons are pressed, what level it will be (LOW or HIGH) it depends how you wired your Arduino.
 

djsfantasi

Joined Apr 11, 2010
9,156
You did not state exactly what the problem is.
The "While" statement pauses the program until the condition is true. I'm not sure if that is what you want your program to do. If ButtonOne never goes high, the program will never go past line 20.
The While() statement does NOT pause the program until the condition is True.

It does check the condition and if it’s False, it passes execution to the statement after the code block it introduces.

Code:
While ( condition ) {
   // block of code
   }
// next statement
If the condition is true, it executes the block of code, and then tests the condition again. What happens is the same as before..
 

click_here

Joined Sep 22, 2020
548
Can I suggest to use 3 LEDs and 3 outputs to test whether you code flow and buttons are working properly.

Then test the tone generator seporately to make sure that there are no hardware issues there

I'd imagine that you would do something more like this pseudo code...
Code:
if button 1 is pressed
  play tone 1
  while button one still pressed
    do nothing
  turn off tone 1

Else if button 2 is pressed
  play tone 2
  while button one still pressed
    do nothing
  turn off tone 2

Else if button 3 is pressed
  play tone 3
  while button one still pressed
    do nothing
  turn off tone 3
 

click_here

Joined Sep 22, 2020
548
Looking back over your code, the flow is not going to work.

Program starts and it steps over the first while loop (L20), but step into the second (L25). It will loop there until button 1 is pressed, step over the next while loop (L30), and step into the next while loop (L34) and loop there until button 2 is pushed, and so on...
 
Top