noam76

Cowntdown Timer

Dec 20th, 2016
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.04 KB | None | 0 0
  1. #include "Wire.h"
  2.  
  3. // the I2C address of Tiny RTC
  4. #define DS1307_I2C_ADDRESS 0x68
  5.  
  6. // define button select time for the timer
  7. #define buttonSelectPin 2
  8. #define buttonHoursPin 3
  9. #define buttonMinPin 4
  10. #define buttonSecPin 5
  11. #define buttonSavePin 6
  12.  
  13. // for the RTC
  14. byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  15.  
  16. // variable setup the timer
  17. int LED1 = 13;
  18. int Hours=0;
  19. int Min=0;
  20. int Sec=0;
  21.  
  22. // debounce button
  23. long lastDebounceTime = 0;  // the last time the output pin was toggled
  24. long debounceDelay = 228;    // the debounce time; increase if the output flickers
  25.  
  26. boolean LED1State = false;
  27. // boolean LED2State = false;
  28.  
  29. //
  30. long buttonTimer = 0;
  31. long longPressTime = 500;
  32. boolean buttonActive = false;
  33. boolean longPressActive = false;
  34.  
  35. boolean save_mode = false; // flag for the save button
  36.  
  37. // set each second read from RTC
  38. unsigned long previousMillis = 0;
  39. const long interval = 1000;
  40.  
  41. void setup()
  42. {
  43. Wire.begin();
  44. Serial.begin(19200);
  45.  
  46. //configure pinX as an input and enable the internal pull-up resistor
  47. pinMode(buttonSelectPin, INPUT_PULLUP);
  48. pinMode(buttonHoursPin, INPUT_PULLUP);
  49. pinMode(buttonMinPin, INPUT_PULLUP);
  50. pinMode(buttonSecPin, INPUT_PULLUP);
  51. pinMode(buttonSavePin, INPUT_PULLUP);
  52. pinMode(13, OUTPUT);
  53. setDateDs1307(); //Set current time;
  54. }
  55.  
  56. void loop()
  57. {
  58.  // read from RTC each second
  59.  unsigned long currentMillis = millis();
  60.  if (currentMillis - previousMillis >= interval)
  61.   {
  62.    previousMillis = currentMillis;
  63.    getDateDs1307(); //get the time data from tiny RTC
  64.  
  65.    counter_down(); // need countdown only if the time setup
  66.   }
  67.   read_button();
  68. }
  69.  
  70. void counter_down()
  71. {
  72.  // ???
  73. }
  74.  
  75. void read_button()
  76. {
  77.  if (digitalRead(buttonSelectPin) == LOW)
  78.  {
  79.   if (buttonActive == false)
  80.   {
  81.    buttonActive = true;
  82.    buttonTimer = millis();
  83.   }
  84.   if ((millis() - buttonTimer > longPressTime) && (longPressActive == false))
  85.   {
  86.     // long press on the button activate the LED1
  87.    longPressActive = true;
  88.    LED1State = !LED1State;
  89.    digitalWrite(LED1, LED1State);
  90.   }
  91.  }
  92.   else
  93.   {
  94.    if (buttonActive == true)
  95.    {
  96.     if (longPressActive == true)
  97.     {
  98.      longPressActive = false;
  99.     }
  100.     else
  101.     {
  102.      // LED2State = !LED2State;
  103.      // digitalWrite(LED2, LED2State);
  104.     }  
  105.      buttonActive = false;
  106.    }
  107.   }  
  108.    // when the LED1 is ON, the long press is activate, enter the setup loop
  109.   while(LED1State == HIGH)
  110.   {
  111.    setup_timer(); // set the timer, read from button
  112.    if(save_mode == true)
  113.    {
  114.     save_mode = false;
  115.     LED1State = !LED1State;
  116.     digitalWrite(LED1, LED1State);
  117.     break;
  118.    }
  119.   }
  120. }
  121.  
  122. // read button with debounce
  123. void setup_timer()
  124. {
  125.  if ( (millis() - lastDebounceTime) > debounceDelay)
  126.  {
  127.   if (digitalRead(buttonHoursPin) == LOW)
  128.   {
  129.    Hours++;
  130.    if (Hours >= 24) Hours=0;
  131.    Serial.println(Hours);
  132.    lastDebounceTime = millis();
  133.   }
  134.   if (digitalRead(buttonMinPin) == LOW)
  135.   {
  136.    Min++;
  137.    if (Min >= 60) Min=0;
  138.    Serial.println(Min);
  139.    lastDebounceTime = millis();
  140.   }
  141.   if (digitalRead(buttonSecPin) == LOW)
  142.   {
  143.    Sec++;
  144.    if (Sec >= 60) Sec=0;
  145.    Serial.println(Sec);
  146.    lastDebounceTime = millis();
  147.   }
  148.   if (digitalRead(buttonSavePin) == LOW)
  149.   {
  150.    save_mode = true;
  151.   }
  152.  }
  153. }
  154.  
  155. // Convert normal decimal numbers to binary coded decimal
  156. byte decToBcd(byte val)
  157. {
  158.  return ( (val/10*16) + (val%10) );
  159. }
  160. // Convert binary coded decimal to normal decimal numbers
  161. byte bcdToDec(byte val)
  162. {
  163.  return ( (val/16*10) + (val%16) );
  164. }
  165. // Function to set the currnt time, change the second&minute&hour to the right time
  166. void setDateDs1307()
  167. {
  168.  second =45;
  169.  minute = 29;
  170.  hour = 13;
  171.  dayOfWeek = 2;
  172.  dayOfMonth =18;
  173.  month =9;
  174.  year= 12;
  175.  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  176.  Wire.write(decToBcd(0));
  177.  Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock
  178.  Wire.write(decToBcd(minute));
  179.  Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set
  180.  // bit 6 (also need to change readDateDs1307)
  181.  Wire.write(decToBcd(dayOfWeek));
  182.  Wire.write(decToBcd(dayOfMonth));
  183.  Wire.write(decToBcd(month));
  184.  Wire.write(decToBcd(year));
  185.  Wire.endTransmission();
  186. }
  187. // Function to gets the date and time from the ds1307 and prints result
  188. void getDateDs1307()
  189. {
  190.  // Reset the register pointer
  191.  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  192.  Wire.write(decToBcd(0));
  193.  Wire.endTransmission();
  194.  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
  195.  second = bcdToDec(Wire.read() & 0x7f);
  196.  minute = bcdToDec(Wire.read());
  197.  hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
  198.  dayOfWeek = bcdToDec(Wire.read());
  199.  dayOfMonth = bcdToDec(Wire.read());
  200.  month = bcdToDec(Wire.read());
  201.  year = bcdToDec(Wire.read());
  202.   Serial.print(hour, DEC);
  203.  Serial.print(":");
  204.  Serial.print(minute, DEC);
  205.  Serial.print(":");
  206.  Serial.print(second, DEC);
  207.  Serial.print(" ");
  208.  Serial.print(month, DEC);
  209.  Serial.print("/");
  210.  Serial.print(dayOfMonth, DEC);
  211.  Serial.print("/");
  212.  Serial.print(year,DEC);
  213.  Serial.print(" ");
  214.  Serial.println();
  215.  //Serial.print("Day of week:");
  216. }
Advertisement
Add Comment
Please, Sign In to add comment