Arduino Polska Forum

Pełna wersja: Arduino Micro- projekt minutnika, problem
Aktualnie przeglądasz uproszczoną wersję forum. Kliknij tutaj, by zobaczyć wersję z pełnym formatowaniem.
Witam serdecznie, mam problem z poniższym programem. Wszystko działa jak należy, odlicza czas, natomiast po ustawieniu więcej niż 0 godzin odliczanie czasu nie uruchamia się.
Jest to projekt na zajęcia, ma być na przerwaniach, mamy 3 przyciski, pierwszym zatwierdzamy np ilość godzin i przechodzimy dalej a pozostałymi dwoma wybieramy ilość czasu.
po przejściu do counter() uruchamia się odliczanie czasu, jednak gdy liczba godzin jest większa niż 0 odliczanie czasu nie uruchamia się. Proszę o pomoc

Kod:
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(3, 4, 5, 6, 7, 8); //Informacja o podłączeniu nowego wyświetlacza

  int reset=10;
  int hours=0;
  int minutes=0;
  int seconds=0;
  unsigned long previousMillis = 0;
  const long interval = 1000;
  int count=0;
  int steps=0;
 
void setup() {
  digitalWrite(reset, HIGH);
  pinMode(reset, OUTPUT);
  Serial.begin(9600);
  lcd.begin(16, 2); //deklaracja typu
  lcd.setCursor(0,0); //ustawienie kursora
  lcd.print("Minutnik RTC"); //Wyświetlenie tekstu
  lcd.setCursor(0, 1); //Ustawienie kursora
  lcd.print("z obsluga przerwan"); //Wyświetlenie tekstu
  delay(2000);
   
  pinMode(0, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(0), ISR1, LOW);
  pinMode(1, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(1), ISR2, LOW);
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), ISR3, LOW);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (steps==1){
  hours=count;
  if (count > 24){
    count = 0;
  }
  if (count < 0){
    count = 24;
  }
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Set hours: ");
  lcd.print(hours);
  }
  if (steps==2){
  minutes=count;
  if (count > 59){
    count = 0;
  }
  if (count < 0){
    count = 59;
  }
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Set minutes: ");
  lcd.print(minutes);
  }
  if (steps==3){
  seconds=count;
  if (count > 59){
    count = 0;
  }
  if (count < 0){
    count = 59;
  }
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Set seconds: ");
  lcd.print(seconds);
  }
  if (steps==4){
  counter();
}
}
// obsługa przerwania T1 (przycisk P1)
void ISR1(){
  static unsigned long lastTime;
  unsigned long timeNow = millis();
  if (timeNow - lastTime < 500)
    return;
   
  steps++;
  count=0;
  lastTime = timeNow;
}
// obsługa przerwania T2 (przycisk P2)
void ISR2(){
  static unsigned long lastTime;
  unsigned long timeNow = millis();
  if (timeNow - lastTime < 200)
    return;

  count++;
  lastTime = timeNow;
}
// obsługa przerwania T3 (przycisk P3)
void ISR3(){
static unsigned long lastTime;
  unsigned long timeNow = millis();
  if (timeNow - lastTime < 200)
    return;

  count--;
  lastTime = timeNow;
}
//obliczanie czasu
void counter(){
  unsigned long countdown;
  unsigned long timepassed;
  countdown=hours*3600+minutes*60+seconds*1;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    for (timepassed=0; timepassed < countdown; timepassed++){
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Remaining:");
      lcd.setCursor(14,1);
      lcd.print(seconds);
      lcd.setCursor(13,1);
      lcd.print(":");
      lcd.setCursor(11,1);
      lcd.print(minutes);
      lcd.setCursor(10,1);
      lcd.print(":");
      lcd.setCursor(8,1);
      lcd.print(hours);       
    }
    seconds--;
    if (seconds < 0){
      seconds=59;
      minutes--;
    }
    if (minutes < 0){
      minutes=59;
      hours--;
    }       
  }
  if (timepassed == 0){
    KaBoom();
  }
  Serial.println(timepassed);
  //Serial.println(seconds);

// obsługa brzęczka
void KaBoom(){
for (int a=0; a<20; a++){
  lcd.clear();
  lcd.print("KABOOOM"); 
  tone(11, 1000);
  delay(300);
  tone(11, 800);
  delay(400);
}
digitalWrite(reset, LOW);
}
Jaki uc??
Chyba nie rozumiem logiki tego projektu.

Funkcję counter() wywołujesz z loop i sprawdzasz czy minęła  1s od poprzedniej kontroli - oczywista sprawa.
Ale czemu w samej funkcji masz "for (timepassed=0; timepassed < countdown; timepassed++){" ?
countdown jest to ilość s do końca ale cykl pętli nie jest 1 s.

Kod:
void counter(){
  unsigned long countdown;
  unsigned long timepassed;
  countdown=hours*3600+minutes*60+seconds*1;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
  //  for (timepassed=0; timepassed < countdown; timepassed++){///<po co jest ta pętla? spróbuj bez niej.
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Remaining:");
      lcd.setCursor(14,1);
      lcd.print(seconds);
      lcd.setCursor(13,1);
      lcd.print(":");
      lcd.setCursor(11,1);
      lcd.print(minutes);
      lcd.setCursor(10,1);
      lcd.print(":");
      lcd.setCursor(8,1);
      lcd.print(hours);      
    //}
    seconds--;
    if (seconds < 0){
      seconds=59;
      minutes--;
    }
    if (minutes < 0){
      minutes=59;
      hours--;
    }      
  }
  if((hours==0)&&(minutes ==0)&&(seconds==0)){//if (timepassed == 0){
    KaBoom();
  }
  Serial.println(timepassed);
  //Serial.println(seconds);
}
tak naprawdę 
to 
  unsigned long countdown;
  unsigned long timepassed;
 są nie potrzebne.

if (timepassed == 0){ można zamienić na if((hours==0)&&(minutes ==0)&&(seconds==0)){