Relay will not reset once activated

I have a relay set to activate when the time (rtc) and the potentiometer are of the correct value. For example if it is between 00:00 and 14:00, and the potentiometer is set to the voltage that corresponds to that it will turn on. However if you adjust the potentiometer it will just stay on. I tried using "else digitalWrite(RelayS,LOW);" but that made it only turn on for the 24hr setting. I'm not sure what command I am missing... any help would be greatly appreciated. This is the full code...

#include <Wire.h>
#include <DS3231.h>

DS3231 clock;
RTCDateTime dt;
const int RelayS = 8;

int Pot = A2;
int readVal;
float V2;


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

  Serial.println("Initialize RTC module");
  
  clock.begin();
  pinMode(8,OUTPUT);

  clock.setDateTime(__DATE__, __TIME__);    
 
}

void loop()
{
  dt = clock.getDateTime();


  Serial.print("Time: ");
  Serial.print(dt.hour);   Serial.print(":");
  Serial.print(dt.minute); Serial.print(":");
  Serial.print(dt.second); Serial.println("");

  delay(1000);


//   RELAY TEST  //   RELAY TEST  //   RELAY TEST  //   RELAY TEST  //   RELAY TEST  //   RELAY TEST  
  
  //if(dt.minute > 45)
    //{digitalWrite(RelayS,LOW);}

//   POT. TEST  //   POT. TEST  //   POT. TEST  //   POT. TEST  //   POT. TEST  //   POT. TEST  

readVal=analogRead(Pot);
V2=(5./1023.)*readVal;
Serial.print ("Pot. Voltage Is ");
Serial.println(V2);

// Light Hours //// Light Hours //// Light Hours //// Light Hours //// Light Hours //// Light Hours //
//1
if ((V2>0.00 && V2<0.20) && (dt.hour>00 && dt.hour<1)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 01:00");} 

//2
if ((V2>0.21 && V2<0.41) && (dt.hour>00 && dt.hour<2)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 02:00");} 

//3
if ((V2>0.42 && V2<0.62) && (dt.hour>00 && dt.hour<3)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 03:00");} 

//4
if ((V2>0.63 && V2<0.83) && (dt.hour>00 && dt.hour<4)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 04:00");} 

//5
if ((V2>0.84 && V2<1.04) && (dt.hour>00 && dt.hour<5)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 05:00");} 

//6
if ((V2>1.05 && V2<1.25) && (dt.hour>00 && dt.hour<6)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 06:00");} 

//7
if ((V2>1.26 && V2<1.45) && (dt.hour>00 && dt.hour<7)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 07:00");} 

//8
if ((V2>1.46 && V2<1.66) && (dt.hour>00 && dt.hour<8)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 08:00");} 

//9
if ((V2>1.67 && V2<1.87) && (dt.hour>00 && dt.hour<9)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 09:00");} 

//10
if ((V2>1.88 && V2<2.08) && (dt.hour>00 && dt.hour<10)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 10:00");}

//11
if ((V2>2.09 && V2<2.29) && (dt.hour>00 && dt.hour<11)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 11:00");} 

//12
if ((V2>2.30 && V2<2.50) && (dt.hour>00 && dt.hour<12)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 12:00");} 

//13
if ((V2>2.51 && V2<2.70) && (dt.hour>00 && dt.hour<13)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 13:00");} 

//14
if ((V2>2.71 && V2<2.91) && (dt.hour>00 && dt.hour<14)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 14:00");} 

//15
if ((V2>2.92 && V2<3.12) && (dt.hour>00 && dt.hour<15)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 15:00");} 

//16
if ((V2>3.13 && V2<3.33) && (dt.hour>00 && dt.hour<16)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 16:00");} 

//17
if ((V2>3.34 && V2<3.54) && (dt.hour>00 && dt.hour<17)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 17:00");} 

//18
if ((V2>3.55 && V2<3.75) && (dt.hour>00 && dt.hour<18)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 18:00");} 

//19
if ((V2>3.76 && V2<3.95) && (dt.hour>00 && dt.hour<19)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 19:00");}

//20
if ((V2>3.96 && V2<4.16) && (dt.hour>00 && dt.hour<20)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 20:00");}

//21
if ((V2>4.17 && V2<4.37) && (dt.hour>00 && dt.hour<21)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 21:00");}

//22
if ((V2>4.38 && V2<4.58) && (dt.hour>00 && dt.hour<22)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 22:00");} 

//23
if ((V2>4.59 && V2<4.79) && (dt.hour>00 && dt.hour<23)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 23:00");}

//24
if ((V2>4.80 && V2<5.00) && (dt.hour>00 && dt.hour<24)) 
{digitalWrite(RelayS,HIGH); Serial.println("00:00 To 24:00");}



}

That's some long and repetitive code!

Can you explain to us, like you were explaining to your mum, what your are trying to do?

Feels like you are making it more complicated than it is.

1 Like

Nowhere in your code do you ever set the Relay to LOW... so once it is on it is on for good.

If you change your if statements to else if (they are all mutually exclusive), and add the else digitalWrite(RelayS,LOW); at the end it should work.

1 Like

I had that else statement at the end of every line and only the 24 hr setting would work. Then I tried the just putting that statement at the end and it never turned on.

Did you do this?

1 Like

So basically, the RTC cycles through 24 hours, then I divided the potentiometer readings into 24 different parts, hence the repetitive code. The reason for that is to have the relay on from only 1hr to 24hrs and everything in between. So if I want it on for 6 hrs I just turn it to that voltage. I had the else {digitalWrite(RelayS,LOW)} at the end of every one of those statements, but when I did that, it would only come on for the 24hr setting. I also tried putting digitalWrite (RelayS, LOW) just at the end and it would never come on.

Also these statements are missing some values...

  if ((V2 > 0.00 && V2 < 0.20) && (dt.hour > 00 && dt.hour < 1))

should probably be <= 0.20 and <= 1

It's pretty ugly going using a big if statement like that... but anyway

1 Like

Yeah this is my first shot not following tutorials so I figured it was probably ugly but I gotta learn somehow. The else if fixed it. I really appreciate it, i know it was a noob question!

You should check out the map statement. You could use it to map your analogRead value directly to a number 0-23. Then just compare the result directly to dt.hour ... that way you can remove the big if statement.

2 Likes

Was thinking the same. And then use that number in a Switch(case) statement.

Example pot code, to get that case number.

const byte potPin = A0;
byte number, oldNumber;

void setup() {
  Serial.begin(9600);
}

void loop() {
  number = map(analogRead(potPin), 0, 1024, 1, 25); // 0-1023 to 1-24 (not a typo)
  if (number != oldNumber) { // only print if changed
    Serial.println(number);
    oldNumber = number; // update
    delay(10); // stability
  }
}

Leo..

1 Like

I'll give it a shot, I didn't know how to use the map function so I was just going off what I learned so far. I'll put the code in and give it a try, I'm gonna have to condense it at some point anyway so it'll definitely be good to figure it out so I have enough room in my sketch for the rest. I really appreciate the help! As you can probably tell I'm pretty new to this. :slight_smile:

Would this work?
Leo..

  number = map(analogRead(potPin), 0, 1024, 1, 25);
  if (number != oldNumber) { // only print if changed
    Serial.println(number);
    oldNumber = number; // update
    delay(10); // stability
  }
    switch (number) {
      case 1: (dt.hour > 00 && dt.hour <= 1) ? digitalWrite(RelayS, HIGH) : digitalWrite(RelayS, LOW), break;
      case 2: (dt.hour > 00 && dt.hour <= 2) ? digitalWrite(RelayS, HIGH) : digitalWrite(RelayS, LOW), break;
      case 3: (dt.hour > 00 && dt.hour <= 3) ? digitalWrite(RelayS, HIGH) : digitalWrite(RelayS, LOW), break;
        //etc
    }

You don't need a case statement at all...

if (dt.hour > 00 && dt.hour <= number)
  digitalWrite(RelayS, HIGH);
else
  digitalWrite(RelayS, LOW);
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.