Advertisement
Maderdash

bounce

Aug 20th, 2022 (edited)
1,146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "RTClib.h"
  2. RTC_DS1307 rtc;
  3.  
  4. const int num_tasks = 6;
  5. bool timeisup = false;                // is the time up ?
  6. bool  failedtasks = false;
  7. bool active[num_tasks ];
  8.  
  9. int switchPins[num_tasks ] = {52, 53, 50, 51, 48, 49}; // list of pins
  10. int GreenLedPins[num_tasks ] = {22, 24, 26, 28, 30, 32}; // list of pins
  11. int RedLedPins[num_tasks ] = {23, 25, 27, 29, 31, 33}; // list of pins
  12. int endHour;
  13. int endMinute;
  14. int previousSeconds;
  15.  
  16. char comments[num_tasks ][15] =  // the number
  17. {
  18.   "Showering,\t", // change these to possible task names and make fit in "box"
  19.   "Eating,\t",
  20.   "Locking door,\t",
  21.   "Washing,\t",
  22.   "Phone Family,\t",
  23.   "Medication,\t"
  24. }                           // list of task names
  25.  
  26.  
  27. void setup() {
  28.   Serial.begin(115200);
  29.   if (! rtc.begin()) {
  30.     Serial.println("Couldn't find RTC");
  31.     Serial.flush();
  32.     abort();
  33.   }
  34.   if (! rtc.isrunning()) {
  35.     Serial.println("RTC is NOT running, let's set the time!");
  36.     rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  37.   }
  38.   DateTime  now = rtc.now(); // rtc .now is the reading gives . hour . min .sec
  39.   endHour = now.hour();        // test junk
  40.   endMinute = now.minute() + 1; // test junk
  41.   for ( int x = 0; x < num_tasks ; x++){
  42.     active[x] = false;                      // sets all tasks to not compteted
  43.     pinMode(switchPins[x] , INPUT_PULLUP);    // sets switches pins to input pull up
  44.     pinMode(GreenLedPins[x] , OUTPUT);        // sets green leds pins to output
  45.     pinMode(RedLedPins[x ], OUTPUT);          // sets red leds pins to output
  46.     digitalWrite(GreenLedPins[x], active[x]); // sets the initial state
  47.     digitalWrite(RedLedPins[x], !active[x]);  // sets the initial state
  48.   }
  49. }
  50.  
  51. void loop() {
  52.   DateTime now = rtc.now();    // get the time
  53.   if (!timeisup && endHour == now.hour() && endMinute == now.minute()){
  54.     // check the time isnt up checking for same hour and same min
  55.     Serial.println ( " END OF TIME");  // if same min and hour than time is up is true
  56.     timeisup = true;
  57.   }else{
  58.     if (!timeisup ){
  59.       // print some diag stuff
  60.       diagnostics(now);
  61.     }
  62.   }
  63.   for ( int x = 0; x < num_tasks ; x++){ // creates x and starts at 0 checks all the way trhough to 5
  64.     if (digitalRead(switchPins[x]) == 1){ // switch pins is array of switches
  65.       active[x] = true;
  66.       digitalWrite(GreenLedPins[x], active[x]);
  67.       digitalWrite(RedLedPins[x], !active[x]);
  68.     }
  69.   }
  70.   if ( timeisup ){
  71.     for ( int x = 0; x < num_tasks ; x++){
  72.       if (!active[x]){            // if the task isnt finished
  73.         Serial.print(comments[x]);  // print out the failed task name
  74.         Serial.println(active[x]);
  75.         failedtasks = true;         // flag for one or more tasks not finished
  76.       }
  77.     }
  78.     if (failedtasks){       // if any failed tasks
  79.    
  80.       Serial.println(" Failed to complete "); //stick in email code here
  81.       failedtasks = false;
  82.     }else{
  83.       Serial.println(" All tasks completed "); //stick in email code
  84.     }
  85.     delay(5000);    // just to see the results
  86.     endHour = now.hour();          // test value
  87.     endMinute = now.minute() + 1;  // test value
  88.     // set every thing back to startup values
  89.     reset();
  90.   }
  91. }
  92.  
  93.  
  94. void diagnostics (DateTime nowthis){
  95.   if (previousSeconds != nowthis.second()){
  96.     Serial.print("End time ");
  97.     Serial.print(endHour , DEC);
  98.     Serial.print(":");
  99.     Serial.print(endMinute, DEC);
  100.     Serial.print(":00");
  101.     Serial.print(" Present time ");
  102.     Serial.print(nowthis.hour(), DEC);
  103.     Serial.print(':');
  104.     Serial.print(nowthis.minute(), DEC);
  105.     Serial.print(':');
  106.     Serial.print(nowthis.second(), DEC);
  107.     Serial.println();
  108.     previousSeconds = nowthis.second();
  109.   }
  110. }
  111. void reset(){
  112.   for ( int x = 0; x < num_tasks ; x++){
  113.     active[x] = false;                      // sets all tasks to not compteted
  114.     digitalWrite(GreenLedPins[x], active[x]); // sets the initial state
  115.     digitalWrite(RedLedPins[x], !active[x]);  // sets the initial state
  116.   }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement