Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Wire.h"
- // the I2C address of Tiny RTC
- #define DS1307_I2C_ADDRESS 0x68
- // define button select time for the timer
- #define buttonSelectPin 2
- #define buttonHoursPin 3
- #define buttonMinPin 4
- #define buttonSecPin 5
- #define buttonSavePin 6
- // for the RTC
- byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
- // variable setup the timer
- int LED1 = 13;
- int Hours=0;
- int Min=0;
- int Sec=0;
- // debounce button
- long lastDebounceTime = 0; // the last time the output pin was toggled
- long debounceDelay = 228; // the debounce time; increase if the output flickers
- boolean LED1State = false;
- // boolean LED2State = false;
- //
- long buttonTimer = 0;
- long longPressTime = 500;
- boolean buttonActive = false;
- boolean longPressActive = false;
- boolean save_mode = false; // flag for the save button
- // set each second read from RTC
- unsigned long previousMillis = 0;
- const long interval = 1000;
- void setup()
- {
- Wire.begin();
- Serial.begin(19200);
- //configure pinX as an input and enable the internal pull-up resistor
- pinMode(buttonSelectPin, INPUT_PULLUP);
- pinMode(buttonHoursPin, INPUT_PULLUP);
- pinMode(buttonMinPin, INPUT_PULLUP);
- pinMode(buttonSecPin, INPUT_PULLUP);
- pinMode(buttonSavePin, INPUT_PULLUP);
- pinMode(13, OUTPUT);
- setDateDs1307(); //Set current time;
- }
- void loop()
- {
- // read from RTC each second
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= interval)
- {
- previousMillis = currentMillis;
- getDateDs1307(); //get the time data from tiny RTC
- counter_down(); // need countdown only if the time setup
- }
- read_button();
- }
- void counter_down()
- {
- // ???
- }
- void read_button()
- {
- if (digitalRead(buttonSelectPin) == LOW)
- {
- if (buttonActive == false)
- {
- buttonActive = true;
- buttonTimer = millis();
- }
- if ((millis() - buttonTimer > longPressTime) && (longPressActive == false))
- {
- // long press on the button activate the LED1
- longPressActive = true;
- LED1State = !LED1State;
- digitalWrite(LED1, LED1State);
- }
- }
- else
- {
- if (buttonActive == true)
- {
- if (longPressActive == true)
- {
- longPressActive = false;
- }
- else
- {
- // LED2State = !LED2State;
- // digitalWrite(LED2, LED2State);
- }
- buttonActive = false;
- }
- }
- // when the LED1 is ON, the long press is activate, enter the setup loop
- while(LED1State == HIGH)
- {
- setup_timer(); // set the timer, read from button
- if(save_mode == true)
- {
- save_mode = false;
- LED1State = !LED1State;
- digitalWrite(LED1, LED1State);
- break;
- }
- }
- }
- // read button with debounce
- void setup_timer()
- {
- if ( (millis() - lastDebounceTime) > debounceDelay)
- {
- if (digitalRead(buttonHoursPin) == LOW)
- {
- Hours++;
- if (Hours >= 24) Hours=0;
- Serial.println(Hours);
- lastDebounceTime = millis();
- }
- if (digitalRead(buttonMinPin) == LOW)
- {
- Min++;
- if (Min >= 60) Min=0;
- Serial.println(Min);
- lastDebounceTime = millis();
- }
- if (digitalRead(buttonSecPin) == LOW)
- {
- Sec++;
- if (Sec >= 60) Sec=0;
- Serial.println(Sec);
- lastDebounceTime = millis();
- }
- if (digitalRead(buttonSavePin) == LOW)
- {
- save_mode = true;
- }
- }
- }
- // Convert normal decimal numbers to binary coded decimal
- byte decToBcd(byte val)
- {
- return ( (val/10*16) + (val%10) );
- }
- // Convert binary coded decimal to normal decimal numbers
- byte bcdToDec(byte val)
- {
- return ( (val/16*10) + (val%16) );
- }
- // Function to set the currnt time, change the second&minute&hour to the right time
- void setDateDs1307()
- {
- second =45;
- minute = 29;
- hour = 13;
- dayOfWeek = 2;
- dayOfMonth =18;
- month =9;
- year= 12;
- Wire.beginTransmission(DS1307_I2C_ADDRESS);
- Wire.write(decToBcd(0));
- Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock
- Wire.write(decToBcd(minute));
- Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set
- // bit 6 (also need to change readDateDs1307)
- Wire.write(decToBcd(dayOfWeek));
- Wire.write(decToBcd(dayOfMonth));
- Wire.write(decToBcd(month));
- Wire.write(decToBcd(year));
- Wire.endTransmission();
- }
- // Function to gets the date and time from the ds1307 and prints result
- void getDateDs1307()
- {
- // Reset the register pointer
- Wire.beginTransmission(DS1307_I2C_ADDRESS);
- Wire.write(decToBcd(0));
- Wire.endTransmission();
- Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
- second = bcdToDec(Wire.read() & 0x7f);
- minute = bcdToDec(Wire.read());
- hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
- dayOfWeek = bcdToDec(Wire.read());
- dayOfMonth = bcdToDec(Wire.read());
- month = bcdToDec(Wire.read());
- year = bcdToDec(Wire.read());
- Serial.print(hour, DEC);
- Serial.print(":");
- Serial.print(minute, DEC);
- Serial.print(":");
- Serial.print(second, DEC);
- Serial.print(" ");
- Serial.print(month, DEC);
- Serial.print("/");
- Serial.print(dayOfMonth, DEC);
- Serial.print("/");
- Serial.print(year,DEC);
- Serial.print(" ");
- Serial.println();
- //Serial.print("Day of week:");
- }
Advertisement
Add Comment
Please, Sign In to add comment