433 Mhz module versus IR receiver

Hi everyody,
I'm not really good in arduino sketchs as other guys in this forum but I tried to fix the problem in my sketch during so much time.
I need your help.

The sketch attached use a 433 mhz module receiver everything work well exept the
Lower limit switch and Upper limit switch, nothing happen even switches are LOW or HIGH .

All my wire connections are good.

When SwitchState1 or SwitchState2 == LOW we've got 0 volts and HIGH we've got 5 volts.

Then let me know if you got something wrong in my sketch.
:o

Cordon_Led_433mhz.ino (3.23 KB)

here is the code as it should be posted here using code tags

#include <FiniteStateMachine.h>

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

#define switch_2ledBlueON       2214824882
#define switch_2ledBlueOFF      2214822842
#define switch_3ledRedON        2214825398
#define switch_3ledRedOFF       2214823358
#define switch_4UP              2214825653
#define switch_4DOWN            2214823613
#define switch_All              2214824112




int ledBlue = 5;    // Led blue Light
int ledRed = 6;    // Led blue Light
int DOWN = 8;    // DOWN pin
int UP = 7;   // DOWN pin
int switchEnd1 = 3; // SWITCH DOWN pin
int switchEnd2 = 4; // SWITCH UP pin


int SwitchState1 = 0;
int SwitchState2 = 0;

void setup()
{
  pinMode(ledBlue, OUTPUT);
  pinMode(ledRed, OUTPUT);
  pinMode(DOWN, OUTPUT);
  pinMode(UP, OUTPUT);
  pinMode(switchEnd1, INPUT);
  pinMode(switchEnd2, INPUT);
  mySwitch.setProtocol(1);
  mySwitch.setPulseLength(260);

  mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2
}

void loop()
{

  SwitchState1 = digitalRead(switchEnd1);      //switch du bas
  SwitchState2 = digitalRead(switchEnd2);      //switch du haut

  if (mySwitch.available())
  {

    long value = mySwitch.getReceivedValue();  //get value example: Switch 2 ON: 2214824882;

    switch (value) {

      case 2214824882:         // Blue Light ON 433Mhz Transmitter
        {
          digitalWrite(ledBlue, HIGH);
          delay(100);
          break;

        case 2214822842:         // Blue Light OFF 433Mhz Transmitter
          digitalWrite(ledBlue, LOW);
          delay(100);
          break;

        case 2214825398:         // Red Light ON 433Mhz Transmitter
          digitalWrite(ledRed, HIGH);
          delay(100);
          break;

        case 2214823358:         // Red Light OFF 433Mhz Transmitter
          digitalWrite(ledRed, LOW);
          delay(100);
          break;

        case 2214825653:           // Up Store 433Mhz Transmitter
          digitalWrite(UP, HIGH);
          delay(100);
          digitalWrite(DOWN, LOW);
          delay(100);
          break;

        case 2214823613:         // Down Store 433Mhz Transmitter
          digitalWrite(DOWN, HIGH);
          delay(100);
          digitalWrite(UP, LOW);
          delay(100);
          break;

        case 2214824112:         // Stop Store 433Mhz Transmitter
          digitalWrite(DOWN, LOW);
          delay(100);
          digitalWrite(UP, LOW);
          delay(100);
          break;
        }

        if (SwitchState1 == LOW)    // Lower limit switch
        {

          digitalWrite(DOWN, LOW);
          delay(10);
          digitalWrite(UP, HIGH);
          delay(200);
          digitalWrite(UP, LOW);
          delay(10);

        }
        if (SwitchState2 == LOW)    //Upper limit switch
        {

          digitalWrite(UP, LOW);
          delay(10);
          digitalWrite(DOWN, HIGH);
          delay(200);
          digitalWrite(DOWN, LOW);
          delay(10);
        }


        mySwitch.resetAvailable();
    }
  }
}

I think the issue is that you execute this

        if (SwitchState1 == LOW)    // Lower limit switch
        {

          digitalWrite(DOWN, LOW);
          delay(10);
          digitalWrite(UP, HIGH);
          delay(200);
          digitalWrite(UP, LOW);
          delay(10);

        }
        if (SwitchState2 == LOW)    //Upper limit switch
        {

          digitalWrite(UP, LOW);
          delay(10);
          digitalWrite(DOWN, HIGH);
          delay(200);
          digitalWrite(DOWN, LOW);
          delay(10);
        }

only once, when you have received something from the RC. so you get your motor going but since you are not receiving data anymore, you don't do your limit check. you need to move that part of the code out of the englobing if statement

also the

digitalWrite(DOWN, LOW);
delay(10);

when you hit the top switch is probably not needed; if you hit the top then you were not moving down and thus DOWN was LOW anyway already. you can probably get rid of it. same for the other one.

Also for this in the switch case

          digitalWrite(UP, HIGH);
          delay(100);
          digitalWrite(DOWN, LOW);
          delay(100);

why do you need the delay(100) ? get rid of them.

so you have nice #define at the beginning (switch_2ledBlueON) and then you don't use them in you switch case ?

this is how getReceivedValue is defined

unsigned long getReceivedValue();

why do you store the value into a signed long?

unsigned longs won't store negative numbers, making their range from 0 to 4,294,967,295 (2^32 - 1) whereas long store values from -2,147,483,648 to 2,147,483,647.

Looks like the values you expect are within range and won't overflow but this could be a source of error.

the #define at the beginning (if you were to use them) should also instruct the compiler about the format and should read#define switch_2ledBlueON      2214824882ULThe UL at the end tells the compiler what type of data to use when it will see that value later in the code.

Are your switches without pullups? If so try:

  pinMode(switchEnd1, INPUT_PULLUP);
  pinMode(switchEnd2, INPUT_PULLUP);

MarkT:
Are your switches without pullups? If so try:

  pinMode(switchEnd1, INPUT_PULLUP);

pinMode(switchEnd2, INPUT_PULLUP);

Indeed I had assumed he had external PULLUP. if not this is needed too

Hi everyone,
I was away, sorry for delay.
At first thanks so much for your answers and helps.
J-M-L, you wrote this below.
But I don't understand where to moved this part of code, could you tell me.

only once, when you have received something from the RC. so you get your motor going but since you are not receiving data anymore, you don't do your limit check. you need to move that part of the code out of the englobing if statement:

if (SwitchState1 == LOW) // Lower limit switch
{

digitalWrite(DOWN, LOW);
delay(10);
digitalWrite(UP, HIGH);
delay(200);
digitalWrite(UP, LOW);
delay(10);

}
if (SwitchState2 == LOW) //Upper limit switch
{

digitalWrite(UP, LOW);
delay(10);
digitalWrite(DOWN, HIGH);
delay(200);
digitalWrite(DOWN, LOW);
delay(10);
}
Thanks for you help
JL

please post code within code tags as I did in post #1. read how to use this forum

To your question, at the moment your loop() looks like this

void loop()
{

  SwitchState1 = digitalRead(switchEnd1);      //switch du bas
  SwitchState2 = digitalRead(switchEnd2);      //switch du haut

  if (mySwitch.available())
  {
    long value = mySwitch.getReceivedValue();  //get value example: Switch 2 ON: 2214824882;
    switch (value) {
        // various cases
        mySwitch.resetAvailable();
    }
  }
}

so in english that is

read state of both switches
if I've received something from the IR then do something with it

so if you have not received anything from the IR, then you just go back to the top of the loop and check again. consequence is that your code never checks for limit switches unless when IR codes are received.

what you need to do is

read state of both switches
if I've received something from the IR then do something with it
if motors are moving check if I'm hitting the limit switches

makes sense?

Hi J-M-L,
Here is my new code:

#include <FiniteStateMachine.h>

#include <RCSwitch.h>

RCSwitch mySwitch = RCSwitch();

#define switch_2ledBlueON       2214824882L
#define switch_2ledBlueOFF      2214822842L
#define switch_3ledRedON        2214825398L
#define switch_3ledRedOFF       2214823358L
#define switch_4UP              2214825653L
#define switch_4DOWN            2214823613L
#define switch_All              2214824112L




int ledBlue = 5;    // Led blue Light
int ledRed = 6;    // Led blue Light
int DOWN = 8;    // DOWN pin 
int UP= 7;    // UP pin 
int switchEnd1 = 3; // SWITCH DOWN pin 
int switchEnd2 = 4; // SWITCH UP pin


int SwitchState1 = 0;
int SwitchState2 = 0;

void setup()
{
  pinMode(ledBlue, OUTPUT); 
  pinMode(ledRed, OUTPUT);  
  pinMode(DOWN, OUTPUT);
  pinMode(UP, OUTPUT);
  pinMode(switchEnd1, INPUT_PULLUP);
  pinMode(switchEnd2, INPUT_PULLUP);  
  mySwitch.setProtocol(1);
  mySwitch.setPulseLength(260); 
  
  mySwitch.enableReceive(0);  // Receiver on inerrupt 0 => that is pin #2
}
   
void loop() 
{
   
    SwitchState1 = digitalRead(switchEnd1);      //    READ STATE OF BOTH SWITCHES 
    SwitchState2 = digitalRead(switchEnd2);      //    READ STATE OF BOTH SWITCHES

  
  
  if (mySwitch.available())
  {
     
  long value = mySwitch.getReceivedValue();  //get value example: Switch 2 ON: 2214824882;

switch (value) {   //    IF I'VE RECEIVED SOMETHING FROM THE 433 MHZ TRANSMITTER DO SOMETHING WITH IT
  
  case 2214824882L:         // Blue Light ON 433Mhz Transmitter 
{
            digitalWrite(ledBlue, HIGH); 
            delay(10);                       
            break;
              
  case 2214822842L:         // Blue Light OFF 433Mhz Transmitter 
            digitalWrite(ledBlue, LOW); 
            delay(10);
            break;            
                       
  case 2214825398L:         // Red Light ON 433Mhz Transmitter 
            digitalWrite(ledRed, HIGH); 
            delay(10);
            break;            
                            
  case 2214823358L:         // Red Light OFF 433Mhz Transmitter 
            digitalWrite(ledRed, LOW); 
            delay(10);
            break;            
           
 case 2214825653L:           // Up Store 433Mhz Transmitter 
            digitalWrite(UP, HIGH);
            delay(10);
            break;            
 
  case 2214823613L:         // Down Store 433Mhz Transmitter 
            digitalWrite(DOWN, HIGH);
            delay(10);
            break;
            
  case 2214824112L:         // Stop Store 433Mhz Transmitter 
            digitalWrite(DOWN, LOW);
            delay(10);            
            digitalWrite(UP, LOW);
            delay(10);
            break;  
  default:    //  IF MOTORS ARE MOVING CHECK IF I'M HITTING THE LIMIT SWITCHES
  if (DOWN== HIGH & SwitchState1== LOW)      // Lower limit switch 
  {       
            digitalWrite(UP, HIGH);
            delay(200);
            digitalWrite(UP, LOW);
            delay(10);       
  }
   if (UP== HIGH & SwitchState2== LOW)      //Upper limit switch 
  {        
            digitalWrite(DOWN, HIGH);
            delay(200);
            digitalWrite(DOWN, LOW);
            delay(10);
  }
                                                         
}
     
      mySwitch.resetAvailable();          
  }
}
}

I followed your advices but for a modest carpenter but I don't understand why it's always wrong.

Please Could you correct for me.

Kind regards
JL

Your limit testing is STILL in the switch/case and thus in the if mySwitch.available()
Your motors will be running even when something is not available. So instead of checking for limits within your if available and switch case, you need to check AFTER that if. As I said above your code structure needs to look like this

read state of both switches
if I've received something from the IR then do something with it

// AND SEPARAETELY FROM RECEIVING OR NOT SOMETHING
if motors are moving check if I'm hitting the limit switches