How to control Button with LED in Arduino,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
int Ledpin=11; //define the LED connect to Pin 11; int Inpin=7; //define the button connect to Pin 7 int val; //val to store the data void setup() { pinMode(ledpin,OUTPUT); //define the LED Pin to OUTPUT; pinMode(inpin,INPUT); //define the button Pin to INPUT; } void loop() { val=digitalRead(inpin); //digital read the button pin if(val==LOW) //if the button pressed; { digitalWrite(ledpin,LOW);} //turn off the LED else { digitalWrite(ledpin,HIGH);} //if not pressed, turn on the LED } |
Download the source code here > Button_Control
Starter #2 Control a Push Button in Arduino