Relay not Switching even though the relay function is called

Hi,

I have the below code, tried to paste relevant parts of it. However MOST of the times, even though the variable inside the relay operating function changes, the relay does not seem to work. In essence there seems to be a physical problem. Do I need to use delays when working with relays?

I’m using Dual Relay module, nRF24L01. as mentioned here:

The variable BlindsMessage below is the value I receive from a switch in another arduino with nRF24L01’s providing connection between to. In essence when I switch it to open, it sends message 11 and 22 if I switched it to close…

Then I try to keep the relay open for a number of seconds so that the blinds are fully closed or opened.

So, sometimes, most of the time, even though the AreTheBlindsOpen changes from 1 to 0, the relay itself does not seem to do any work. and vice versa…

#include <elapsedMillis.h>

elapsedMillis timeElapsed;



if (BlindsMessage == 11 && AreTheBlindsOpen == true)
  {
    CloseBlinds();
    PreviousBlindsMessage = BlindsMessage;
    BlindsMessage = 0;
  }



  if (BlindsMessage == 22 && AreTheBlindsOpen == false)
  {
    OpenBlinds();
    PreviousBlindsMessage = BlindsMessage;
    BlindsMessage = 0;
  
  }



void OpenBlinds()
{
  unsigned int interval = 15000;
  while (timeElapsed < interval)
  {
        digitalWrite(RELAY1_THE_BLINDS_OPENING_RELAY, RELAY_ON);
  };
  digitalWrite(RELAY1_THE_BLINDS_OPENING_RELAY, RELAY_OFF);
  AreTheBlindsOpen = 1;
  timeElapsed = 0;



}


void CloseBlinds()
{
  unsigned int interval = 15000;
  while (timeElapsed < interval)
  {
   
      digitalWrite(RELAY2_THE_BLINDS_CLOSING_RELAY, RELAY_ON);
  };
  digitalWrite(RELAY2_THE_BLINDS_CLOSING_RELAY, RELAY_OFF);
  AreTheBlindsOpen = 0;
  timeElapsed = 0;

}

I’m also attaching my code and the Fritzing diagram I have so far…

sketch_nov08a.ino (9.29 KB)

Check physical circuit using a basic code to turn the relay on and off every couple of seconds. This will then check that you have wired the module correctly. Basically “Blink” using the relay.

digitalWrite (relay1, RELAY_OFF);

Where have you defined the value of RELAY_OFF?

Weedpharma

while (timeElapsed < interval)
  {
        digitalWrite(RELAY1_THE_BLINDS_OPENING_RELAY, RELAY_ON);
  };

That’s an infinite loop as timeElapsed doesn’t change inside the body.

Sorry I should have mentioned, Timeellapsed is part of the ellapsedmillis library http://playground.arduino.cc/Code/ElapsedMillis And it helps to avoid using delay by providing its own timer counter. Though I will try declaring and modifying this within the function...

Yes also I have defined the value off relayoff as 0.

is timeElapsed driven by some sort of interrupt? If that's the case then you're ok. Not good because you could just be using millis there and saving yourself some code space. But at least the while loop wouldn't be infinite.

http://snippets-r-us.com

Weedpharma

Hi, Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png? How are you driving the relay?

Tom.... :)

I like that http://snippets-r-us.com I will definetely keep that in mind :slight_smile: But I also attached the original code as ino file so consider that too.

I tried to attach earlier but it didnt work, here is the final fritzing.

Hi, Are those pullups 220R? Change them to 10K, or turn pullups ON in software.

Tom.... :)

nervusvagus: .........But I also attached the original code as ino file so consider that too.

I use an iPad to read the forum. It will not allow me to read .ino.

Weedpharma