Controlling LED with a Push Button using Arduino
Components Required:
Circuit Diagram:
Procedure:
Code:
int ledpin=11; //definition digital 11 pins as pin to control the LED
int btnpin=2; //Set the digital 2 to button interface
volatile int state = LOW; // Defined output status LED Interface
void setup()
{
pinMode(ledpin,OUTPUT); //Set digital 11 port mode, the OUTPUT for the output
pinMode(btnpin,INPUT); //Set digital 2 port mode, the INPUT for the input
}
void loop()
{
if(digitalRead(btnpin)==LOW) //Detection button interface to low
{
delay(10); //Delay 10ms for the elimination of key leading-edge jitter
if(digitalRead(btnpin)==LOW) //Confirm button is pressed
{
while(digitalRead(btnpin)==LOW); //Wait for key interfaces to high
delay(10); //delay 10ms for the elimination of key trailing-edge jitter
while(digitalRead(btnpin)==LOW); //Confirm button release
state = !state; //Negate operation, each time you run the program here, state
// the HIGH becomes LOW, or the state becomes the LOW HIGH.
digitalWrite(ledpin,state); //Output control status LED, ON or OFF
}
}
}
Comments
Post a Comment