Advertisement
elektronek

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

Feb 11th, 2021 (edited)
1,685
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 = 7;                // 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. unsigned long lastOn;
  15.  
  16.  
  17. // Start time
  18. int hodinyOn=22;
  19. int minutyOn=0;
  20. // Stop time
  21. int hodinyOff=6;
  22. int minutyOff=0;
  23. // Prikladne cas ted - skutecny se vezme z RTC
  24. int hodinyTed=20;
  25. int minutyTed=32;
  26.  
  27. void setup() {
  28.     Serial.begin(9600);
  29.     pinMode(ledPin, OUTPUT);      // declare LED as output
  30.     pinMode(inputPin, INPUT);     // declare sensor as input
  31.     // Initialize a new chip by turning off write protection and clearing the
  32.     // clock halt flag. These methods needn't always be called. See the DS1302
  33.     // datasheet for details.
  34.     rtc.writeProtect(false);
  35.     rtc.halt(false);
  36.  
  37.     // Make a new time object to set the date and time.
  38.     // Sunday, September 22, 2013 at 01:38:50.
  39.    //  Time t(2021, 2, 10, 16, 58, 00, Time::kMonday);
  40.  
  41.     // Set the time and date on the chip.
  42.    //  rtc.time(t);
  43. }
  44.  
  45. void loop()
  46. {
  47.    
  48.     Time t = rtc.time();
  49.     hodinyTed = t.hr;
  50.     minutyTed = t.min;
  51.        
  52.     // Prevedeme casy na minuty
  53.     int startTime = hodinyOn*60+minutyOn;
  54.     int stopTime = hodinyOff*60+minutyOff;
  55.     int casTed = hodinyTed*60+minutyTed;
  56.  
  57.     // Vyhodnocujeme, prvni tretina plati pro casy co nejsou pres pulnoc, dalsi 2/3 jsou pro casy co jdou pres pulnoc
  58.     if ((startTime < stopTime && casTed > startTime && casTed < stopTime) || (startTime > stopTime && casTed > startTime) || (startTime > stopTime && casTed < stopTime))
  59.     {
  60.         if (pirState==LOW)
  61.         {
  62.             val = digitalRead(inputPin);  // read input value
  63.             if (val==HIGH)
  64.             {
  65.                 lastOn=millis();
  66.                 pirState=HIGH;
  67.             }
  68.         }
  69.         if (pirState == HIGH)  // check if the input is HIGH
  70.         {            
  71.             digitalWrite(ledPin, HIGH);  // turn LED ON
  72.         }
  73.         else
  74.         {
  75.             digitalWrite(ledPin, LOW); // turn LED OFF
  76.         }
  77.         if (millis() > (lastOn + (2*60*1000))) pirState=LOW;    // 2*60*1000 = 2 minuty
  78.     }
  79.     delay(100);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement