Advertisement
elektronek

Jan Konečný - časové spínání 2

Feb 8th, 2021
1,459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <DS1302.h>
  3.  
  4. // Create a DS1302 object.
  5. const int kCePin   = 3;  // Chip Enable
  6. const int kIoPin   = 4;  // Input/Output
  7. const int kSclkPin = 5;  // Serial Clock
  8. DS1302 rtc(kCePin, kIoPin, kSclkPin);
  9.  
  10. int ledPin = 13;                // choose the pin for the LED
  11. int inputPin = 2;               // choose the input pin (for PIR sensor)
  12. int pirState = LOW;             // we start, assuming no motion detected
  13. int val = 0;                    // variable for reading the pin status
  14.  
  15. // Start time
  16. int hodinyOn=20;
  17. int minutyOn=30;
  18. // Stop time
  19. int hodinyOff=22;
  20. int minutyOff=30;
  21. // Prikladne cas ted - skutecny se vezme z RTC
  22. int hodinyTed=20;
  23. int minutyTed=32;
  24.  
  25. void setup() {
  26.     Serial.begin(9600);
  27.     pinMode(ledPin, OUTPUT);      // declare LED as output
  28.     pinMode(inputPin, INPUT);     // declare sensor as input
  29.     // Initialize a new chip by turning off write protection and clearing the
  30.     // clock halt flag. These methods needn't always be called. See the DS1302
  31.     // datasheet for details.
  32.     rtc.writeProtect(false);
  33.     rtc.halt(false);
  34.  
  35.     // Make a new time object to set the date and time.
  36.     // Sunday, September 22, 2013 at 01:38:50.
  37.     // Time t(2021, 2, 8, 18, 55, 00, Time::kMonday);
  38.  
  39.     // Set the time and date on the chip.
  40.     // rtc.time(t);
  41. }
  42.  
  43. void loop()
  44. {
  45.    
  46.     Time t = rtc.time();
  47.     hodinyTed = t.hr;
  48.     minutyTed = t.min;
  49.    
  50.     Serial.println("##############");
  51.     Serial.print("Start ");
  52.     Serial.print(hodinyOn);
  53.     Serial.println(minutyOn);
  54.     Serial.print("Stop ");
  55.     Serial.print(hodinyOff);
  56.     Serial.println(minutyOff);
  57.     Serial.print("Now ");
  58.     Serial.print(hodinyTed);
  59.     Serial.println(minutyTed);
  60.     Serial.println("-----------");
  61.        
  62.     // Prevedeme casy na minuty
  63.     int startTime = hodinyOn*60+minutyOn;
  64.     int stopTime = hodinyOff*60+minutyOff;
  65.     int casTed = hodinyTed*60+minutyTed;
  66.  
  67.     // Vypis casu prevedenych na minuty
  68.     Serial.print("Start");
  69.     Serial.println(startTime);
  70.     Serial.print("Stop");
  71.     Serial.println(stopTime);
  72.     Serial.print("Now");
  73.     Serial.println(casTed);
  74.     Serial.println("---------------");
  75.  
  76.     // Vyhodnocujeme, prvni tretina plati pro casy co nejsou pres pulnoc, dalsi 2/3 jsou pro casy co jdou pres pulnoc
  77.     if ((startTime < stopTime && casTed > startTime && casTed < stopTime) || (startTime > stopTime && casTed > startTime) || (startTime > stopTime && casTed < stopTime)){
  78.         Serial.println("Svitime");
  79.         val = digitalRead(inputPin);  // read input value
  80.         if (val == HIGH)  // check if the input is HIGH
  81.         {            
  82.             digitalWrite(ledPin, HIGH);  // turn LED ON
  83.             if (pirState == LOW)
  84.             {
  85.                 Serial.println("Motion detected!"); // print on output change
  86.                 pirState = HIGH;
  87.             }
  88.         }
  89.         else
  90.         {
  91.             digitalWrite(ledPin, LOW); // turn LED OFF
  92.             if (pirState == HIGH)
  93.             {
  94.                 Serial.println("Motion ended!");  // print on output change
  95.                 pirState = LOW;
  96.             }
  97.         }
  98.     }
  99.     delay(1000);
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement