简体   繁体   English

运行一个卡在零的计时器代码

[英]Running a timer code which gets stuck at zero

OK so firstly, I am VERY new to C++ as you will undoubtedly see from the code.好的,首先,我对 C++ 非常陌生,您无疑会从代码中看到。 I have been struggling with this to get it to where it is now so my apologies if it's not very good.我一直在努力解决这个问题,以使其达到现在的水平,所以如果它不是很好,我深表歉意。

Ok here is what SHOULD happen.好的,这就是应该发生的事情。

Button 2 (downButton) is pressed to select the correct program (that works).按钮 2 (downButton) 被按下到 select 正确的程序(有效)。 Button 3 (startButton) is used to start the timer for that program (that works) The timer is supposed to count down (also working) and when it reaches zero, display a message and then go back to the start of the switch case.按钮 3 (startButton) 用于启动该程序的计时器(有效) 计时器应该倒计时(也有效),当它达到零时,显示一条消息,然后 go 回到开关盒的开始。

This is what I am struggling with.这就是我正在努力解决的问题。 Firstly the timer gets to zero and does nothing else.首先,计时器归零并且什么都不做。 I have tried return, goto etc without any success.我尝试过返回、转到等,但没有任何成功。

I commented out the end message function, which does work BUT still gets stuck there without exiting the function.我注释掉了结束消息 function,它确实有效,但在没有退出 function 的情况下仍然卡在那里。

What I am looking for is a little help to make the timer jump back to where it was called from.我正在寻找的是让计时器跳回到它被调用的地方的一点帮助。

Here is the code:这是代码:

#include <Wire.h>
#include <LiquidCrystal.h>

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

int hours = 0; // start hours
int minutes = 00; //start min
int seconds = 05; //start seconds

const int buttonPin = 9;      // the pin that the pushbutton is attached to
const int ledPin    = A0;     // the pin that the LED is attached to
const int ledPin2    = A1;     // the pin that the LED is attached to

int buttonState     = 0;      // current state of the button

int WhichScreen = 0;  // This variable stores the current Screen number
boolean hasChanged = true;
const int upButton = 8;  // the number of the select pin
const int downButton = 10;  // the number of the select pin
const int startButton = 9;   // the number of the start pin


int selectState;             // the current reading from the select pin
int lastSelectState = LOW;   // the previous reading from the select pin
int backState;             // the current reading from the select pin
int lastBackState = LOW;   // the previous reading from the select pin
int startState;             // the current reading from the start pin
int lastStartState = LOW;   // the previous reading from the start pin

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup()
{
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);  // initialize the button pin as a input
  pinMode(ledPin, OUTPUT);    // initialize the button pin as a output
  pinMode(ledPin2, OUTPUT);    // initialize the button pin as a output
  lcd.begin(16, 2);
  pinMode(upButton, INPUT);
  pinMode(startButton, INPUT);
  pinMode(downButton, INPUT);
  pinMode(A0, OUTPUT);    // LED 1
  pinMode(A1, OUTPUT);    // LED 2
  pinMode(A2, OUTPUT);    // LED 3

}
void loop()
{
start:
  // Read the SELECT pin
  if (WhichScreen == 1) {
    buttonState = digitalRead(buttonPin);

    if (buttonState == HIGH) {

      digitalWrite(ledPin, buttonState);
      delay(2000);
      digitalWrite(ledPin, LOW);
    }
  }


  if (WhichScreen == 4) {
    buttonState = digitalRead(buttonPin);

    if (buttonState == HIGH) {

      //   digitalWrite(ledPin2, buttonState);
      //   delay(5000);
      //   digitalWrite(ledPin2, LOW);

      lcd.begin(16, 2);

      minutes = 00; //start min
      seconds = 05; //start seconds
      digitalWrite(ledPin, buttonState);
      lcd.print("P1-AMBER");
      timer();

// Function SHOULD return to here when reaches zero

      digitalWrite(ledPin, LOW);
      minutes = 00; //start min
      seconds = 05; //start seconds
      lcd.print("P1-DARK RED");
      timer();
    }
  }





  if (hasChanged == true) {

    switch (WhichScreen) {

      case 0:
        {
          lcd.clear();
          lcd.setCursor(2, 0); // Column, line
          lcd.print("LIGHT THERAPY");
          lcd.setCursor(2, 1);
          lcd.print("PANEL   V1.01");
          delay(2500);    // will be removed once relays are installed
          lcd.clear();
          lcd.setCursor(4, 0); // Column, line
          lcd.print("WELCOME");
          lcd.setCursor(1, 1);
          lcd.print("GARY & TRACEY");
          delay(2500);    // will be removed once relays are installed
          WhichScreen++;
          program1();
        }
        break;

      case 1:
        {
          program1();

        }
        break;

      case 2:
        {
          program2();
        }
        break;

      case 3:
        {
          program3();
        }
        break;

      case 4:
        {
          program4();
        }
        break;

      case 5:
        {
          program5();
        }
        break;

      case 6:
        {
          program6();
        }
        break;

      case 7:
        {
          program7();
        }
        break;

      case 8:
        {
          program8();
        }
        break;

      case 9:
        {
          program9();
        }
        break;

    }
  }

  //-------------------------------
  // BEGIN of the switch debouncing code
  int reading = digitalRead(upButton);
  if (reading != lastSelectState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != selectState) {
      selectState = reading;

      // only toggle the LED if the new button state is HIGH
      if (selectState == HIGH) {
        hasChanged = true;
        WhichScreen++;


      }


    } else {
      hasChanged = false;
    }
  }
  lastSelectState = reading;
  // END of the switch Debouncing code
  // --------------------------------------
  if (WhichScreen > 9) {
    WhichScreen = 1;
  }
}

void program1()
{
  lcd.clear();
  lcd.setCursor(1, 0); // Column, line
  lcd.print("PROGRAM 1 (P1)");
  lcd.setCursor(3, 1);
  lcd.print("14 Minutes");
}
void program2()
{
  lcd.clear();
  lcd.setCursor(1, 0); // Column, line
  lcd.print("PROGRAM 2 (P2)");
  lcd.setCursor(3, 1);
  lcd.print("14 Minutes");
}
void program3()
{
  lcd.clear();
  lcd.setCursor(1, 0); // Column, line
  lcd.print("PROGRAM 3 (P3)");
  lcd.setCursor(3, 1);
  lcd.print("14 Minutes");
}
void program4()
{
  lcd.clear();
  lcd.setCursor(1, 0); // Column, line
  lcd.print("PROGRAM 4 (P4)");
  lcd.setCursor(3, 1);
  lcd.print("14 Minutes");
}
void program5()
{
  lcd.clear();
  lcd.setCursor(1, 0); // Column, line
  lcd.print("PROGRAM 5 (P5)");
  lcd.setCursor(3, 1);
  lcd.print("14 Minutes");
}
void program6()
{
  lcd.clear();
  lcd.setCursor(1, 0); // Column, line
  lcd.print("PROGRAM 6 (P6)");
  lcd.setCursor(3, 1);
  lcd.print("14 Minutes");
}
void program7()
{
  lcd.clear();
  lcd.setCursor(1, 0); // Column, line
  lcd.print("PROGRAM 7 (P7)");
  lcd.setCursor(3, 1);
  lcd.print("14 Minutes");
}
void program8()
{
  lcd.clear();
  lcd.setCursor(1, 0); // Column, line
  lcd.print("PROGRAM 8 (P8)");
  lcd.setCursor(3, 1);
  lcd.print("14 Minutes");
}
void program9()
{
  lcd.clear();
  lcd.setCursor(1, 0); // Column, line
  lcd.print("PROGRAM 9 (P9)");
  lcd.setCursor(3, 1);
  lcd.print("14 Minutes");
}
void stepDown() {
  if (seconds > 0) {
    seconds -= 1;
  } else {
    if (minutes > 0) {
      seconds = 59;
      minutes -= 1;
    } else {
      if (hours > 0) {
        seconds = 59;
        minutes = 59;
        hours -= 1;
      } else {
        //trigger();
      }
    }
  }
}


void timer() {
  delay(150);
  while (hours > 0 || minutes > 0 || seconds >= 0) {

    lcd.setCursor(0, 2);

    //(hours < 10) ? lcd.print("0") : NULL;
    // lcd.print(hours);
    //lcd.print(":");
    lcd.print("TIME LEFT: ");
    (minutes < 10) ? lcd.print("0") : NULL;

    lcd.print(minutes);
    lcd.print(":");
    (seconds < 10) ? lcd.print("0") : NULL;
    lcd.print(seconds);
    lcd.display();
    stepDown();
    delay(1000);

  }
}

void trigger() {
  lcd.clear(); // clears the screen and buffer
  lcd.setCursor(6, 0); // set timer position on lcd for end.
  lcd.print("TIMES UP");
  lcd.setCursor(6, 1); // set timer position on lcd for end.
  lcd.print("NUMBER 4");
  delay(1000);
  lcd.display();

}

Sorry it's a bit messy and all over the place, I'm also sure it's not flowing very well either but it works mostly lol.抱歉,它有点乱,到处都是,我也确定它流动得也不是很好,但它主要是哈哈。

Any and all help much appreciated, and thank you in advance.非常感谢任何和所有帮助,并提前感谢您。

In your timer function.在您的timer function 中。 You have a loop condition hours > 0 || minutes > 0 || seconds >= 0你有一个循环条件hours > 0 || minutes > 0 || seconds >= 0 hours > 0 || minutes > 0 || seconds >= 0 hours > 0 || minutes > 0 || seconds >= 0 . hours > 0 || minutes > 0 || seconds >= 0 Since your seconds is always greater than 0, you have a infinite loop in timer .由于您的seconds始终大于 0,因此您在timer中有一个无限循环。

As you can see, seconds does not decrease seconds == 0.如您所见,秒数不会减少秒数 == 0。

void stepDown() {
  if (seconds > 0) {
    seconds -= 1;
  } else {
    if (minutes > 0) {
      seconds = 59;
      minutes -= 1;
    } else {
      if (hours > 0) {
        seconds = 59;
        minutes = 59;
        hours -= 1;
      } else {
        //trigger();
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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