SWITCH CASE
SWITCH CASE
https://github.com/anokhramesh/SWITCH-CASE-PRACTICAL
Arduino Sketch
int count = 0;// Variable name count default value is 0.
void setup()
{
Serial.begin(9600);// select serial monitor baud rate 9600
pinMode(2, INPUT_PULLUP);// connect push button to digital pin 2
pinMode(3, OUTPUT);// connect LED-1 to digital pin 3
pinMode(4, OUTPUT);// connect LED-2 to digital pin 4
pinMode(5, OUTPUT);// connect LED-3 to digital pin 5
pinMode(6, OUTPUT);// connect LED-4 to digital pin 6
pinMode(7, OUTPUT);// connect LED-5 to digital pin 7
pinMode(8, OUTPUT);// connect LED-6 to digital pin 8
}
void loop() {
Serial.print("Case ");// print the value of count on serial monitor
Serial.println(count);
int val = digitalRead(2);// store the value of push button to variable 'val'
if(val == 0)// if value of val is equal to 0,add 1 to the value of count.
{
count = count + 1;
}
switch(count)//assign the count to switch
{
case 1:
digitalWrite(3, HIGH);//Turn ON LED connected to Pin3
break;
case 2:
digitalWrite(3, LOW);//Turn OFF LED connected to Pin3
digitalWrite(4, HIGH);//Turn ON LED connected to Pin4
break;
case 3:
digitalWrite(4, LOW);//Turn OFF LED connected to Pin4
digitalWrite(5, HIGH);//Turn ON LED connected to Pin5
break;
case 4:
digitalWrite(5, LOW);//Turn OFF LED connected to Pin5
digitalWrite(6, HIGH);//Turn ON LED connected to Pin6
break;
case 5:
digitalWrite(6, LOW);//Turn OFF LED connected to Pin6
digitalWrite(7, HIGH);//Turn ON LED connected to Pin7
break;
case 6:
digitalWrite(7, LOW);//Turn OFF LED connected to Pin7
digitalWrite(8, HIGH);//Turn ON LED connected to Pin8
break;
default:
digitalWrite(3, LOW);//Turn OFF All LED
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
count = 0;
break;
}
delay(300);
}
Comments
Post a Comment