Tuesday, 29 March 2016

Sensing a button press




An explanation of a method to use a button. The skech uses pins 7 and 11. Pin 7 is used to sense the input voltage created when the button is either pressed or released. Pin 11 is used to write to the LED.

The arduino sketch is as follows...

int ledpin=11;//define the LED connect to Pin11;
int inpin=7;//define the button connect to Pin7
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
}

An explanation of the code and a schematic diagram are available.
Further reading about buttons can be found on the Arduino Site Button Page



No comments:

Post a Comment

Note: only a member of this blog may post a comment.