Advertisement
andrewb

clock.cpp

Nov 24th, 2014
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. // Numbers for the display found at the following address:
  2. // https://lb-community.s3.amazonaws.com/uploads/supporting_file/asset/457/Dice.ino
  3. int numbers [] = {0,3,6,8,10,13,15,18,20,23,26,28,30,33,35,38,40,43,45,48,50,53,55,58,61,
  4.                   64,66,69,71,74,76,78,81,84,86,89,91,94,96,99,102,104,106,108,111,114,116,
  5.                   119,121,124,127,129,132,134,136,139,141,143,146,149,151,154,156,159,160,163,
  6.                   166,169,171,173,176,179,181,184,186,188,190,193,196,199,202,205,208,210,212,
  7.                   214,217,218,221,224,227,230,232,235,237,239,242,245,247,252,255};
  8. // Time count - 1000milli = 1sec
  9. const unsigned long MLEN = 1000;
  10. const unsigned int MAX_HOUR = 24;
  11. const unsigned int MAX_MINS = 60;
  12. const unsigned int MAX_SECS = 60;
  13. // output
  14. int digo = 1;
  15. int mo = 5;
  16. int so = 9;
  17. // input
  18. int digi = 0;
  19. int mi = A0;
  20. int si = A1;
  21. // Constants for measuring the button state
  22. const int BDOWN = 1;
  23. const int BUP = 0;
  24. const int MODEINP = 1;
  25. const int MODECLK = -1;
  26. // The variables that handle the state of the LED
  27. int btnState = 0;
  28. int lstState = 0;
  29. // The state of the clock
  30. int CLOCK_STATE = -1;
  31. // Clock times
  32. unsigned long baset = 0;
  33. unsigned long start = 0;
  34. int the_hr = 0;
  35. int the_mn = 0;
  36.  
  37. // functions
  38. unsigned long setBaseT() {
  39.   return millis() + (MAX_SECS * MLEN);
  40. }
  41.  
  42. // Start up and set the pin modes
  43. void setup() {
  44.   // Input
  45.   pinMode(digi, INPUT);
  46.   pinMode(mi, INPUT);
  47.   pinMode(si, INPUT);
  48.   // 7-segment display
  49.   pinMode(digo, OUTPUT);
  50.   pinMode(mo, OUTPUT);
  51.   pinMode(so, OUTPUT);
  52.   // Set the default
  53.   baset = setBaseT();
  54. }
  55.  
  56. // Main loop
  57. void loop() {
  58.   if (CLOCK_STATE == -1) {
  59.     // Update time
  60.     if (millis() > baset) {
  61.       the_mn += 1;
  62.       if (the_mn == MAX_MINS) {
  63.         the_mn = 0;
  64.         the_hr += 1;
  65.        
  66.         if (the_hr == MAX_HOUR) {
  67.           the_hr = 0;
  68.         }
  69.       }
  70.       // Update the display
  71.       analogWrite(mo, numbers[the_hr]);
  72.       analogWrite(so, numbers[the_mn]);
  73.       // Update baset
  74.       baset = setBaseT();
  75.       // Short delay
  76.       delay(50);
  77.     } else { // when millis() rolls over
  78.       if (millis() < start) {
  79.         baset = setBaseT();
  80.       }
  81.     }    
  82.   } else {
  83.     // Get analog input
  84.     int ha = analogRead(mi);
  85.     int ma = analogRead(si);
  86.     // Map it to the range
  87.     the_hr = map(ha, 0, 1020, 0, 23);
  88.     the_mn = map(ma, 0, 1020, 0, 59);
  89.     // Write it out to the display
  90.     analogWrite(mo, numbers[the_hr]);
  91.     analogWrite(so, numbers[the_mn]);
  92.     delay(100);
  93.   }
  94.  
  95.   // Read the input toward the end
  96.   btnState = digitalRead(digi);
  97.  
  98.   // Change state last
  99.   if (btnState == BDOWN) {
  100.     if (lstState == BUP) {
  101.       CLOCK_STATE *= -1;
  102.       lstState = BDOWN;
  103.       // Short delay for the humans
  104.       delay(50);
  105.       if (CLOCK_STATE == 1) {
  106.         digitalWrite(digo, HIGH);
  107.       } else {
  108.         digitalWrite(digo, LOW);
  109.         baset = setBaseT();
  110.         start = millis();
  111.       }
  112.     }
  113.   }
  114.   else {
  115.     lstState = BUP;
  116.   }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement