Exit an IF statement within a function and passing the current value of a variable to another function

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:

  1. 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.
  2. 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!

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#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
}
</code>
<code>#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 } </code>
#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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật