Code works but need explanation pls

Hi All
I working on a timed based project that at a certain time i need to read A1 and turn the the led on.
I have the code working but need some explanation why my first code did not work proberly, time functions working using ds1703 module.

First Code only prints Serial but does not read A1 at set time:

#include <RTClib.h>
#include <Wire.h>

RTC_DS1307 RTC;

void setup () {
  Wire.begin();
  RTC.begin();
  Serial.begin(9600);


  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);

  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  // following line sets the RTC to the date & time this sketch was compiled
  // RTC.adjust(DateTime(__DATE__, __TIME__));
}

void loop () {
  DateTime now = RTC.now();

  Serial.print(now.day(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.year(), DEC);
  Serial.print(' ');

  if (now.hour() < 10)
    Serial.print('0');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  if (now.minute() < 10)
    Serial.print('0');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  if (now.second() < 10)
    Serial.print('0');
  Serial.print(now.second(), DEC);
  Serial.println();
  delay(100);


  // Serial.println(" It is time to report !! ");
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);

  //Timer1
  if ( ( now.hour() == 19 ) && ( now.minute() == 20 ) )
  {
    // Serial.println(" It is time to report !! ");
    int sensorValue = analogRead(A0);
    // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
    float voltage = sensorValue * (5.0 / 1023.0);

    if (voltage >= 1);
    Serial.println(voltage);
    digitalWrite(7, HIGH);

  }
  else
  {
    digitalWrite(7, LOW);
  }

SECOND CODE works well:

#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;

void setup () {

  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);

  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);

  Serial.begin(9600);

  Wire.begin();
  RTC.begin();
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
}
void loop () {
  DateTime now = RTC.now();
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
  delay(1000);

  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);


  if ( ( now.hour() == 20 ) && ( now.minute() == 44 )  && (voltage >= 1))
  {
    digitalWrite(7, HIGH);
  }
  else
  {

    digitalWrite(7, LOW);
  }
}

Any info would be app.
Thanks

You are not reading A1 at all in any of the examples, only A0 is read. In the first example you are reading A0 twice and you only need to do this once. The line "if (voltage >= 1);" is useless since it does nothing.

Ivanfd1:
Hi All
I working on a timed based project that at a certain time i need to read A0 and turn the the led on.
I have the code working but need some explanation why my first code did not work proberly, time functions working using ds1703 module.

First Code only prints Serial but does not read A0 at set time:

#include <RTClib.h>

#include <Wire.h>

RTC_DS1307 RTC;

void setup () {
  Wire.begin();
  RTC.begin();
  Serial.begin(9600);

pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);

pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  // following line sets the RTC to the date & time this sketch was compiled
  // RTC.adjust(DateTime(DATE, TIME));
}

void loop () {
  DateTime now = RTC.now();

Serial.print(now.day(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.year(), DEC);
  Serial.print(' ');

if (now.hour() < 10)
    Serial.print('0');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  if (now.minute() < 10)
    Serial.print('0');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  if (now.second() < 10)
    Serial.print('0');
  Serial.print(now.second(), DEC);
  Serial.println();
  delay(100);

//Timer1

if ( ( now.hour() == 19 ) && ( now.minute() == 20 ) )
  {
   
    int sensorValue = analogRead(A0);
    // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
    float voltage = sensorValue * (5.0 / 1023.0);

if (voltage >= 1);
    Serial.println(voltage);
    digitalWrite(7, HIGH);

}
  else
  {
    digitalWrite(7, LOW);
  }






SECOND CODE works well:



#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;

void setup () {

pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);

pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);

Serial.begin(9600);

Wire.begin();
  RTC.begin();
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(DATE, TIME));
  }
}
void loop () {
  DateTime now = RTC.now();
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
  delay(1000);

int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);

if ( ( now.hour() == 20 ) && ( now.minute() == 44 )  && (voltage >= 1))
  {
    digitalWrite(7, HIGH);
  }
  else
  {

digitalWrite(7, LOW);
  }
}




Any info would be app.
Thanks

Ok edited mistake but still dont understand why if i put " if (voltage >= 1); " after " if ( ( now.hour() == 19 ) && ( now.minute() == 20 ) )
{" it only serial prints

Don't quote your self, post the new code instead :slight_smile: