Forum / Guestbook




Luberth Fri 17 May 2019 at 18:02:55
Arduino DS3231 One Dollar i2c RealTimeClock and Temperature

-
Arduino DS3231 One Dollar i2c RealTimeClock and Temperature battery backuped timekeeping



Maybe in summer LDR light sensor is not so nice to control fox proof chicken door
in the summer morning its very early light in Holland, maybe open door at 6:30 or 7:00 hour
in the summer evening its very long light in Holland, maybe close the door at 21:30 or 22:00 hour
maybe better to use time limits instead of LDR light sensor in summer
this battery backup real time clock is a nice cheap addon
also has an internal temperature sensor inside DS3231 chip
could be handy for ventilation or water heating control in winter if temp below zero turn on waterheater

http://84.106.2.21:8888/forum/index.php?action=view&id=255

Arduino DS3231 Based Real Time Clock with Alarm & Temperature Monitor Alex Newton — June 13, 2018

We are happy to present you Arduino DS3231 Based Real Time Clock
with Alarm & Temperature Monitor.
This is a simple real time clock with time, day, date
using Arduino UNO board and DS3231 module.
We have also added two alarm functions and temperature monitor to further extend this project.

DS3231 Tutorial - Real Time Clock with Alarm & Temperature Monitor using Arduino
https://www.how2electronics.com/arduino-ds3231-based-real-time-clock/

DS3231 Tutorial - Real Time Clock with Alarm & Temperature Monitor using Arduino
https://www.how2electronics.com/arduino-ds3231-based-real-time-clock/



Strange this one is exact the same but older posting date
Who to give credit for the example code???
and maybe a better video
Arduino + DS3231 real time clock with alarms and temperature monitor
https://youtu.be/lq0R4Yz05o4
More details at: https://simple-circuit.com/arduino-ds3231-real-time-clock-alarm-temperature/
More details at: https://simple-circuit.com/arduino-ds3231-real-time-clock-alarm-temperature/
https://youtu.be/lq0R4Yz05o4

Simple Projects
Published on Nov 18, 2017
Arduino real time clock with 2 alarm functions and temperature monitor.
More details at: https://simple-circuit.com/arduino-ds3231-real-time-clock-alarm-temperature/



it is possible to set the time from Arduino IDE of DS3231 by uploading a code
File=>Examples=>DS1307RTC=>SetTime
on my raspberry i have a 10 second time difference on DS3231 time setting
think slow programming of my arduino nano on my desktop raspberry pi cq this page's webserver=
time difference between arduino ide time get->compile/programming->program run

i used an i2c 4x20 LCD
looks like there are some different i2c LCD libs out there
INT/SQW i do not see the advantage of alarm setting via i2c in DS3231 and then interupt D2 input trigger alarmoutput
also i mistaked thought off was switch output to off when time is there
but alarmtime OFF means Not In Use
will change the code in
A1 alarmtime openchickendoor
A2 alarmtime closechickendoor
why not in the code if alarmhour=match and alarmminutes=match and outputstate!=desired do output on/off
and do it without the ds3231 alarmtime/interupt setting - handle it in arduino
Arduino Nano with ScrewTerminal + - A4 A5 i2c to DS3231 and i2c forwarded to i2c 20x4 LCD
No buttons connected
No INT/SQW to D2 connected
Power from USB

Arduino Nano with ScrewTerminal + - A4 A5 i2c to DS3231 and i2c forwarded to i2c 20x4 LCD
No buttons connected
No INT/SQW to D2 connected
Power from USB

would be nice to use a rotary encoder push button for time alarm setting
long push time setting
short push alarm setting

DS3231 info sheet
https://datasheets.maximintegrated.com/en/ds/DS3231.pdf


// http://84.106.2.21:8888/forum/index.php?<wbr />action=view&id=291
// http://84.106.2.21:8888/forum/index.php?action=view&id=255
//my version so far 18 may 2019
//i used an i2c 4x20 LCD<br>
//looks like there are some different i2c LCD libs out there<br>
// think i used https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home

//it is possible to set the time from Arduino IDE of DS3231 by uploading a code
//File=>Examples=>DS1307RTC=>SetTime

// include LCD library code
#include <LiquidCrystal_I2C.h>                     // https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home
LiquidCrystal_I2C  lcd(0x27,2,1,0,4,5,6,7);        // https://bitbucket.org/fmalpartida/new-li<wbr />quidcrystal/wiki/Home

#include <Wire.h>                                  // include Wire library code (needed for I2C protocol devices)


const int button1   =  9;                   // button1 pin number select timefield
const int button2   = 10;                   // button2 pin number change time
const int button3   = 11;                   // button3 pin number select alarmfield
const int alarm_pin = 13;                   // Alarms pin number 13 is with onboard led

void setup() {
  pinMode(9,  INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  digitalWrite(alarm_pin, LOW);
  // set up the LCD's number of columns and rows
  lcd.begin(20, 4);
  lcd.setBacklightPin(3,POSITIVE);
  lcd.setBacklight(HIGH);
  
  Wire.begin();                                 // Join i2c bus
  attachInterrupt(digitalPinToInterrupt(2)<wbr />, Alarm, FALLING);
}

// Variables declaration
bool alarm1_status, alarm2_status;
char Time[]     = "  :  :  ",
     calendar[] = "      /  /20  ",
     alarm1[]   = "A1:   :  :00", alarm2[]   = "A2:   :  :00",
     temperature[] = "     .   C";             // temperature[] = "T:   .   C"; // removed T: luberth
   
byte  i, second, minute, hour, day, date, month, year,
      alarm1_minute, alarm1_hour, alarm2_minute, alarm2_hour,
      status_reg;

void Alarm(){
  digitalWrite(alarm_pin, HIGH);
}
void DS3231_read(){                             // Function to read time & calendar data
  Wire.beginTransmission(0x68);                 // Start I2C protocol with DS3231 address
  Wire.write(0);                                // Send register address
  Wire.endTransmission(false);                  // I2C restart
  Wire.requestFrom(0x68, 7);                    // Request 7 bytes from DS3231 and release I2C bus at end of reading
  second = Wire.read();                         // Read seconds from register 0
  minute = Wire.read();                         // Read minuts from register 1
  hour   = Wire.read();                         // Read hour from register 2
  day    = Wire.read();                         // Read day from register 3
  date   = Wire.read();                         // Read date from register 4
  month  = Wire.read();                         // Read month from register 5
  year   = Wire.read();                         // Read year from register 6
}
void alarms_read_display(){                     // Function to read and display alarm1, alarm2 and temperature data
  byte control_reg, temperature_lsb;
  char temperature_msb;
  Wire.beginTransmission(0x68);                 // Start I2C protocol with DS3231 address
  Wire.write(0x08);                             // Send register address
  Wire.endTransmission(false);                  // I2C restart
  Wire.requestFrom(0x68, 11);                   // Request 11 bytes from DS3231 and release I2C bus at end of reading
  alarm1_minute = Wire.read();                  // Read alarm1 minutes
  alarm1_hour   = Wire.read();                  // Read alarm1 hours
  Wire.read();                                  // Skip alarm1 day/date register
  alarm2_minute = Wire.read();                  // Read alarm2 minutes
  alarm2_hour   = Wire.read();                  // Read alarm2 hours
  Wire.read();                                  // Skip alarm2 day/date register
  control_reg = Wire.read();                    // Read the DS3231 control register
  status_reg  = Wire.read();                    // Read the DS3231 status register
  Wire.read();                                  // Skip aging offset register
  temperature_msb = Wire.read();                // Read temperature MSB
  temperature_lsb = Wire.read();                // Read temperature LSB
    // Convert BCD to decimal
  alarm1_minute = (alarm1_minute >> 4) * 10 + (alarm1_minute & 0x0F);
  alarm1_hour   = (alarm1_hour   >> 4) * 10 + (alarm1_hour & 0x0F);
  alarm2_minute = (alarm2_minute >> 4) * 10 + (alarm2_minute & 0x0F);
  alarm2_hour   = (alarm2_hour   >> 4) * 10 + (alarm2_hour & 0x0F);
    // End conversion
  alarm1[8]     = alarm1_minute % 10  + 48;
  alarm1[7]     = alarm1_minute / 10  + 48;
  alarm1[5]     = alarm1_hour   % 10  + 48;
  alarm1[4]     = alarm1_hour   / 10  + 48;
  alarm2[8]     = alarm2_minute % 10  + 48;
  alarm2[7]     = alarm2_minute / 10  + 48;
  alarm2[5]     = alarm2_hour   % 10  + 48;
  alarm2[4]     = alarm2_hour   / 10  + 48;
  alarm1_status = bitRead(control_reg, 0);      // Read alarm1 interrupt enable bit (A1IE) from DS3231 control register
  alarm2_status = bitRead(control_reg, 1);      // Read alarm2 interrupt enable bit (A2IE) from DS3231 control register
  if(temperature_msb < 0){
    temperature_msb = abs(temperature_msb);
    temperature[2] = '-';
  }
  else
    temperature[2] = ' ';
  temperature_lsb >>= 6;
  temperature[4] = temperature_msb % 10  + 48;
  temperature[3] = temperature_msb / 10  + 48;
  if(temperature_lsb == 0 || temperature_lsb == 2){
    temperature[7] = '0';
    if(temperature_lsb == 0) temperature[6] = '0';
    else                     temperature[6] = '5';
  }
  if(temperature_lsb == 1 || temperature_lsb == 3){
    temperature[7] = '5';
    if(temperature_lsb == 1) temperature[6] = '2';
    else                     temperature[6] = '7';
  }
  temperature[8]  = 223;                        // Put the degree symbol
  lcd.setCursor(10, 0);
  lcd.print(temperature);                       // Display temperature
  lcd.setCursor(0, 2);
  lcd.print(alarm1);                            // Display alarm1
  lcd.setCursor(17, 2);
  if(alarm1_status)  lcd.print("ON ");          // If A1IE = 1 print 'ON'
  else               lcd.print("OFF");          // If A1IE = 0 print 'OFF'
  lcd.setCursor(0, 3);
  lcd.print(alarm2);                            // Display alarm2
  lcd.setCursor(17, 3);
  if(alarm2_status)  lcd.print("ON ");          // If A2IE = 1 print 'ON'
  else               lcd.print("OFF");          // If A2IE = 0 print 'OFF'
}
void calendar_display(){                        // Function to display calendar
  switch(day){
    case 1:  strcpy(calendar, "Sun   /  /20  "); break;
    case 2:  strcpy(calendar, "Mon   /  /20  "); break;
    case 3:  strcpy(calendar, "Tue   /  /20  "); break;
    case 4:  strcpy(calendar, "Wed   /  /20  "); break;
    case 5:  strcpy(calendar, "Thu   /  /20  "); break;
    case 6:  strcpy(calendar, "Fri   /  /20  "); break;
    case 7:  strcpy(calendar, "Sat   /  /20  "); break;
    default: strcpy(calendar, "Sat   /  /20  ");
  }
  calendar[13] = year  % 10 + 48;
  calendar[12] = year  / 10 + 48;
  calendar[8]  = month % 10 + 48;
  calendar[7]  = month / 10 + 48;
  calendar[5]  = date  % 10 + 48;
  calendar[4]  = date  / 10 + 48;
  lcd.setCursor(0, 1);
  lcd.print(calendar);                          // Display calendar
}
void DS3231_display(){
  // Convert BCD to decimal
  second = (second >> 4) * 10 + (second & 0x0F);
  minute = (minute >> 4) * 10 + (minute & 0x0F);
  hour = (hour >> 4) * 10 + (hour & 0x0F);
  date = (date >> 4) * 10 + (date & 0x0F);
  month = (month >> 4) * 10 + (month & 0x0F);
  year = (year >> 4) * 10 + (year & 0x0F);
  // End conversion
  Time[7]     = second % 10  + 48;
  Time[6]     = second / 10  + 48;
  Time[4]     = minute % 10  + 48;
  Time[3]     = minute / 10  + 48;
  Time[1]     = hour   % 10  + 48;
  Time[0]     = hour   / 10  + 48;
  calendar_display();                           // Call calendar display function
  lcd.setCursor(0, 0);
  lcd.print(Time);                              // Display time
}
void Blink(){
  byte j = 0;
  while(j < 10 && (digitalRead(button1) || i >= 5) && digitalRead(button2) && (digitalRead(button3) || i < 5)){
    j++;
    delay(25);
  }
}
byte edit(byte x, byte y, byte parameter){
  char text[3];
  while(!digitalRead(button1) || !digitalRead(button3));    // Wait until button B1 is released
  while(true){
    while(!digitalRead(button2)){                           // If button B2 is pressed
      parameter++;
      if(((i == 0) || (i == 5)) && parameter > 23)          // If hours > 23 ==> hours = 0
        parameter = 0;
      if(((i == 1) || (i == 6)) && parameter > 59)          // If minutes > 59 ==> minutes = 0
        parameter = 0;
      if(i == 2 && parameter > 31)                          // If date > 31 ==> date = 1
        parameter = 1;
      if(i == 3 && parameter > 12)                          // If month > 12 ==> month = 1
        parameter = 1;
      if(i == 4 && parameter > 99)                          // If year > 99 ==> year = 0
        parameter = 0;
      if(i == 7 && parameter > 1)                           // For alarms ON or OFF (1: alarm ON, 0: alarm OFF)
        parameter = 0;
      lcd.setCursor(x, y);
      if(i == 7){                                           // For alarms ON & OFF
        if(parameter == 1)  lcd.print("ON ");
        else                lcd.print("OFF");
      }
      else{
        sprintf(text,"%02u", parameter);
        lcd.print(text);
      }
      if(i >= 5){
        DS3231_read();                          // Read data from DS3231
        DS3231_display();                       // Display DS3231 time and calendar
      }
      delay(200);                               // Wait 200ms
    }
    lcd.setCursor(x, y);
    lcd.print("  ");                            // Print two spaces
    if(i == 7) lcd.print(" ");                  // Print space (for alarms ON & OFF)
    Blink();                                    // Call Blink function
    lcd.setCursor(x, y);
    if(i == 7){                                 // For alarms ON & OFF
      if(parameter == 1)  lcd.print("ON ");
      else                lcd.print("OFF");
    }
    else{
      sprintf(text,"%02u", parameter);
      lcd.print(text);
    }
    Blink();
    if(i >= 5){
      DS3231_read();
      DS3231_display();}
    if((!digitalRead(button1) && i < 5) || (!digitalRead(button3) && i >= 5)){
      i++;                                      // Increment 'i' for the next parameter
      return parameter;                         // Return parameter value and exit
    }
  }
}

void loop() {
  if(!digitalRead(button1)){                    // If B1 button is pressed
      i = 0;
      hour   = edit(0, 0, hour);
      minute = edit(3, 0, minute);
      while(!digitalRead(button1));             // Wait until button B1 released
      while(true){
        while(!digitalRead(button2)){           // If button B2 button is pressed
          day++;                                // Increment day
          if(day > 7) day = 1;
          calendar_display();                   // Call display_calendar function
          lcd.setCursor(0, 1);
          lcd.print(calendar);                  // Display calendar
          delay(200);
        }
        lcd.setCursor(0, 1);
        lcd.print("   ");                       // Print 3 spaces
        Blink();
        lcd.setCursor(0, 1);
        lcd.print(calendar);                    // Print calendar
        Blink();                                // Call Blink function
        if(!digitalRead(button1))               // If button B1 is pressed
          break;
      }
      date = edit(4, 1, date);                  // Edit date
      month = edit(7, 1, month);                // Edit month
      year = edit(12, 1, year);                 // Edit year
      // Convert decimal to BCD
      minute = ((minute / 10) << 4) + (minute % 10);
      hour = ((hour / 10) << 4) + (hour % 10);
      date = ((date / 10) << 4) + (date % 10);
      month = ((month / 10) << 4) + (month % 10);
      year = ((year / 10) << 4) + (year % 10);
      // End conversion
      // Write time & calendar data to DS3231 RTC
      Wire.beginTransmission(0x68);             // Start I2C protocol with DS3231 address
      Wire.write(0);                            // Send register address
      Wire.write(0);                            // Reset sesonds and start oscillator
      Wire.write(minute);                       // Write minute
      Wire.write(hour);                         // Write hour
      Wire.write(day);                          // Write day
      Wire.write(date);                         // Write date
      Wire.write(month);                        // Write month
      Wire.write(year);                         // Write year
      Wire.endTransmission();                   // Stop transmission and release the I2C bus
      delay(200);
    }
    if(!digitalRead(button3)){                  // If B3 button is pressed
      while(!digitalRead(button3));             // Wait until button B3 released
      i = 5;
      alarm1_hour   = edit(4,  2, alarm1_hour);
      alarm1_minute = edit(7,  2, alarm1_minute);
      alarm1_status = edit(17, 2, alarm1_status);
      i = 5;
      alarm2_hour   = edit(4,  3, alarm2_hour);
      alarm2_minute = edit(7,  3, alarm2_minute);
      alarm2_status = edit(17, 3, alarm2_status);
      alarm1_minute = ((alarm1_minute / 10) << 4) + (alarm1_minute % 10);
      alarm1_hour   = ((alarm1_hour   / 10) << 4) + (alarm1_hour % 10);
      alarm2_minute = ((alarm2_minute / 10) << 4) + (alarm2_minute % 10);
      alarm2_hour   = ((alarm2_hour   / 10) << 4) + (alarm2_hour % 10);
      // Write alarms data to DS3231
      Wire.beginTransmission(0x68);               // Start I2C protocol with DS3231 address
      Wire.write(7);                              // Send register address (alarm1 seconds)
      Wire.write(0);                              // Write 0 to alarm1 seconds
      Wire.write(alarm1_minute);                  // Write alarm1 minutes value to DS3231
      Wire.write(alarm1_hour);                    // Write alarm1 hours value to DS3231
      Wire.write(0x80);                           // Alarm1 when hours, minutes, and seconds match
      Wire.write(alarm2_minute);                  // Write alarm2 minutes value to DS3231
      Wire.write(alarm2_hour);                    // Write alarm2 hours value to DS3231
      Wire.write(0x80);                           // Alarm2 when hours and minutes match
      Wire.write(4 | alarm1_status | (alarm2_status << 1));      // Write data to DS3231 control register (enable interrupt when alarm)
      Wire.write(0);                              // Clear alarm flag bits
      Wire.endTransmission();                     // Stop transmission and release the I2C bus
      delay(200);                                 // Wait 200ms
    }
    if(!digitalRead(button2) && digitalRead(alarm_pin)){         // When button B2 pressed with alarm (Reset and turn OFF the alarm)
      digitalWrite(alarm_pin, LOW);               // Turn OFF the alarm indicator
      Wire.beginTransmission(0x68);               // Start I2C protocol with DS3231 address
      Wire.write(0x0E);                           // Send register address (control register)
      // Write data to control register (Turn OFF the occurred alarm and keep the other as it is)
      Wire.write(4 | (!bitRead(status_reg, 0) & alarm1_status) | ((!bitRead(status_reg, 1) & alarm2_status) << 1));
      Wire.write(0);                              // Clear alarm flag bits
      Wire.endTransmission();                     // Stop transmission and release the I2C bus
    }
    DS3231_read();                                // Read time and calendar parameters from DS3231 RTC
    alarms_read_display();                        // Read and display alarms parameters
    DS3231_display();                             // Display time & calendar
    delay(50);                                    // Wait 50ms
}



















How to set DS3231 Real time clock module for Arduino
https://youtu.be/Z-66x-9-7T0

https://youtu.be/Z-66x-9-7T0
Link download library
https://github.com/JChristensen/DS3232RTC

what is the 24c32 doing on the DS3231 PrintCircuitBoard???
i2c eeprom
Additionally the RTC is often found paired with an I2C EEPROM (usually AT24C32) which can be used for convenient data-logging purposes.
https://github.com/sleemanj/DS3231_Simple
24C32 is a 4K x 8 (32K bit) Serial Electrically Erasable PROM.
ok thats a separate i2c eeprom free to use memory at i2c adres 0x57

code to write and read the i2c 24c32

//https://lastminuteengineers.com/ds32<wbr />31-rtc-arduino-tutorial/
#include <Wire.h>

void setup()
{
    char somedata[] = "lastminuteengineers.com"; // data to write
    Wire.begin(); // initialise the connection
    Serial.begin(9600);
    Serial.println("Writing into memory...");
	
	// write to EEPROM
    i2c_eeprom_write_page(0x57, 0, (byte *)somedata, sizeof(somedata));

    delay(100); //add a small delay
    Serial.println("Memory written");
}

void loop()
{
    Serial.print("Reading memory: ");
    int addr=0; //first address
	
	// access the first address from the memory
    byte b = i2c_eeprom_read_byte(0x57, 0);

    while (b!=0)
    {
        Serial.print((char)b); //print content to serial port
        addr++; //increase address
        b = i2c_eeprom_read_byte(0x57, addr); //access an address from the memory
    }
    Serial.println(" ");
    delay(2000);
}

void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
    int rdata = data;
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddress >> 8)); // MSB
    Wire.write((int)(eeaddress & 0xFF)); // LSB
    Wire.write(rdata);
    Wire.endTransmission();
}

// WARNING: address is a page address, 6-bit end will wrap around
// also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddresspage >> 8)); // MSB
    Wire.write((int)(eeaddresspage & 0xFF)); // LSB
    byte c;
    for ( c = 0; c < length; c++)
        Wire.write(data[c]);
    Wire.endTransmission();
}

byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
    byte rdata = 0xFF;
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddress >> 8)); // MSB
    Wire.write((int)(eeaddress & 0xFF)); // LSB
    Wire.endTransmission();
    Wire.requestFrom(deviceaddress,1);
    if (Wire.available()) rdata = Wire.read();
    return rdata;
}

// maybe let's not read more than 30 or 32 bytes at a time!
void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
    Wire.beginTransmission(deviceaddress);
    Wire.write((int)(eeaddress >> 8)); // MSB
    Wire.write((int)(eeaddress & 0xFF)); // LSB
    Wire.endTransmission();
    Wire.requestFrom(deviceaddress,length);
<wbr />
    int c = 0;
    for ( c = 0; c < length; c++ )
        if (Wire.available()) buffer[c] = Wire.read();
}
//https://lastminuteengineers.com/ds3231-rtc-arduino-tutorial/







Arduino Rotary Encoder Clock
https://youtu.be/HsTXarwsVWQ
Arduino Nano + DS3231 + 1602 LCD + piezoelectric buzzer + rotary encoder.
https://github.com/faowff034faerf3490ur/arduino_rotary_clock

https://github.com/faowff034faerf3490ur/arduino_rotary_clock











simple rotary encoder code print val on serial monitor

/*     Arduino Rotary Encoder Tutorial
 *      
 *  by Dejan Nedelkovski, www.HowToMechatronics.com
 *  changes
 *  http://84.106.2.21:8888/forum/index.php?action=view&id=291
 */
 
 #define Encoder_A 6
 #define Encoder_B 7
 int counter = 0; 
 int CurrentState;
 int LastState;  
 
 
 void setup() { 
   pinMode (Encoder_A,INPUT);
   pinMode (Encoder_B,INPUT);
   
   Serial.begin (9600);
   LastState = digitalRead(Encoder_A);   
 } 

 
 void loop() { 
   CurrentState = digitalRead(Encoder_A); // Reads the "current" state of the Encoder_A
   // If the previous and the current state of the Encoder_A are different, that means a Pulse has occured
   if (CurrentState != LastState){     
     // If the Encoder_B state is different to the Encoder_A state, that means the encoder is rotating clockwise
     if (digitalRead(Encoder_B) != CurrentState) { 
       counter ++;
     } else {
       counter --;
     }
  
     Serial.println(counter);
   } 
   LastState = CurrentState; // Updates the previous state of the outputA with the current state
 }






Arduino RailRoad FastClock nice rotary encoder use
https://github.com/Futski-III/Arduino-Fast-Clock

https://github.com/Futski-III/Arduino-Fast-Clock









https://www.instructables.com/id/Automatic-Chicken-Coop-Door-Arduino-Controlled/
like this one do an action on actual current minutes off day
convert actual time to minutes and check that with alarmsetting
changed a bit of his code
open chickendoor at 6:30 close chickendoor command at 21:00
hardcoded times only prints to serial monitor
just an example arduino on/off clock open/close clock timed actions clock

JavaScript actual time in minutes


JavaScript actual time in minutes


/* 
 *  Automatic Chicken Coop Door - Arduino Controlled. by rscholten
 *  
 *  from Hentry main file. April 2018
 *  https://www.instructables.com/id/Automat<wbr />ic-Chicken-Coop-Door-Arduino-Controlled/<wbr />
 *  
 *  parts bits of code  reused
 *  http://84.106.2.21:8888/forum/index.php?<wbr />action=view&id=291
 *  http://84.106.2.21:8888/forum/index.php?action=view&id=255
*/

#include <Wire.h>           
#include <DS3232RTC.h>                        // https://github.com/JChristensen/DS3232RTC

int current_time_in_minutes = 0 ;            // 24hoursx60minutes = 0 to 1440 minutes of day
        
void setup() {
  Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  Wire.begin();
}


void loop() {    



int opentime = (6*60+30);                  // door opentime in minutes 6:30     (6hours x 60minutes + 30minutes)
int closetime = (21*60);                   // door closetime in minutes 21:00   (21hours x 60minutes)

current_time_in_minutes = hour() * 60 + minute();        // read time, convert to minutes of day (0 to 1440)


if ((current_time_in_minutes >= opentime) && (current_time_in_minutes <= closetime)) {                // greater as 6:30 = open
    //door_open();
    Serial.println("Door is open ( Time > OpenTime)");
    
}

if (current_time_in_minutes <= opentime){                // smaller as 6:30 = close
    //door_close();
    Serial.println("Door is Closed ( Time < OpenTime )");
}

if (current_time_in_minutes >= closetime){              // greater as 21:00 = close 
    //door_close();
    Serial.println("Door is Closed ( Time > CloseTime )");
}


Serial.print("Current Hour ");
Serial.println(hour());
Serial.pri<wbr />nt("Current Minute ");
Serial.println(minute());
Serial.p<wbr />rint("Current time in minutes ");
Serial.println(current_time_in_minu<wbr />tes);
Serial.print("OpenTime in Minutes ");
Serial.println(opentime);
Serial.p<wbr />rint("ClosTime in Minutes ");
Serial.println(closetime);
Serial.<wbr />println("-");
delay(5000);

}








or seconds off day
convert actual time in seconds and check alarmtime switch action on off time with that

JavaScript actual time in seconds


JavaScript actual time in seconds


/* 
 *  Automatic Chicken Coop Door - Arduino Controlled. by rscholten
 *  
 *  from Hentry main file. April 2018
 *  https://www.instructables.com/id/Automat<wbr />ic-Chicken-Coop-Door-Arduino-Controlled/<wbr />
 *  
 *  parts bits of code  reused
 *  http://84.106.2.21:8888/forum/index.php?<wbr />action=view&id=291
 *  http://84.106.2.21:8888/forum/index.php?action=view&id=255
*/

#include <Wire.h>           
#include <DS3232RTC.h>                        // https://github.com/JChristensen/DS3232RTC

long current_time_in_seconds = 0L ;            // 24hoursx60minutesx60seconds = 0 to 86400 seconds of day
long opentime = 0L;                  // door opentime in seconds 6:30     (6hours x 60minutes + 30minutes x 60seconds)
long closetime = 0L;   

        
void setup() {
  Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  Wire.begin();
}


void loop() {    



opentime = ((6*60 + 30)*60L);                  // door opentime in seconds 6:30     (6hours x 60minutes + 30minutes x 60seconds)
closetime = (21*60*60L);                   // door closetime in seconds 21:00   (21hours x 60minutes x 60seconds)
        
        // hmmm why is 21x60x60 10064 something and not 75600
        // Arduino reference says
        // If doing math with integers, 
        // at least one of the numbers must be followed by an L, 
        // forcing it to be a long

current_time_in_seconds = (hour() * 60 + minute()) * 60L + second();        // read time, convert to seconds of day (0 to 86400)


if ((current_time_in_seconds >= opentime) && (current_time_in_seconds <= closetime)) {               // greater as 6:30 = open
    //door_open();
    Serial.print ("Door is open ( Time > OpenTime) ");
    Serial.print (current_time_in_seconds);
    Serial.print (" >= ");
    Serial.println (opentime);
}

if (current_time_in_seconds <= opentime){                // smaller as 6:30 = close
    //door_close();
    Serial.print ("Door is Closed ( Time < OpenTime ) ");
    Serial.print (current_time_in_seconds);
    Serial.print (" <= ");
    Serial.println (opentime);
}

if (current_time_in_seconds >= closetime){              // greater as 21:00 = close 
    //door_close();
    Serial.print ("Door is Closed ( Time > CloseTime ) ");
    Serial.print (current_time_in_seconds);
    Serial.print (" >= ");
    Serial.println (closetime);
}


Serial.print("Curre<wbr />nt Hour ");
Serial.println(hour());
Serial.pri<wbr />nt("Current Minute ");
Serial.println(minute());
Serial.p<wbr />rint("Current Seconds ");
Serial.println(second());
Serial.p<wbr />rint("Current time in seconds ");
Serial.println(current_time_in_seco<wbr />nds);
Serial.print("OpenTime in Seconds ");
Serial.println(opentime);
Serial.p<wbr />rint("CloseTime in Seconds ");
Serial.println(closetime);
Serial.<wbr />println("-");
delay(5000);

}





terminal output


-
Door is open ( Time > OpenTime) 33819 >= 23400
Current Hour 9
Current Minute 23
Current Seconds 39
Current time in seconds 33819
OpenTime in Seconds 23400
CloseTime in Seconds 75600
-
Door is open ( Time > OpenTime) 33824 >= 23400
Current Hour 9
Current Minute 23
Current Seconds 44
Current time in seconds 33824
OpenTime in Seconds 23400
CloseTime in Seconds 75600
-
Door is open ( Time > OpenTime) 33829 >= 23400
Current Hour 9
Current Minute 23
Current Seconds 49
Current time in seconds 33829
OpenTime in Seconds 23400
CloseTime in Seconds 75600
-




















Please Let me Sleep


End of Message, Thanks For Viewing! ☺
Tip: if your watching on your phone or tablet ⁝ add / save to homescreen and you get a nice icon its Free!
Tip: on your computer or laptop do not forget to bookmark this page its Free!
BookMark ShortCut Keys press ctrl+d on your keyboard




-

-

Reply to this Message







POLL / VOTE
Did you like this Page id=291 ?

NO:         YES:




   


Design a Bit like ForumOne 4.0 WayBack 1999 ©
Always Read the Small print?!



http://forum.luberth.com - - http://www.luberth.com/forum/

NO COOKIES !?

thanks for your support
Thank You!




Co2 FootPrint WebServer



                         My Cheap Chinese Friend Ali     
Copyright Luberth Dijkman 2018 ... 2019




GoodBye Galaxy!