Circuit for using 3 Push buttons controls
Source Code for controlling the push Buttons
const int ledPin=13;
const int ledPin2=12;
const int ledPin3=8;
const int buttonPin=2;
const int buttonPin2=3;
const int buttonPin3=7;
boolean lastButton=LOW;
boolean currentButton=LOW;
boolean ledOn=false;
boolean lastButton2=LOW;
boolean currentButton2=LOW;
boolean ledOn2=false;
boolean lastButton3=LOW;
boolean currentButton3=LOW;
boolean ledOn3=false;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin2, INPUT);
pinMode(ledPin3, OUTPUT);
pinMode(buttonPin3, INPUT);
Serial.begin(9600);
}
boolean debounce(boolean last,int bp)
{
boolean current=digitalRead(bp);
if (last!=current)
{
current=digitalRead(bp);
}
return current;
}
void loop()
{
currentButton=debounce(lastButton,buttonPin);
if (lastButton==LOW && currentButton==HIGH)
{
ledOn=!ledOn;
}
lastButton=currentButton;
digitalWrite(ledPin, ledOn);
currentButton2=debounce(lastButton2,buttonPin2);
if (lastButton2==LOW && currentButton2==HIGH)
{
ledOn2=!ledOn2;
}
lastButton2=currentButton2;
digitalWrite(ledPin2, ledOn2);
currentButton3=debounce(lastButton3,buttonPin3);
if (lastButton3==LOW && currentButton3==HIGH)
{
ledOn3=!ledOn3;
}
lastButton3=currentButton3;
digitalWrite(ledPin3, ledOn3);
}
No comments:
Post a Comment