Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By sprinkfitter
#72473 The following is the code for the RTC that works with my relays
Code: Select all#include <Wire.h>

#define SDA_PIN 21
#define SCL_PIN 22
#define DS3231_I2C_ADDRESS 0x68


const int relay1 = 2; //Digital pin that the Relay is connected
const int relay2 = 5; //Digital pin that the Relay is connected
const int OnHour1 = 10;
const int OnMin1 = 23;
const int OffHour1 = 10;
const int OffMin1 = 24;
const int OnHour2 = 10;
const int OnMin2 = 23;
const int OffHour2 = 10;
const int OffMin2 = 24;



// 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) );
}
void setup()
{
  Wire.begin();
  Serial.begin(9600);

  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);

  digitalWrite(relay1, HIGH); //OFF
  digitalWrite(relay2, HIGH); //OFF

  // set the initial time here:
  //DS3231 seconds, minutes, hours, day, date, month, year
  //setDS3231time(30,30,18,4,22,11,17);
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
                   dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(byte *second,
                    byte *minute,
                    byte *hour,
                    byte *dayOfWeek,
                    byte *dayOfMonth,
                    byte *month,
                    byte *year)

{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
                 &year);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  Serial.print(":");
  if (minute < 10)
  {
    Serial.print("0");
  }
  Serial.print(minute, DEC);
  Serial.print(":");
  if (second < 10)
  {
    Serial.print("0");
  }
  Serial.print(second, DEC);
  Serial.print(" ");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print(" Day of week: ");
  switch (dayOfWeek) {
    case 1:
      Serial.println("Sunday");
      break;
    case 2:
      Serial.println("Monday");
      break;
    case 3:
      Serial.println("Tuesday");
      break;
    case 4:
      Serial.println("Wednesday");
      break;
    case 5:
      Serial.println("Thursday");
      break;
    case 6:
      Serial.println("Friday");
      break;
    case 7:
      Serial.println("Saturday");
      break;
  }
}
void loop()
{
  displayTime(); // display the real-time clock data on the Serial Monitor,
  delay(1000); // every second

  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
                 &year);
  if (hour == OnHour1 && minute == OnMin1) {
    digitalWrite (relay1, LOW);
  }
  else if (hour == OffHour1 && minute == OffMin1) {
    digitalWrite (relay1, HIGH);
  }
  displayTime(); // display the real-time clock data on the Serial Monitor,
  delay(1000); // every second


  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
                 &year);
  if (hour == OnHour2 && minute == OnMin2) {
    digitalWrite (relay1, LOW);
  }
  else if (hour == OffHour2 && minute == OffMin2) {
    digitalWrite (relay1, HIGH);
  }
}


Now I want to add a LCD 20x4 to the project. I got this from a site and it works with my LCD Adding the both to my project is what I am trying to do
Code: Select all#include <dummy.h>


/*-----( Import needed libraries )-----*/
#include <Wire.h>  // Comes with Arduino IDE
// Get the LCD I2C Library here:
// www.4tronix.co.uk/arduino/sketches/LiquidCrystal_V1.2.1.zip
// Move any other LCD libraries to another folder or delete them
// See Library "Docs" folder for possible commands etc.
#include <LiquidCrystal_I2C.h>

/*-----( Declare Constants )-----*/
/*-----( Declare objects )-----*/
// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
//LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address

/*-----( Declare Variables )-----*/
//NONE

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
   Serial.begin(9600);  // Used to type in characters

   lcd.begin(20, 4);   // initialize the lcd for 20 chars 4 lines, turn on backlight

  // ------- Quick 3 blinks of backlight  -------------
   for (int i = 0; i < 3; i++)
   {
      lcd.backlight();
      delay(250);
      lcd.noBacklight();
      delay(250);
   }
   lcd.backlight(); // finish with backlight on 

  //-------- Write characters on the display ------------------
  // NOTE: Cursor Position: (CHAR, LINE) start at 0 
   lcd.setCursor(0, 0); //Start at character 4 on line 0
   lcd.print("");
   delay(1000);
   lcd.setCursor(0, 1);
   lcd.print(""); // Print text on 2nd Line
   delay(1000);
   lcd.setCursor(0, 2);
   lcd.print(" "); //Print 20 characters on 3rd line
   delay(1000);
   lcd.setCursor(0, 3);
   lcd.print("");
   delay(8000);

   // Wait and then tell user they can start the Serial Monitor and type in characters to
   // Display. (Set Serial Monitor option to "No Line Ending")
   lcd.clear();
   lcd.setCursor(0, 0); //Start at character 0 on line 0
   lcd.print("Use Serial Mon");
   lcd.setCursor(0, 1);
   lcd.print("Type to display");


}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/

{
   // when characters arrive over the serial port...
   if (Serial.available()) {
      // wait a bit for the entire message to arrive
      delay(100);
      // clear the screen
      //lcd.clear();
      // read all the available characters
      while (Serial.available() > 0) {
         // display each character to the LCD
         //lcd.write(Serial.read());
      }
   }
}

/* --(end main loop )-- */


/* ( THE END ) */


Thank You for your help