For this Arduino project, I am struggling to figure out what might be the solution to this problem and would appreciate your help. I am trying to achieve the following:
- Within the main function, call the function timeSet() and set an integer value for variable buttonPushCounter. This is achieved through the functions checkDown() and checkUp() and seems to work well.
- At any chosen point I want to exit the loop of the timeSet() function, use the current integer value under variable buttonPushCounter and use that in another function
However I am really struggling to understand how to best do that since everything I have tried has not worked.
Any advice would be appreciated.
Thank you!
#include <Wire.h>
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// constants which won't change:
const int Up_buttonPin = 9; // the pin that the upbutton is attached to
const int Down_buttonPin = 7; // the pin that the down is attached to
const int End_buttonPin = 8; // the pin that the end button is attached to
const int relayPin = 6;
// Variables which will change:
int buttonPushCounter = 0; // counter for the number of button presses
int up_buttonState = 0; // current state of the up button
int up_lastButtonState = 0; // previous state of the up button
int down_buttonState = 0; // current state of the up button
int down_lastButtonState = 0; // previous state of the up button
bool bPress = false;
int end_buttonState = 0; // current state of the end button
void setup()
{
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
pinMode(Up_buttonPin, INPUT_PULLUP);
pinMode(Down_buttonPin, INPUT_PULLUP);
pinMode(End_buttonPin, INPUT_PULLUP);
lcd.begin(16, 2); // initialize the lcd
// Print a message to the LCD.
lcd.setCursor(0, 0);
lcd.print("Select runtime:");
lcd.setCursor(2, 1);
lcd.print(buttonPushCounter);
lcd.setCursor(4, 1);
lcd.print("hours");
}
void loop()
{
timeSet();
}
int timeSet() {
checkUp();
checkDown();
if (bPress) {
bPress = false;
lcd.setCursor(2, 1);
lcd.print(" ");
lcd.setCursor(2, 1);
lcd.print(buttonPushCounter);
lcd.setCursor(4, 1);
lcd.print("hours");
}
return (buttonPushCounter);
}
void checkUp()
{
up_buttonState = digitalRead(Up_buttonPin);
// compare the buttonState to its previous state
if (up_buttonState != up_lastButtonState) {
// if the state has changed, increment the counter
if (up_buttonState == LOW) {
bPress = true;
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
up_lastButtonState = up_buttonState;
}
void checkDown()
{
down_buttonState = digitalRead(Down_buttonPin);
// compare the buttonState to its previous state
if (down_buttonState != down_lastButtonState) {
// if the state has changed, increment the counter
if (down_buttonState == LOW) {
bPress = true;
// if the current state is HIGH then the button went from off to on:
buttonPushCounter--;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
down_lastButtonState = down_buttonState;
}
void pump() {
digitalWrite(relayPin, HIGH); // Turn on the pump
delay(1000); // Run for counter seconds (assuming counter is in seconds)
digitalWrite(relayPin, LOW); // Turn off the pump
delay(5000); // Wait for 5 seconds before allowing another pump activation
}
I tried to exit the IF statement with another physical button by looking for state changes in it but that had no impact