Untitled

 avatar
unknown
plain_text
a year ago
3.0 kB
3
Indexable
#include <LiquidCrystal.h>

int seconds = 0;
int minutes = 0;
int count = 0;
bool isPaused = false;
unsigned long milliOffset = 0;
unsigned long pauseStart = 0;

// declare variables for the length of each period and the number of sessions before a longer break
const int study_minutes = 25;
const int short_break_minutes = 5;
const int long_break_minutes = 15;
const int repeats = 4;

// a variable used to time the breaks later
int break_duration;

// declare pins used for hardware
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int led1 = 6;
const int led2 = 7;
const int startButton = 8;
const int pauseButton = 9;

void setup() {
  lcd.begin(16, 2); // Set up the number of columns and rows on the LCD.
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(startButton, INPUT);
  pinMode(pauseButton, INPUT);

  // display initial message to user
  lcd.print("Press button");
  lcd.setCursor(0, 1);
  lcd.print("to start");

  // wait for start button press to start
  while (digitalRead(startButton) == LOW) {
    // do nothing
  }
}

void loop() {
  count = 0; // set count to zero

  while (count < repeats) {
    // Study period
    lcd.clear();
    lcd.print("Study time!");
    digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);
    startTimer(study_minutes);

    // Break period
    lcd.clear();
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);

    if (count == (repeats - 1)) { // do a long break on the last repetition
      break_duration = long_break_minutes;
      lcd.print("Long break!");
    } else { // otherwise do a short break
      break_duration = short_break_minutes;
      lcd.print("Short break!");
    }

    startTimer(break_duration);
    count++; // increment the counter for the number of study sessions
  }
}

void startTimer(int duration) {
  minutes = 0;

  while (minutes < duration) {
      seconds = 0;
      milliOffset = millis();
      while (seconds < 60) {
      if (digitalRead(pauseButton) == HIGH) {
        isPaused = !isPaused; // Toggle pause state
        delay(300); // Debounce delay
        milliOffset += 300;
        lcd.clear();
        if (isPaused) {
     	  pauseStart = millis();
          lcd.print("Study time!");
          lcd.setCursor(0, 1);
          lcd.print("Paused!");
        }
      }

      if (!isPaused) {
        lcd.setCursor(0, 1);
        if (minutes < 10) lcd.print("0");
        lcd.print(minutes);
        lcd.print(":");
        if (seconds < 10) lcd.print("0");
        lcd.print(seconds);
        seconds = (millis() - milliOffset) - ((millis() - milliOffset) % 1000);
        seconds = seconds / 1000;
      }

      while (isPaused) {
        if (digitalRead(pauseButton) == HIGH) {
          milliOffset += (millis() - pauseStart);
          pauseStart = 0;
          isPaused = !isPaused; // Toggle pause state
          delay(300); // Debounce delay
          milliOffset += 300;
          lcd.clear();
        }
      }
    }
    minutes++;
  }
}
Editor is loading...
Leave a Comment