Advertisement
tic

Clock-1.02

tic
Nov 23rd, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.87 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <RTClib.h>
  3. #include <TimerOne.h>
  4.  
  5. RTC_DS1307 RTC;
  6.  
  7. volatile int countM = 0;
  8. volatile int countH = 0;
  9. volatile boolean zcM = 0;
  10. volatile boolean zcH = 0;
  11. int potPin = A0;
  12. int buttonPin = A1;
  13. int handArr[] = {4, 5, 6, 11, 12, 13, 14, 15, 16, 17, 18, 19};
  14. int freqStep = 65;
  15.  
  16. int hourPin = handArr[0];
  17. int hourBrightness = 0;
  18.  
  19. int minutePin = handArr[0];
  20. int minuteBrightnessArr[] = {80, 65, 50, 35, 20};
  21. int minuteBrightness = minuteBrightnessArr[0];
  22.  
  23. int dim = 1;
  24.  
  25. void setup()
  26. {
  27.     Serial.begin(57600);
  28.  
  29.     for (int i = 0; i < sizeof(handArr); ++i)
  30.         pinMode(handArr[i], OUTPUT);
  31.  
  32.     Wire.begin();
  33.     RTC.begin();
  34.  
  35.     attachInterrupt(0, zcDetect, RISING);
  36.  
  37.     Timer1.initialize(freqStep);
  38.     Timer1.attachInterrupt(dimCheck);
  39. }
  40.  
  41. void zcDetect()
  42. {
  43.     zcM = true;
  44.     zcH = true;
  45.     for (int i = 0; i < sizeof(handArr); ++i)
  46.         digitalWrite(handArr[i], LOW);
  47. }
  48.  
  49. void dimCheck()
  50. {
  51.     //MINUTES
  52.     if (zcM)
  53.     {
  54.         if (countM >= minuteBrightness)
  55.         {
  56.             digitalWrite(minutePin, HIGH);
  57.             countM = 0;
  58.             zcM = false;
  59.         }
  60.         else
  61.             countM++;
  62.     }
  63.    
  64.     //HOURS
  65.     if (zcH)
  66.     {
  67.         if (countH >= hourBrightness)
  68.         {
  69.             digitalWrite(hourPin, HIGH);
  70.             countH = 0;
  71.             zcH = false;
  72.         }
  73.         else
  74.             countH++;
  75.     }
  76. }
  77.  
  78. void setBulbs()
  79. {
  80.     DateTime now = RTC.now();
  81.     int hour = now.hour();
  82.     int minute = now.minute();
  83.    
  84.     hourPin = handArr[hour - 1];
  85.  
  86.     if (minute == 0)
  87.         minutePin = handArr[0];
  88.     else
  89.         minutePin = handArr[(int)ceil(minute/5) - 1];
  90.        
  91.     dim = analogRead(potPin);
  92.     long tempDim = map(dim, 0, 1023, 1, 1.5);
  93.  
  94.     minuteBrightness = round(minuteBrightnessArr[(minute % 5) - 1] * tempDim);
  95.     hourBrightness = minuteBrightness - minuteBrightnessArr[4];
  96. }
  97.  
  98. void checkButtonState()
  99. {
  100.     buttonState = analogRead(buttonPin);
  101.     int stateArr[] = {80, 65, 50, 35, 20};
  102.  
  103. }
  104.  
  105. void loop()
  106. {
  107.     setBulbs();
  108.     checkButtonState();
  109.     delay(20);
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement