i have arduino lcd , 1 spdt switch connected board. the common pin of spdt grounded , outer pins each connected digital input. program should increment , decrement counter being printed lcd screen. have 1 input working increments counter not know how implement code input decrement counter. code posted below.
thank you
#include <liquidcrystal.h> liquidcrystal lcd(12, 11, 5,4,3,2); const byte buttonpin = 8; int counter = 0; // set counter 0 begin byte buttonstate; // current reading input pin byte lastbuttonstate = high; // previous reading input pin unsigned long lastdebouncetime = 0; unsigned long debouncedelay = 50; void setup() { serial.begin(9600); pinmode(buttonpin, input_pullup); lcd.begin(16, 2); // 2x16 lcd display } void loop() { // read state of switch local variable: byte reading = digitalread(buttonpin); // check see if pressed button // (i.e. input went high low), , you've waited // long enough since last press ignore noise: // if switch changed, due noise or pressing: if (reading != lastbuttonstate) { // reset debouncing timer lastdebouncetime = millis(); } if ((millis() - lastdebouncetime) >= debouncedelay) { // whatever reading at, it's been there longer // debounce delay, take actual current state: // if button state has changed: if (reading != buttonstate) { buttonstate = reading; if (buttonstate == low) { counter ++; serial.println(counter); lcd.setcursor(0, 1); lcd.print(counter); } } } lastbuttonstate = reading; }
you cannot connect 1 pole of switch input pin other ground. detect low when when suppose detect high on pin, floating. connect pull-up resistor input pins.
or can use pinmode(inpin, input_pullup);
this pull input pin high internally , can detect swithces , implememnt code.
Comments
Post a Comment