简体   繁体   中英

How can I get out of the loop?

I'd like to get out of the loop in my Arduino project. Currently I am programming a digital watch, everything works just fine but I wanted to add options menu by clicking a button, but after clicking it nothing pops up even if I have something inside the code. Take a look at it. I don't know how to write it better. If you have some ideas, please you could rewrite some parts of the code and explain why you did so. Thanks forwardly.

Please do not mind s = s + 1, I wanted it like that.

#include "LiquidCrystal.h"
#include <EEPROM.h>

LiquidCrystal lcd(12,11,5,4,3,2);

int h = 0;
int m = 0;
int s = 0;
int right = 8;
int left = 9;
int buttonStateLeft = 0;
String when;


uint8_t EEPROMaddress_sec = 1;
uint8_t EEPROMaddress_min = 2;
uint8_t EEPROMaddress_hour = 3;

bool clockShown = true;
bool menuShown = false;

void setup()
{
  lcd.begin(16,2);
  pinMode(right, INPUT);
  pinMode(left, INPUT);
}

void loop()
{

  if(menuShown)
  {
        lcd.setCursor(0,0);
        lcd.print("jozo je kkt");
        delay(200);
  }


  if(clockShown) {

      lcd.setCursor(0,0);
      buttonStateLeft = digitalRead(left);

      if(buttonStateLeft == HIGH)
      {
        clockShown = false;
        menuShown = true;
        lcd.clear();
      }

      s = EEPROM.read(EEPROMaddress_sec);
      m = EEPROM.read(EEPROMaddress_min);
      h = EEPROM.read(EEPROMaddress_hour);

      s = s + 1;

      if(h > 12)
      when = "PM";
      if(h < 12)
      when = "AM";
      if(h == 12)
      when = "PM";

      lcd.print("Cas: ");
      if(h<10)lcd.print("0");
      lcd.print(h);
      lcd.print(":");
      if(m<10)lcd.print("0");
      lcd.print(m);
      lcd.print(":");
      if(s<10)lcd.print("0");
      lcd.print(s);
      lcd.print(" ");
      lcd.print(when);

      if(s == 60)
      {
        s = 0;
        m = m+1;
      }

      if(m == 60)
      {
        s = 0;
        m = 0;
        h = h+1;
      }
      if(h == 24)
      {
        m = 0;
        s = 0;
        h = 0;
      }

      EEPROM.write(EEPROMaddress_sec, s);
      EEPROM.write(EEPROMaddress_min, m);
      EEPROM.write(EEPROMaddress_hour, h);

      delay(1000);
  }
}

In order to do that you will have to use Interrupts, note that you must connect your button to an interrupt pin (not every pin is an interrupt pin) you can google "what are the interrupt pins of 'your_card_name' ", the code would have to change, you can follow these :

In the setup function replace :

 pinMode(left, INPUT);

by :

 attachInterrupt(digitalPinToInterrupt(left), switchMode, RISING);

add this function before setup(){...}

 int lastPressTime=millis();
 void switchMode(){ // function called when the button is pressed
    if((millis()-lastPressTime)>60){ // for debouncing
     clockShown = false;
     menuShown = true;
     lcd.clear();
     lastPressTime=millis();
    }
 }

and remove this part from your code : (the one in the loop() function)

        buttonStateLeft = digitalRead(left);

  if(buttonStateLeft == HIGH)
  {
    clockShown = false;
    menuShown = true;
    lcd.clear();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM