Learning ARDUINO for Beginners EP#8 while loop and pull up resistors

In this tutorial we will make a Arduino piano to demonstrate how the while loop works..

 

ARDUINO Code:

 

#define button1 2 // button pins
#define button2 3
#define button3 4
#define button4 5
#define button5 6
#define button6 7
#define button7 8
#define button8 9
#define button9 10
// save pin 11 for speaker
#define button10 12
int v1=HIGH; // button state value

void setup() {
pinMode(button1,INPUT_PULLUP);
pinMode(button2,INPUT_PULLUP);
pinMode(button3,INPUT_PULLUP);
pinMode(button4,INPUT_PULLUP);
pinMode(button5,INPUT_PULLUP);
pinMode(button6,INPUT_PULLUP);
pinMode(button7,INPUT_PULLUP);
pinMode(button8,INPUT_PULLUP);
pinMode(button9,INPUT_PULLUP);
pinMode(button10,INPUT_PULLUP);

}

void loop() {

v1=digitalRead(button1);
while(v1==LOW)
{
tone(11,1000);
v1=digitalRead(button1);
}

v1=digitalRead(button2);
while(v1==LOW)
{
tone(11,900);
v1=digitalRead(button2);
}

v1=digitalRead(button3);
while(v1==LOW)
{
tone(11,800);
v1=digitalRead(button3);
}

v1=digitalRead(button4);
while(v1==LOW)
{
tone(11,700);
v1=digitalRead(button4);
}

v1=digitalRead(button5);
while(v1==LOW)
{
tone(11,600);
v1=digitalRead(button5);
}

v1=digitalRead(button6);
while(v1==LOW)
{
tone(11,500);
v1=digitalRead(button6);
}

v1=digitalRead(button7);
while(v1==LOW)
{
tone(11,400);
v1=digitalRead(button7);
}

v1=digitalRead(button8);
while(v1==LOW)
{
tone(11,300);
v1=digitalRead(button8);
}

v1=digitalRead(button9);
while(v1==LOW)
{
tone(11,200);
v1=digitalRead(button9);
}

v1=digitalRead(button10);
while(v1==LOW)
{
tone(11,100);
v1=digitalRead(button10);
}

noTone(11); //stop the tone from playing
}

Advertisements