MrGarretto

timer to take out rocket

Feb 21st, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.20 KB | None | 0 0
  1. // Pins
  2. const int latchPin = 12;
  3. const int clockPin = 13;
  4. const int dataPin = 11;
  5. const int buttonPin = 9;
  6. const int ledRed = 6;
  7. const int ledGreen = 7;
  8. const int buzzer = 5;
  9. const int toggleDemo = 8;
  10. const int togglePower = 4;
  11. const int lcdBacklight = 1;
  12.  
  13. int powerButtonPressed = 0;
  14.  
  15. int powerOn = 1;
  16.  
  17. int alarmOn = LOW;
  18.  
  19. unsigned long currentMillis = millis();
  20. long previousMillis = 0;
  21.  
  22. long previousBlink = 0;
  23. int blinkDelay = 200;
  24. int percentageUntilNext = 0;
  25.  
  26. int redLedState = -1;
  27.  
  28. int demoInterval = 5000; // This interval is used after the demo interval button is pressed
  29. long realInterval = 3.5 * 1000 * 60 * 60; // 3.5 hours
  30. long interval = realInterval;
  31.  
  32. int demoButtonPressed = 0;
  33.  
  34. // LCD Display
  35.  
  36. int alarmTime = 0;
  37.  
  38. #include <LiquidCrystal.h>
  39.  
  40. char array1[] = "";
  41. char array2[] = "";
  42.  
  43. LiquidCrystal lcd(2, 3, 10, 11, 12, 13);
  44.  
  45.  
  46. void setup() {
  47.   // put your setup code here, to run once:
  48.   pinMode(latchPin, OUTPUT);
  49.   pinMode(clockPin, OUTPUT);
  50.   pinMode(dataPin, OUTPUT);
  51.   pinMode(buttonPin, INPUT);
  52.   pinMode(toggleDemo, INPUT);
  53.   pinMode(ledRed, OUTPUT);
  54.   pinMode(ledGreen, OUTPUT);
  55.   pinMode(buzzer, OUTPUT);
  56.   pinMode(lcdBacklight, OUTPUT);
  57.  
  58.   lcd.begin(16, 2);
  59.   digitalWrite(lcdBacklight, HIGH);
  60. }
  61.  
  62. void loop() {
  63.   // put your main code here, to run repeatedly:
  64.  
  65.   if (powerOn == 1) {
  66.     // Update currentMillis each time it loops
  67.     currentMillis = millis();
  68.  
  69.     // If it the time since the last press is over the interval
  70.     if (currentMillis - previousMillis > interval) {
  71.  
  72.       alarmTime++;
  73.       if (alarmTime > 5) {
  74.         alarmTime = 2;
  75.       }
  76.  
  77.       if (alarmTime == 1) {
  78.         lcd.clear();
  79.         lcd.setCursor(0, 0);
  80.         lcd.print("  Time to take  ");
  81.         lcd.setCursor(0, 1);
  82.         lcd.print("  Rocket out!   ");
  83.       }
  84.  
  85.       digitalWrite(ledGreen, LOW);
  86.  
  87.       if ((unsigned long)(currentMillis - previousBlink) > blinkDelay) {
  88.         // This will run every blinkDelay (200) milliseconds while the timer is up
  89.         // The buzzer should be turned on and off unlike the led
  90.         for (int i = 0; i < 20; i++) {
  91.           digitalWrite(buzzer, HIGH);
  92.           delay(2);
  93.           digitalWrite(buzzer, LOW);
  94.           delay(2);
  95.         }
  96.  
  97.         previousBlink = currentMillis;
  98.  
  99.         // Negate the value of the red LED
  100.         redLedState = -redLedState;
  101.         if (redLedState > 0) {
  102.           digitalWrite(ledRed, LOW);
  103.         } else {
  104.           digitalWrite(ledRed, HIGH);  
  105.         }
  106.       }
  107.  
  108.       // The led can be on constantly while the time is up
  109.        
  110.     } else {
  111.       // If the time since last press is still below the interval
  112.  
  113.       alarmTime = 0;
  114.  
  115.       digitalWrite(buzzer, LOW);
  116.  
  117.       percentageUntilNext = (int)((double)((double)(currentMillis - previousMillis) / (double)interval) * 100);
  118.       // Make the red led get brigher as it gets closer to beeping
  119.       analogWrite(ledRed, map(percentageUntilNext, 0, 100, 0, 255));
  120.  
  121.       if (percentageUntilNext > 65) {
  122.         digitalWrite(ledGreen, HIGH);
  123.       } else {
  124.         digitalWrite(ledGreen, LOW);
  125.       }
  126.  
  127.       // LCD display
  128.       lcd.setCursor(6, 0);
  129.       lcd.print(percentageUntilNext);
  130.       lcd.setCursor(9, 0);
  131.       lcd.print("%");
  132.  
  133.       // Bottom line
  134.       double hoursLeftExact = ((((double)((100 - percentageUntilNext) * interval) / 60) / 60) / 1000) / 100;
  135.       int hoursLeft = floor(hoursLeftExact);
  136.       int minutesLeft = (int)((double)((double)((int)(hoursLeftExact * 100) % 100) / 100) * 60);
  137.  
  138.       lcd.setCursor(0, 2);
  139.       lcd.print(hoursLeft);
  140.       lcd.setCursor(1, 2);
  141.       lcd.print(" hrs ");
  142.       lcd.setCursor(6, 2);
  143.       lcd.print(minutesLeft);
  144.       int lastPos = 7;
  145.       if (minutesLeft > 9) {
  146.         lastPos = 8;  
  147.       }
  148.       lcd.setCursor(lastPos, 2);
  149.       lcd.print(" min left");
  150.     }
  151.  
  152.     // If the button is pressed
  153.     if (digitalRead(buttonPin) == HIGH) {
  154.       previousMillis = currentMillis;
  155.       lcd.clear();
  156.       alarmOn = LOW;
  157.     }
  158.  
  159.     // If the 'toggle demo' button is pressed
  160.     if (digitalRead(toggleDemo) == HIGH) {
  161.       demoButtonPressed++;
  162.       if (demoButtonPressed > 1000) {
  163.         demoButtonPressed = 200;  
  164.       }
  165.  
  166.       if (demoButtonPressed == 1) {
  167.         if (interval == demoInterval) {
  168.           interval = realInterval;  
  169.         }  else {
  170.           interval = demoInterval;  
  171.         }
  172.         previousMillis = currentMillis;
  173.         lcd.clear();
  174.       }
  175.     } else {
  176.       demoButtonPressed = 0;  
  177.     }
  178.   }
  179.  
  180.   // If the 'toggle power' button is pressed
  181.   if (digitalRead(togglePower) == HIGH) {
  182.     powerButtonPressed++;
  183.     if (powerButtonPressed > 1000) {
  184.       powerButtonPressed = 200;  
  185.     }
  186.  
  187.     if (powerButtonPressed == 1) {
  188.       if (powerOn == 0) {
  189.         powerOn = 1;
  190.         previousMillis = currentMillis;
  191.         digitalWrite(lcdBacklight, HIGH);
  192.       } else {
  193.         powerOn = 0;
  194.         // Reset LCD display
  195.         lcd.clear();
  196.        
  197.         digitalWrite(ledRed, LOW); // Turn off led
  198.         digitalWrite(ledGreen, LOW); // Turn off led
  199.         digitalWrite(lcdBacklight, LOW);
  200.       }
  201.     }
  202.   } else {
  203.     powerButtonPressed = 0;  
  204.   }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment