Hello everyone, I'm stuck in a programming rut. I am attempting to implement a counter using 2 pushbuttons (one UP button & one DOWN button).
This is exactly what I need it to do:
1 single press of either button will increment/decrement a counter by 1.
Holding either button for 2 or more seconds will increment/decrement the counter by 10 at a time.
Off the top of my head in like 3 minutes. Only covers up button, no warranty against typos. Something like this
byte lastb1st=0;
byte lastb2st=0;
unsigned long lastb1time=0;
unsigned long lastb2time=0;
//assumes the buttons are active low (ie, button pressing connects an INPUT_PULLUP pin to ground)
void loop(){
if (lastb1st!=digitalRead(btn1)) { //button changed state
lastb1st=digitalRead(btn1);
if (!lastb1st) {counter++;} //if button is not pressed
lastb1time=millis();
}
if (lastb1time && lastb1time < millis()-10000 && !lastb1st) {
counter+=10;
delay(500); //otherwise this will go very quickly
}
}
Hey so I’ve implemented the above code into mine and I am running into two problems.
The counter in the serial monitor is increasing by 10 at a time per individual button press.
Serial monitor stops working (not updating) after 5-6 seconds.
Here is my code:
byte lastb1st=0;
byte lastb2st=0;
unsigned long lastb1time=0;
unsigned long lastb2time=0;
int signalIn1 = 5; // set pin 5 as digital input pin
int signalIn2 = 6; // set pin 6 as digital input pin
int counter=0;
void setup() {
pinMode(signalIn1, INPUT_PULLUP); // initialize pin 5 as an input:
pinMode(signalIn2, INPUT_PULLUP); //initialize pin 6 as an input:
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
//assumes the buttons are active low (ie, button pressing connects an INPUT_PULLUP pin to ground)
void loop(){
if (lastb1st!=digitalRead(signalIn1)) { //button changed state
lastb1st=digitalRead(signalIn1);
if (!lastb1st) {counter++;} //if button is not pressed
lastb1time=millis();
}
if (lastb1time && lastb1time < millis()-10000 && !lastb1st) {
counter+=10;
Serial.println(counter);
delay(100); //otherwise this will go very quickly
}
}
I currently have a simple program working for incrementing and decrementing a counter when pressing an UP & DOWN pushbutton, however, I am not sure how to implement the part where I want the counter to increase/decrease by 10 after the button has been held down for more than 2 seconds.
Here is what I have so far:
int inPin1 = 5; // set input pin (for UP pushbutton)
int inPin2 = 6; // set input pin 6 (for DOWN pushbutton)
int val1 = 0; // variable for reading the pin 5 status
int val2 =0; // variable for reading the pin 6 status
int counter = 0; // initialize counter at 0
void setup() {
pinMode(inPin1, INPUT_PULLUP); // declare pushbutton UP as input
pinMode(inPin2, INPUT_PULLUP); // declare pushbutton DOWN as input
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop(){
val1 = digitalRead(inPin1); // read input value
if (val1 == LOW) { // check if the input is HIGH (button released)
counter++;
}
val2 = digitalRead(inPin2);
if (val2 == LOW) {
counter--;
}
Serial.println(counter);
delay(100);//
}