please help me

Hi guys I am a total newbee and I am trying to build a timer circuit that will put on a relay 15 min on and 15 min off, I have a arduino uno and a ds3231 rtc, here is my current code and all it does is switch the relay on and nothing else
#include <DS3231.h>

int Relay = 4;

DS3231 rtc(SDA, SCL);
Time t;

const int OnHour = (12,1,2,3,4,5,6,7,8,9,10,11);
const int OnMin = (0,1,3,5,7,9);
const int OffHour =( 12,1,2,3,4,5,6,7,8,9,10,11);
const int OffMin = (2,4,6,8,10);

void setup() {
Serial.begin(9600);
rtc.begin();
pinMode(Relay, OUTPUT);
digitalWrite(Relay, LOW);
}

void loop() {
t = rtc.getTime();
Serial.print(t.hour);
Serial.print(" hour(s), “);
Serial.print(t.min);
Serial.print(” minute(s)");
Serial.println(" ");
delay (1000);

if(t.hour == OnHour && t.min == OnMin){
digitalWrite(Relay,LOW);
Serial.println(“LIGHT ON”);
}

else if(t.hour == OffHour && t.min == OffMin){
digitalWrite(Relay,HIGH);
Serial.println(“LIGHT OFF”);
}
}

062-Trigger-Relay-with-DS3231-RTC.ino (798 Bytes)

const int OnHour = (12,1,2,3,4,5,6,7,8,9,10,11); What are all those values for?

What do your debug prints tell you?

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:

hi I was trying to set up on time in hour and min to when it switches on and the same for off time

So a single on time and a single off time.

So, what are all the values in parentheses?

Which did you intend to use?

Which one gets used?

TheMemberFormerlyKnownAsAWOL:
So a single on time and a single off time.

So, what are all the values in parentheses?

Which did you intend to use?

Which one gets used?

what I need is to turn on a fan for 15 min then turn off the fan for 15 min and repeat it until I turn it off

I know there is a way to do this I have just hit a blank and need a bit of help please

If that’s really all you have to do, I’d recommend delay

const byte Relay = 4;

int relayState = LOW; // may needs to be HIGH, depends on the relay driver.
void setup() 
{
  pinMode(Relay, OUTPUT);
  digitalWrite(Relay, relayState);
}

void loop ()
{
  delay (15UL * 1000UL * 60UL);
  relayState = (HIGH + LOW) - relayState;
  digitalWrite (Relay, relayState);
}

(uncompiled, untested)

if(t.hour == OnHour && t.min == OnMin){

Because OnHour and OnMin are arrays, your comparison statements are comparing t.hour and t.min to the memory address that the array is stored at, not the values stored in the arrays.

david_2018:

if(t.hour == OnHour && t.min == OnMin){

Because OnHour and OnMin are arrays, your comparison statements are comparing t.hour and t.min to the memory address that the array is stored at, not the values stored in the arrays.

but they’re not arrays.

Mushroom_man:
I am trying to build a timer circuit that will put on a relay 15 min on and 15 min off, I have a arduino uno and a ds3231 rtc, here is my current code and all it does is switch the relay on and nothing else

Something like this?

#include <DS3231.h>
const byte RelayPin = 4;
DS3231  rtc(SDA, SCL);
Time t;
void setup()
{
  Serial.begin(115200);
  while (!Serial);
  rtc.begin();
  pinMode(RelayPin, OUTPUT);
  digitalWrite(RelayPin, LOW);  // Turn relay on
}
void SetRelayOnState(bool newOnState)
{
  static bool currentOnState = true;
  // Only print when the state changes
  if (newOnState == currentOnState)
    return; // Nothing to do
  currentOnState = newOnState;
  if (currentOnState)
  {
    digitalWrite(RelayPin, LOW);
    Serial.println("RELAY ON");
  }
  else
  {
    digitalWrite(RelayPin, HIGH);
    Serial.println("RELAY OFF");
  }
}
void loop()
{
  static int currentMinute = -1;
  t = rtc.getTime();
  // Show time at the start of each new minute
  if (t.min != currentMinute)
  {
    currentMinute = t.min;
    Serial.print(t.hour);
    Serial.print(" hour(s), ");
    Serial.print(t.min);
    Serial.println(" minute(s)");
  }
  if (t.min % 30 < 15)
  {
    // For the first half of every half hour, turn on the Relay
    SetRelayOnState(true);
  }
  else
  {
    // For the second half of every half hour, turn off the Relay
    SetRelayOnState(false);
  }
}

You guys are awesome thanks so much for the helpful info

TheMemberFormerlyKnownAsAWOL:
but they're not arrays.

Ah, I see that now - will have to run that through the compiler to actually see what it is, the form is not something I'm familiar with.

I don’t think anyone is.
All hail the comma operator and parentheses.

Mushroom_man:
Hi guys I am a total newbee and I am trying to build a timer circuit that will put on a relay 15 min on and 15 min off, I have a arduino uno and a ds3231 rtc, here is my current code and all it does is switch the relay on and nothing else

const int OnHour = (12,1,2,3,4,5,6,7,8,9,10,11);

const int OnMin = (0,1,3,5,7,9);
const int OffHour =( 12,1,2,3,4,5,6,7,8,9,10,11);
const int OffMin = (2,4,6,8,10);

They aren’t arrays but you only need to add two character and change two characters in each line to make them arrays. It’s easier to work with ‘byte’ arrays rather than ‘int’ arrays so I would change the type. Then, because you can’t compare an integer value to an array, it makes sense to write a function that will check an array for a match.

#include <DS3231.h>


const byte RelayPin = 4;


DS3231  rtc(SDA, SCL);
Time t;


const byte OnHour[] = {12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
const byte OnMin[] = {0, 1, 3, 5, 7, 9};
const byte OffHour[] = {12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
const byte OffMin[] = {2, 4, 6, 8, 10};


void setup()
{
  Serial.begin(9600);
  rtc.begin();
  pinMode(RelayPin, OUTPUT);
  digitalWrite(RelayPin, LOW);
}


bool isInByteList(byte value, const byte *array, byte length)
{
  for (byte i = 0; i < length; i++)
    if (array[i] == value)
      return true;


  return false;
}


void loop()
{
  t = rtc.getTime();
  Serial.print(t.hour);
  Serial.print(" hour(s), ");
  Serial.print(t.min);
  Serial.print(" minute(s)");
  Serial.println(" ");
  delay (1000);


  if (isInByteList(t.hour, OnHour, sizeof OnHour) && isInByteList(t.min, OnMin, sizeof OnMin))
  {
    digitalWrite(RelayPin, LOW);
    Serial.println("LIGHT ON");
  }
  else  if (isInByteList(t.hour, OffHour, sizeof OffHour) && isInByteList(t.min, OffMin, sizeof OffMin))
  {
    digitalWrite(RelayPin, HIGH);
    Serial.println("LIGHT OFF");
  }
}

You guys are super stars thanks