Arduino on/off button

On/Off Button Press

I began this project by googling “debouncing” and find instruction on how to use, as well as it’s purpose.  I then plugged the components into my current bread board set-up and began playing around.

Code:

// int buttonState = 0;
int inputPin = 2;
int outputPin = 13;

int state = HIGH;
int reading;
int previous = LOW;

long time = 0;
long debounce = 200;

void setup(){
pinMode(inputPin, INPUT);
pinMode(outputPin, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
reading = digitalRead(inputPin);
if (reading == HIGH && previous == LOW && millis() – time > debounce) {
if(state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}

digitalWrite(outputPin, state);
previous = reading;
}

Leave a reply:

Your email address will not be published.

Site Footer