IR command issue

Hi, im extremely new to the arduino/C/C++ environment, I have been watching a lot of tutorials and i have gotten to a decent point, but im stuck with my current project(one of my first projects). the basic premise is i have 3 sets of lights(front, back, and amber director) and they all need to work independently and together(1,2, or 3 on at the same time) while being in there own sequences. i can get each sequence going individually but it either gets stuck on or it gets killed when i switch to a new pattern. im also trying to use a IR remote to control all of this.

any help would be greatly appreciated.

here is the code i have so far.

I have included a print line for all buttons on the remote but other than print what button they are for they have no purpose, but are open for further development.

emergency_lights_with_director.ino (7.86 KB)

When posting to the forum, it is better to place the sketch directly in your post using code tags, instead of attaching it to the post. This saves people time by being able to read through the code without having to download it, and open it in Arduino, which prompts them to create a folder etc.

#include <IRremote.h>

int RECV_PIN = 13;

IRrecv irrecv(RECV_PIN);

decode_results results;

int front1 = 4;
int front2 = 5;
int back1 = 6;
int back2 = 7;
int amber1 = 8;
int amber2 = 9;
int amber3 = 10;
int amber4 = 11;
int amber5 = 12;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver

  pinMode(front1,OUTPUT);
  pinMode(front2,OUTPUT);
  pinMode(back1,OUTPUT);
  pinMode(back2,OUTPUT);
  pinMode(amber1,OUTPUT);
  pinMode(amber2,OUTPUT);
  pinMode(amber3,OUTPUT);
  pinMode(amber4,OUTPUT);
  pinMode(amber5,OUTPUT);
}
void loop() {
  if (irrecv.decode(&results)) {

    irrecv.resume();
  }
  delay(50);
  switch(results.value)
  {
    case 0xFFA25D:
      Serial.println("Power, All lights off");
      digitalWrite(front1,LOW);
      digitalWrite(front2,LOW);
      digitalWrite(back1,LOW);
      digitalWrite(back2,LOW);
      digitalWrite(amber1,LOW);
      digitalWrite(amber2,LOW);
      digitalWrite(amber3,LOW);
      digitalWrite(amber4,LOW);
      digitalWrite(amber5,LOW);
        break;
    
    case 0xFF629D:
      Serial.println("Vol +");
        break;
          irrecv.resume();
    
    case 0xFFA857:
      Serial.println("Vol -");
        break;
          irrecv.resume();
    
    case 0xFFE21D:
      Serial.println("Funcion/stop");
        break;
          irrecv.resume();
      
      case 0xFF02FD: 
        Serial.println("Play/Pause");
        digitalWrite(front1,HIGH);
        digitalWrite(back1,HIGH);
          delay(100);
        digitalWrite(front1,LOW);
        digitalWrite(back1,LOW);
          delay(100);
        digitalWrite(front1,HIGH);
        digitalWrite(back1,HIGH);
          delay(100);
        digitalWrite(front1,LOW);
        digitalWrite(back1,LOW);
          delay(100);
        digitalWrite(front1,HIGH);
        digitalWrite(back1,HIGH);
          delay(100);
        digitalWrite(front1,LOW);
        digitalWrite(back1,LOW);
          delay(100);
        digitalWrite(front2,HIGH);
        digitalWrite(back2,HIGH);
          delay(100);
        digitalWrite(front2,LOW);
        digitalWrite(back2,LOW);
          delay(100);
        digitalWrite(front2,HIGH);
        digitalWrite(back2,HIGH);
          delay(100);
        digitalWrite(front2,LOW);
        digitalWrite(back2,LOW);
          delay(100);
        digitalWrite(front2,HIGH);
        digitalWrite(back2,HIGH);
          delay(100);
        digitalWrite(front2,LOW);
        digitalWrite(back2,LOW);
          delay(100);
        break;
          irrecv.resume();
    
    case 0xFF22DD:
      Serial.println("Rewind");
        break;
          irrecv.resume();
    
    case 0xFFC23D:
      Serial.println("Fast forward");
        break;
          irrecv.resume();
    
    case 0xFF906F:
        Serial.println("UP");
        digitalWrite(front1,HIGH);
          delay(100);
        digitalWrite(front1, LOW);
          delay(100);
        digitalWrite(front1,HIGH);
          delay(100);
        digitalWrite(front1, LOW);
          delay(100);
        digitalWrite(front1,HIGH);
          delay(100);
        digitalWrite(front1, LOW);
          delay(100);
          
        digitalWrite(front2,HIGH);
          delay(100);
        digitalWrite(front2, LOW);
          delay(100);
        digitalWrite(front2,HIGH);
          delay(100);
        digitalWrite(front2, LOW);
          delay(100);
        digitalWrite(front2,HIGH);
          delay(100);
        digitalWrite(front2, LOW);
        break;
          irrecv.resume();
    
    case 0xFFE01F:
      Serial.println("DOWN");
        digitalWrite(back1,HIGH);
          delay(100);
        digitalWrite(back1, LOW);
          delay(100);
        digitalWrite(back1,HIGH);
          delay(100);
        digitalWrite(back1, LOW);
          delay(100);
        digitalWrite(back1,HIGH);
          delay(100);
        digitalWrite(back1, LOW);
          delay(100);
          
        digitalWrite(back2,HIGH);
          delay(100);
        digitalWrite(back2, LOW);
          delay(100);
        digitalWrite(back2,HIGH);
          delay(100);
        digitalWrite(back2, LOW);
          delay(100);
        digitalWrite(back2,HIGH);
          delay(100);
        digitalWrite(back2, LOW);
        break;
          irrecv.resume();
    
    case 0xFF9867:
      Serial.println("EQ");
        break;
          irrecv.resume();
    
    case 0xFFB04F:
      Serial.println("ST/Rept");
        break;
          irrecv.resume();
    
    case 0xFF30CF:
      Serial.println("1");
      digitalWrite(amber1,HIGH);
        delay(100);
      digitalWrite(amber1,LOW);
        delay(100);
      digitalWrite(amber1,HIGH);
        delay(100);
      digitalWrite(amber1,LOW);
        delay(100);
      digitalWrite(amber1,HIGH);
        delay(500);
      digitalWrite(amber2,HIGH);
        delay(500);
      digitalWrite(amber3,HIGH);
        delay(500);
      digitalWrite(amber4,HIGH);
        delay(500);
      digitalWrite(amber5,HIGH);
        delay(100);
      digitalWrite(amber5,LOW);
        delay(100);
      digitalWrite(amber5,HIGH);
        delay(100);
      digitalWrite(amber5,LOW);
        delay(100);
      digitalWrite(amber5,HIGH);
        delay(500);
      digitalWrite(amber1,LOW);
      digitalWrite(amber2,LOW);
      digitalWrite(amber3,LOW);
      digitalWrite(amber4,LOW);
      digitalWrite(amber5,LOW);
        delay(500);
        break;
          irrecv.resume();
    
    case 0xFF18E7:
      Serial.println("2");
      digitalWrite(amber5,HIGH);
        delay(100);
      digitalWrite(amber5,LOW);
        delay(100);
      digitalWrite(amber5,HIGH);
        delay(100);
      digitalWrite(amber5,LOW);
        delay(100);
      digitalWrite(amber5,HIGH);
        delay(500);
      digitalWrite(amber4,HIGH);
        delay(500);
      digitalWrite(amber3,HIGH);
        delay(500);
      digitalWrite(amber2,HIGH);
        delay(500);
      digitalWrite(amber1,HIGH);
        delay(100);
      digitalWrite(amber1,LOW);
        delay(100);
      digitalWrite(amber1,HIGH);
        delay(100);
      digitalWrite(amber1,LOW);
        delay(100);
      digitalWrite(amber1,HIGH);
        delay(500);
      digitalWrite(amber1,LOW);
      digitalWrite(amber2,LOW);
      digitalWrite(amber3,LOW);
      digitalWrite(amber4,LOW);
      digitalWrite(amber5,LOW);
        delay(500);
        break;
          irrecv.resume();
    
    case 0xFF7A85:
      Serial.println("3");
      digitalWrite(amber3,HIGH);
        delay(500);
      digitalWrite(amber2,HIGH);
      digitalWrite(amber4,HIGH);
        delay(500);
      digitalWrite(amber1,HIGH);
      digitalWrite(amber5,HIGH);
        delay(100);
      digitalWrite(amber1,LOW);
      digitalWrite(amber5,LOW);
        delay(100);
      digitalWrite(amber1,HIGH);
      digitalWrite(amber5,HIGH);
        delay(100);
      digitalWrite(amber1,LOW);
      digitalWrite(amber5,LOW);
        delay(100);
      digitalWrite(amber1,HIGH);
      digitalWrite(amber5,HIGH);
        delay(100);
      digitalWrite(amber1,LOW);
      digitalWrite(amber5,LOW);
        delay(100);
      digitalWrite(amber1,HIGH);
      digitalWrite(amber5,HIGH);  
        delay(500);      
      digitalWrite(amber1,LOW);
      digitalWrite(amber2,LOW);
      digitalWrite(amber3,LOW);
      digitalWrite(amber4,LOW);
      digitalWrite(amber5,LOW);
        delay(500);
        break;
          irrecv.resume();
    
    case 0xFF10EF:
      Serial.println("4");
        break;
          irrecv.resume();
    
    case 0xFF38C7:
      Serial.println("5");
        break;
          irrecv.resume();
    
    case 0xFF5AA5:
      Serial.println("6");
        break;
          irrecv.resume();
    
    case 0xFF42BD:
      Serial.println("7");
        break;
          irrecv.resume();
    
    case 0xFF4AB5:
      Serial.println("8");
        break;
          irrecv.resume();
    
    case 0xFF52AD:
      Serial.println("9");
        break;
          irrecv.resume();
    
    case 0xFF6897:
      Serial.println("0");
      break;
        irrecv.resume();
  }
  delay(100);
}

You are using a lot of delay(), which prevents the microcontroller from doing anything else. This will certainly prevent the 3 lights from working independently.

I tried to post the code directly into the text, but i exceeded the maximum character limit. and as far as the delays go, thats the most effective way ive found to make the lights flash in specific patterns.

d.raymond:
Hi, im extremely new to the arduino/C/C++ environment, I have been watching a lot of tutorials and i have gotten to a decent point, but im stuck with my current project(one of my first projects). the basic premise is i have 3 sets of lights(front, back, and amber director) and they all need to work independently and together(1,2, or 3 on at the same time) while being in there own sequences. i can get each sequence going individually but it either gets stuck on or it gets killed when i switch to a new pattern. im also trying to use a IR remote to control all of this.

any help would be greatly appreciated.

here is the code i have so far.

I have included a print line for all buttons on the remote but other than print what button they are for they have no purpose, but are open for further development.

I looked over your code and noticed the IR decodes you are looking for. For example, "case 0xFFA857:"

That looks like the NEC code format, and if it is you are leaving out part of the code. The above, for example, should be 0x00FFA857.

Those codes contain their own checksums in order to verify a correct reception. Taking, again, the example above, split it in half:

0x00FF and 0xA857.

Notice that 0x00 + 0xFF = 0xFF.
Likewise, 0xA8 + 0x57 = 0xFF.

BEFORE you send your received IR code to the switch/case section, you should test the top and bottom half to make sure the checksums check out (and ignore the code if one or the other fails).

Also, realize that a lot of IR remotes (especially those sending the NEC style codes) send the keycode once, then a "repeat" code over and over again until the button is released. You need to either find and reject the repeat codes, or if you want them, decode them as well.

Most important thing though is to checksum your received data before using it. A code can get garbled, but have the right number of bits and timings to be recognized, but the resulting code will be garbage. The checksum should help you with that.

Good luck!

I dont have a problem with the board receiving the code, and the repeat code is FFFFFF, its been excluded as to not interfere. the only problem im having is putting turning on multiple sections at a time.

d.raymond:
I dont have a problem with the board receiving the code, and the repeat code is FFFFFF, its been excluded as to not interfere. the only problem im having is putting turning on multiple sections at a time.

OK, I think I know what you want. Look at this code... it has all your lights defined by name and pin number, then each time you receive an IR code, the list is scanned for a match and that particular light is turned on. A SPECIAL code turns all lights off.

Note that the "light_list" and "ir_codes" arrays have to contain the same number of entries... obviously since why look for a code if there's no use for it (not to mention that a size mismatch could crash the Arduino):

#include <IRremote.h>

define RECV_PIN 13

#define front1  4
#define front2  5
#define back1   6
#define back2   7
#define amber1  8
#define amber2  9
#define amber3 10
#define amber4 11
#define amber5 12

const int light_list[] = {
	front1,
	front2,
	back1,
	back2,
	amber1,
	amber2,
	amber3,
	amber4,
	amber5,
};

// note: codes in the same order as the lights they control
// for instance, "0x00FFA25D" controls "front1" and
// "0x00FF629D" controls "front2, etc...
const uint32_t ir_codes[] = {
	0x00FFA25D,
	0x00FF629D,
	...etc...
};

const uint8_t list_size = (sizeof (light_list) / sizeof (*light_list));

IRrecv irrecv(RECV_PIN);

void setup (void)
{
	Serial.begin(9600);
	irrecv.enableIRIn(); // Start the receiver

	uint8_t n;

	for (n = 0; n < list_size; n++) {
		// set all light control pins as outputs
		pinMode (light_list[n], OUTPUT);
	}
}

void loop (void)
{
	// receive IR code
	// special code means "all lights off"
	if (results.value == 0x01234567) { // SPECIAL code
		for (n = 0; n < list_size; n++) {
			digitalWrite (lights_list[n], LOW);
		}
	}
	// otherwise, scan for a particular light code
	for (n = 0; n < list_size; n++) {
		if (results.value == ir_codes[n]) { // code matchs, turn on this lamp
			digitalWrite (lights_list[n], HIGH);
		}
	}
}

This isn't complete, but it should give you enough to go on.

Note that if you want to see the results in the serial output, just make another for "n=0; n<list_size;n++" loop for that.
Hope this helps.

That works to just turn the light on and off, but each set of lights has a flash pattern on a loop, how do i integrate that into it?

d.raymond:
That works to just turn the light on and off, but each set of lights has a flash pattern on a loop, how do i integrate that into it?

I don't know what kind of "flash pattern" you want. I showed you how to turn on or off any lamp inside a single loop.

Surely you can take it from there?

i dont need 1 button to control 1 light, i need 1 button to control up to 5 lights in a pattern. and i have that, what i dont have is the ability to push 2 buttons and have them both on at the same time.

what i dont have is the ability to push 2 buttons and have them both on at the same time.

You'll need to explain what having two buttons on at the same time means.

You'll need to post the code that doesn't let you have two "buttons on" at the same time.

for instance, the up button turns the front lights on in a flash pattern:

case 0xFF906F:
Serial.println("UP");
digitalWrite(front1,HIGH);
delay(100);
digitalWrite(front1, LOW);
delay(100);
digitalWrite(front1,HIGH);
delay(100);
digitalWrite(front1, LOW);
delay(100);
digitalWrite(front1,HIGH);
delay(100);
digitalWrite(front1, LOW);
delay(100);

digitalWrite(front2,HIGH);
delay(100);
digitalWrite(front2, LOW);
delay(100);
digitalWrite(front2,HIGH);
delay(100);
digitalWrite(front2, LOW);
delay(100);
digitalWrite(front2,HIGH);
delay(100);
digitalWrite(front2, LOW);
break;
irrecv.resume();

then i can turn the director on with the 1 button and it goes left to right:

case 0xFF30CF:
Serial.println("1");
digitalWrite(amber1,HIGH);
delay(100);
digitalWrite(amber1,LOW);
delay(100);
digitalWrite(amber1,HIGH);
delay(100);
digitalWrite(amber1,LOW);
delay(100);
digitalWrite(amber1,HIGH);
delay(500);
digitalWrite(amber2,HIGH);
delay(500);
digitalWrite(amber3,HIGH);
delay(500);
digitalWrite(amber4,HIGH);
delay(500);
digitalWrite(amber5,HIGH);
delay(100);
digitalWrite(amber5,LOW);
delay(100);
digitalWrite(amber5,HIGH);
delay(100);
digitalWrite(amber5,LOW);
delay(100);
digitalWrite(amber5,HIGH);
delay(500);
digitalWrite(amber1,LOW);
digitalWrite(amber2,LOW);
digitalWrite(amber3,LOW);
digitalWrite(amber4,LOW);
digitalWrite(amber5,LOW);
delay(500);
break;
irrecv.resume();

but once i push the "1" button it cancels out the "up" button command. that is what im trying to solve. i do not want a new button push to cancel out the other.

but once i push the "1" button it cancels out the "up" button command.

No, it, whatever it is, does NOT. It is YOU that wrote the code that says "Hey, when I get a new command, forget EVERYTHING that I ever knew.". If that is not what you want to do, don't write the code that way.