IR Sensor & Relay Shield Help

Hey there,

located below is my code. I am attempting to turn on and off 4 relays with my Arduino & Seed studio relay shield first version. I am able to press the first button the the remote and turn on the first relay but the last 3 don’t do anything. the 5th button towards the end of the code turns off all the relays successfully. Can anyone determine a solution or help out. The end goal is to make it so 4 buttons turn on and off individual relays.

Thank You!
Chris

#include <IRremote.h>

int RECV_PIN = 11;
int relay1 = 2;
int relay2 = 3;
int relay3 = 4;
int relay4 = 5;

int on = 1;
int on1 = 0;
int on2 = 0;
int on3 = 0;

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  pinMode(relay4, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay1, OUTPUT);
  pinMode(13, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}

unsigned long last = millis();

void loop() {
  if (irrecv.decode(&results))
  {
   if (results.value == 551505585) 
    {
      if (millis() - last > 250) 
      {
        on3 = !on3;
        digitalWrite(relay4, HIGH);
      }
      last = millis();
    }
else if (results.value == 551521905) 
    {
      if (millis() - last > 250) 
      {
        on3 = !on3;
        digitalWrite(relay1, LOW);
        digitalWrite(relay2, LOW);
        digitalWrite(relay3, LOW);
        digitalWrite(relay4, LOW);  
}

 else if (results.value == 551519865) 
    {
      if (millis() - last > 250) 
      {
        on3 = !on3;
        digitalWrite(relay4, HIGH);
      }
      last = millis();
    }
else if (results.value == 551494365) 
    {
      if (millis() - last > 250) 
      {
        on3 = !on3;
        digitalWrite(relay1, LOW);
        digitalWrite(relay2, LOW);
        digitalWrite(relay3, LOW);
        digitalWrite(relay4, LOW);
      }
      last = millis();
    }
    irrecv.resume(); // Receive the next value
    }
  }
}

Looks like you have some curly braces issues. This portion just doesn't look right:

else if (results.value == 551521905) 
    {
      if (millis() - last > 250) 
      {
        on3 = !on3;
        digitalWrite(relay1, LOW);
        digitalWrite(relay2, LOW);
        digitalWrite(relay3, LOW);
        digitalWrite(relay4, LOW);  
}

okay I have got it to work on relay 3 and 4 when each time i press the button it turns the relay on and or off how can i add relay 1 and 2 ?? seems the setup for 3 and 4 wont work the same when coded that way

here is the code

#include <IRremote.h>

int RECV_PIN = 11;
int relay1 = 2;
int relay2 = 3;
int relay3 = 4;
int relay4 = 5;

int on = 1;
int on1 = 0;
int on2 = 0;
int on3 = 0;

IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  pinMode(relay4, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay1, OUTPUT);
  pinMode(13, OUTPUT);
  irrecv.enableIRIn(); // Start the receiver
}

unsigned long last = millis();

void loop() {
  if (irrecv.decode(&results))
  {
   

if (results.value == 16830159 ) 
    {
      if (millis() - last > 250) 
      {
        on2 = !on2;
        digitalWrite(relay3, on2 ? HIGH : LOW);
      }
      last = millis();
}

 else if (results.value == 16797263) 
    {
      if (millis() - last > 250) 
      {
        on3 = !on3;
        digitalWrite(relay4, on3 ? HIGH : LOW);
      }
      last = millis();

    }
    irrecv.resume(); // Receive the next value
  }
}

sager9102:
seems the setup for 3 and 4 wont work the same when coded that way

I don't see why not. What stopping you from expanding your else ifs to accomadate for relays 1 and 2?

The point was that you need to use Tools + Auto Format to clean up that horrid indenting, delete the excessive, useless white space, and repost your code.

Something more than "this doesn't work" is needed, too. That code does something, right or wrong. You expect it to do something (that expectation might also be right or wrong). But, you haven't told us what it does or what you expect.