Time based loop with DS3231

Purpose is to read the time from DS3231, at specific instances of "second" a digital output is to be set either HIGH or LOW.
Libraries used work well.

I use this to read the time:

  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
                 &year);

..and this to set the conditions (output HIGH when seconds hit 01, 11, 21, 31, 41, 51):

  if (second == 01 || second == 11 || second == 21 || second == 31 || second == 41 || second == 51)
  {
    digitalWrite(LED2, HIGH);
  }

..and this to set the conditions for output LOW (when seconds are either 05, 15, 25, 35, 45 or 55):

  if (second == 05 || second == 15 || second == 25 || second == 35 || second == 45 || second == 55)
  {
    digitalWrite(LED2, LOW);
  }

When adding code below before the if-conditions, the output changes state erratically:

  static byte lastSecond = 0;
  if ( second == lastSecond ) {
    return;
  }
  lastSecond = second;  // remember what second was last time we looked

What is wrong with this code?

Complete file:

[code]

#include <Wire.h>
#include <RTClibExtended.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include "Adafruit_HTU21DF.h"
#define DS3231_I2C_ADDRESS 0x68
RTC_DS3231 RTC;      //we are using the DS3231 RTC
int analog_ip = A0;   // select the input pin for the potentiometer
int LED1 = 13;          // select the pin for the LED
int LED2 = 14;
int inputVal = 0;     // variable to store the value coming from potentiometer
int old_ip_val = 0;   // varialbe to store historic value of analog input.

// 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) );
}

//============
/**************************
     S E T U P
 **************************/

void setup() {
  pinMode(LED1, OUTPUT);  // Defining LED Pin as output 
  pinMode(LED2, OUTPUT);
  Serial.begin(9600);   // Setup Serial Communication.               

  // set the initial time here below; uncomment second line below; compile and load once, the comment that line again and reload
  // DS3231 seconds, minutes, hours, day (sunday = day1), date, month, year
  // setDS3231time(30,46,7,5,22,3,18);

  
}

/**************************
   S E T   T I M E
**************************/
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();
}

/**************************
   R E A D   T I M E
**************************/

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());
}

/*********************************
   M A I N   L O O P
**********************************/


void loop(){

    inputVal = analogRead(analog_ip);  // Reading and storing analog input value.
    // following if condition is to check last value with new value if new value = +/-5
    // then we are not interested to do anything. For other values we want to it to to -
    if (inputVal <old_ip_val-5 || inputVal >old_ip_val+5) 
    {
        Serial.print("Input Value    : ");
        Serial.print(inputVal);                // Printing Analog input value (0-1023) 
        Serial.print("\nLED Brightness : ");
        Serial.print(inputVal/1);              // Printing PWM values for LED (0-255)
        Serial.print("\n"); 
        analogWrite(LED1, inputVal/1);          // Sending PWM value to LED. 
        delay(50);                            // Waiting for a while.
        old_ip_val = inputVal;                 // Storing historic value of analog input. 
  
  }

  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  // if based on number of times per minute, use second and lastsecond
  // if based on number of times per hour, use minute and lastminute
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
                 &year);
/*       If measurements faster than once per minute: use lastSecond, second          
 *       If measurements slower than once per minute but faster than once per hour: use lastMinute and minute
 *                  
 */
  static byte lastSecond = 0;
  if ( second == lastSecond ) {
    return;
  }
  lastSecond = second;  // remember what minute was last time we looked

 
  if (second == 01 || second == 11 || second == 21 || second == 31 || second == 41 || second == 51)
  {
    digitalWrite(LED2, HIGH);
  }
  if (second == 05 || second == 15 || second == 25 || second == 35 || second == 45 || second == 55)
  {
    digitalWrite(LED2, LOW);
  }
}
  lastSecond = second;  // remember what minute was last time we looked

That comment looks pretty silly.

Print out the values of second and lastSecond before your last snippet, and after your last snippet, and I'm pretty sure that a clue by 4 will whack you.

PaulS:

  lastSecond = second;  // remember what minute was last time we looked

That comment looks pretty silly.

Print out the values of second and lastSecond before your last snippet, and after your last snippet, and I'm pretty sure that a clue by 4 will whack you.

Hi PaulS, serial output show both "second" and "lastSecond" to be 85??

"seconds" should be supposed to be between 0 and 60?

When I remove the lastSecond code, it still show "second" to be 85.

What do I do wrong? Why is the read instruction not giving the actual second value?

Solved: I added

Wire.begin(4, 5); // I2C 4 = SDA, 5 = SCL

in setup.