Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Pins
- const int latchPin = 12;
- const int clockPin = 13;
- const int dataPin = 11;
- const int buttonPin = 9;
- const int ledRed = 6;
- const int ledGreen = 7;
- const int buzzer = 5;
- const int toggleDemo = 8;
- const int togglePower = 4;
- const int lcdBacklight = 1;
- int powerButtonPressed = 0;
- int powerOn = 1;
- int alarmOn = LOW;
- unsigned long currentMillis = millis();
- long previousMillis = 0;
- long previousBlink = 0;
- int blinkDelay = 200;
- int percentageUntilNext = 0;
- int redLedState = -1;
- int demoInterval = 5000; // This interval is used after the demo interval button is pressed
- long realInterval = 3.5 * 1000 * 60 * 60; // 3.5 hours
- long interval = realInterval;
- int demoButtonPressed = 0;
- // LCD Display
- int alarmTime = 0;
- #include <LiquidCrystal.h>
- char array1[] = "";
- char array2[] = "";
- LiquidCrystal lcd(2, 3, 10, 11, 12, 13);
- void setup() {
- // put your setup code here, to run once:
- pinMode(latchPin, OUTPUT);
- pinMode(clockPin, OUTPUT);
- pinMode(dataPin, OUTPUT);
- pinMode(buttonPin, INPUT);
- pinMode(toggleDemo, INPUT);
- pinMode(ledRed, OUTPUT);
- pinMode(ledGreen, OUTPUT);
- pinMode(buzzer, OUTPUT);
- pinMode(lcdBacklight, OUTPUT);
- lcd.begin(16, 2);
- digitalWrite(lcdBacklight, HIGH);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- if (powerOn == 1) {
- // Update currentMillis each time it loops
- currentMillis = millis();
- // If it the time since the last press is over the interval
- if (currentMillis - previousMillis > interval) {
- alarmTime++;
- if (alarmTime > 5) {
- alarmTime = 2;
- }
- if (alarmTime == 1) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print(" Time to take ");
- lcd.setCursor(0, 1);
- lcd.print(" Rocket out! ");
- }
- digitalWrite(ledGreen, LOW);
- if ((unsigned long)(currentMillis - previousBlink) > blinkDelay) {
- // This will run every blinkDelay (200) milliseconds while the timer is up
- // The buzzer should be turned on and off unlike the led
- for (int i = 0; i < 20; i++) {
- digitalWrite(buzzer, HIGH);
- delay(2);
- digitalWrite(buzzer, LOW);
- delay(2);
- }
- previousBlink = currentMillis;
- // Negate the value of the red LED
- redLedState = -redLedState;
- if (redLedState > 0) {
- digitalWrite(ledRed, LOW);
- } else {
- digitalWrite(ledRed, HIGH);
- }
- }
- // The led can be on constantly while the time is up
- } else {
- // If the time since last press is still below the interval
- alarmTime = 0;
- digitalWrite(buzzer, LOW);
- percentageUntilNext = (int)((double)((double)(currentMillis - previousMillis) / (double)interval) * 100);
- // Make the red led get brigher as it gets closer to beeping
- analogWrite(ledRed, map(percentageUntilNext, 0, 100, 0, 255));
- if (percentageUntilNext > 65) {
- digitalWrite(ledGreen, HIGH);
- } else {
- digitalWrite(ledGreen, LOW);
- }
- // LCD display
- lcd.setCursor(6, 0);
- lcd.print(percentageUntilNext);
- lcd.setCursor(9, 0);
- lcd.print("%");
- // Bottom line
- double hoursLeftExact = ((((double)((100 - percentageUntilNext) * interval) / 60) / 60) / 1000) / 100;
- int hoursLeft = floor(hoursLeftExact);
- int minutesLeft = (int)((double)((double)((int)(hoursLeftExact * 100) % 100) / 100) * 60);
- lcd.setCursor(0, 2);
- lcd.print(hoursLeft);
- lcd.setCursor(1, 2);
- lcd.print(" hrs ");
- lcd.setCursor(6, 2);
- lcd.print(minutesLeft);
- int lastPos = 7;
- if (minutesLeft > 9) {
- lastPos = 8;
- }
- lcd.setCursor(lastPos, 2);
- lcd.print(" min left");
- }
- // If the button is pressed
- if (digitalRead(buttonPin) == HIGH) {
- previousMillis = currentMillis;
- lcd.clear();
- alarmOn = LOW;
- }
- // If the 'toggle demo' button is pressed
- if (digitalRead(toggleDemo) == HIGH) {
- demoButtonPressed++;
- if (demoButtonPressed > 1000) {
- demoButtonPressed = 200;
- }
- if (demoButtonPressed == 1) {
- if (interval == demoInterval) {
- interval = realInterval;
- } else {
- interval = demoInterval;
- }
- previousMillis = currentMillis;
- lcd.clear();
- }
- } else {
- demoButtonPressed = 0;
- }
- }
- // If the 'toggle power' button is pressed
- if (digitalRead(togglePower) == HIGH) {
- powerButtonPressed++;
- if (powerButtonPressed > 1000) {
- powerButtonPressed = 200;
- }
- if (powerButtonPressed == 1) {
- if (powerOn == 0) {
- powerOn = 1;
- previousMillis = currentMillis;
- digitalWrite(lcdBacklight, HIGH);
- } else {
- powerOn = 0;
- // Reset LCD display
- lcd.clear();
- digitalWrite(ledRed, LOW); // Turn off led
- digitalWrite(ledGreen, LOW); // Turn off led
- digitalWrite(lcdBacklight, LOW);
- }
- }
- } else {
- powerButtonPressed = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment