need help in my code

hello every one,

I am new to Arduino and I really need your help to finish my project.

my project is controlling 4 led light.

I have finish the programming but i need to add delay time for light in order to turn off automatically and

add restarting as we.

the code is below

//////<

#include <IRremote.h>
#define first_key 0xF7B04F
#define second_key 0xF7A05F
#define third_key 0x7609F
#define fourth_key 0xF710EF
int receiver_pin = 11;
int first_led_pin = 4;
int second_led_pin = 5;
int third_led_pin = 6;
int fourth_led_pin = 7;
int led[]{0,0,0,0,};
IRrecv receiver(receiver_pin);
decode_results output;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
receiver.enableIRIn();
pinMode(first_led_pin, OUTPUT);
pinMode(second_led_pin, OUTPUT);
pinMode(third_led_pin, OUTPUT);
pinMode(fourth_led_pin, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
if (receiver.decode(&output)){
unsigned int value = output.value;
switch(value) {
case first_key:
if(led[1]==1){
digitalWrite(first_led_pin,HIGH);
led[1] = 0;
}
else
{
digitalWrite(second_led_pin,LOW);
digitalWrite(third_led_pin,LOW);
digitalWrite(fourth_led_pin,LOW);
led[1] = 1;
}
break;
case second_key:
if(led[2]==1)
{
digitalWrite(second_led_pin,HIGH);
led[2] = 0;

}
else
{
digitalWrite(first_led_pin,LOW);
digitalWrite(third_led_pin,LOW);
digitalWrite(fourth_led_pin,LOW);
led[2] = 1;
}
break;
case third_key:
if(led[3] == 1)
{
digitalWrite(third_led_pin, HIGH);
led[3] = 0;
}
else
{
digitalWrite(first_led_pin,LOW);
digitalWrite(second_led_pin,LOW);
digitalWrite(fourth_led_pin,LOW);
led[3] = 1;
}
break;
case fourth_key:

if(led[4] == 1) {
digitalWrite(fourth_led_pin, HIGH);
led[4] = 0;
}
else
{
digitalWrite(first_led_pin,LOW);
digitalWrite(second_led_pin,LOW);
digitalWrite(third_led_pin,LOW);
led[4] = 1;
}
break;
}
Serial.println(value);
receiver.resume();
}
}
//////////>

IR_remote_control_project.ino (1.61 KB)

If you indent your code like this it makes it much easier to follow

#include <IRremote.h>
#define first_key 0xF7B04F
#define second_key 0xF7A05F
#define third_key 0x7609F
#define fourth_key 0xF710EF
int receiver_pin = 11;
int first_led_pin = 4;
int second_led_pin = 5;
int third_led_pin = 6;
int fourth_led_pin = 7;
int led[]{0,0,0,0,};
IRrecv receiver(receiver_pin);
decode_results output;

void setup() {
        // put your setup code here, to run once:
    Serial.begin(9600);
    receiver.enableIRIn();
    pinMode(first_led_pin, OUTPUT);
    pinMode(second_led_pin, OUTPUT);
    pinMode(third_led_pin, OUTPUT);
    pinMode(fourth_led_pin, OUTPUT);
}

void loop() {
        // put your main code here, to run repeatedly:
    if (receiver.decode(&output)){
        unsigned int value = output.value;
        switch(value) {
            case first_key:
                if(led[1]==1){
                    digitalWrite(first_led_pin,HIGH);
                    led[1] = 0;
                }
                else
                {
                    digitalWrite(second_led_pin,LOW);
                    digitalWrite(third_led_pin,LOW);
                    digitalWrite(fourth_led_pin,LOW);
                    led[1] = 1;
                }
                break;
            case second_key:
                if(led[2]==1)
                {
                    digitalWrite(second_led_pin,HIGH);
                    led[2] = 0;
                    
                }
                else
                {
                    digitalWrite(first_led_pin,LOW);
                    digitalWrite(third_led_pin,LOW);
                    digitalWrite(fourth_led_pin,LOW);
                    led[2] = 1;
                }
                break;
            case third_key:
                if(led[3] == 1)
                {
                    digitalWrite(third_led_pin, HIGH);
                    led[3] = 0;
                }
                else
                {
                    digitalWrite(first_led_pin,LOW);
                    digitalWrite(second_led_pin,LOW);
                    digitalWrite(fourth_led_pin,LOW);
                    led[3] = 1;
            }
                break;
            case fourth_key:

                if(led[4] == 1) {
                    digitalWrite(fourth_led_pin, HIGH);
                    led[4] = 0;
                 }
                 else
                 {
                 digitalWrite(first_led_pin,LOW);
                    digitalWrite(second_led_pin,LOW);
                    digitalWrite(third_led_pin,LOW);
                 led[4] = 1; 
                }
                break;
        }
        Serial.println(value);
        receiver.resume();
    }
}

Use the Autoformat tool

You need to explain what the existing code does and where in that code you want to have the auto-off for the LEDS.

The demo Several Things at a Time illustrates the use of millis() to manage timing - it should give you ideas about how to achieve what you want.

...R

        if (led[4] == 1)

Fix this problem first. led[4] is the fifth element of the led array but you only declared it as a 4 element array. The elements of the array are numbered 0 to 3 not 1 to 4

Thanxxxxx so much sir,

the code work well, however, while I need when i push button is turning on led light on for 4

second.But, other led lights off. can,t be on...

Robin2:
If you indent your code like this it makes it much easier to follow

#include <IRremote.h>

#define first_key 0xF7B04F
#define second_key 0xF7A05F
#define third_key 0x7609F
#define fourth_key 0xF710EF
int receiver_pin = 11;
int first_led_pin = 4;
int second_led_pin = 5;
int third_led_pin = 6;
int fourth_led_pin = 7;
int led[]{0,0,0,0,};
IRrecv receiver(receiver_pin);
decode_results output;

void setup() {
        // put your setup code here, to run once:
    Serial.begin(9600);
    receiver.enableIRIn();
    pinMode(first_led_pin, OUTPUT);
    pinMode(second_led_pin, OUTPUT);
    pinMode(third_led_pin, OUTPUT);
    pinMode(fourth_led_pin, OUTPUT);
}

void loop() {
        // put your main code here, to run repeatedly:
    if (receiver.decode(&output)){
        unsigned int value = output.value;
        switch(value) {
            case first_key:
                if(led[1]==1){
                    digitalWrite(first_led_pin,HIGH);
                    led[1] = 0;
                }
                else
                {
                    digitalWrite(second_led_pin,LOW);
                    digitalWrite(third_led_pin,LOW);
                    digitalWrite(fourth_led_pin,LOW);
                    led[1] = 1;
                }
                break;
            case second_key:
                if(led[2]==1)
                {
                    digitalWrite(second_led_pin,HIGH);
                    led[2] = 0;
                   
                }
                else
                {
                    digitalWrite(first_led_pin,LOW);
                    digitalWrite(third_led_pin,LOW);
                    digitalWrite(fourth_led_pin,LOW);
                    led[2] = 1;
                }
                break;
            case third_key:
                if(led[3] == 1)
                {
                    digitalWrite(third_led_pin, HIGH);
                    led[3] = 0;
                }
                else
                {
                    digitalWrite(first_led_pin,LOW);
                    digitalWrite(second_led_pin,LOW);
                    digitalWrite(fourth_led_pin,LOW);
                    led[3] = 1;
            }
                break;
            case fourth_key:

if(led[4] == 1) {
                    digitalWrite(fourth_led_pin, HIGH);
                    led[4] = 0;
                }
                else
                {
                digitalWrite(first_led_pin,LOW);
                    digitalWrite(second_led_pin,LOW);
                    digitalWrite(third_led_pin,LOW);
                led[4] = 1;
                }
                break;
        }
        Serial.println(value);
        receiver.resume();
    }
}



Use the Autoformat tool

You need to explain what the existing code does and where in that code you want to have the auto-off for the LEDS.

The demo [Several Things at a Time](http://forum.arduino.cc/index.php?topic=223286.0) illustrates the use of millis() to manage timing - it should give you ideas about how to achieve what you want.

...R

Thanxxx sir,

The code use for competition game. the game is any one pressing the button to turn led light. the led light is going to turn on for 4 second but other led light is off until 4 second finish....

Jass:
the led light is going to turn on for 4 second but other led light is off until 4 second finish....

I have not seen anything in your code that causes anything to happen for 4 seconds (or any other interval).

You need to explain what you are trying to do more clearly and more extensively.

...R

the led light is going to turn on for 4 second but other led light is off until 4 second finish....

Many people have problems when they use the delay() function and find that it stops anything happening for the delay() period. In your case, however, that could be used to advantage.

Turn on the required LED
delay() for 4 seconds
turn off all LEDs

Unless, of course, something else is required to happen during that 4 seconds, in which case a different method of implementing the 4 second "delay" will be needed.

#include <IRremote.h>
#define first_key 0xF7B04F
#define second_key 0xF7A05F
#define third_key 0x7609F
#define fourth_key 0xF710EF
int receiver_pin = 11;
int first_led_pin = 4;
int second_led_pin = 5;
int third_led_pin = 6;
int fourth_led_pin = 7;
int led[]{0,0,0,0,};
IRrecv receiver(receiver_pin);
decode_results output;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
receiver.enableIRIn();
pinMode(first_led_pin, OUTPUT);
pinMode(second_led_pin, OUTPUT);
pinMode(third_led_pin, OUTPUT);
pinMode(fourth_led_pin, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
if (receiver.decode(&output)){
unsigned int value = output.value;
switch(value) {
case first_key:
if(led[1]==1){
digitalWrite(first_led_pin,HIGH);
led[1] = 0;
}
else
{
digitalWrite(second_led_pin,LOW);
digitalWrite(third_led_pin,LOW);
digitalWrite(fourth_led_pin,LOW);
led[1] = 1;
delay(400);
}
break;
case second_key:
if(led[2]==1)
{
digitalWrite(second_led_pin,HIGH);
led[2] = 0;

}
else
{
digitalWrite(first_led_pin,LOW);
digitalWrite(third_led_pin,LOW);
digitalWrite(fourth_led_pin,LOW);
led[2] = 1;
delay(400);
}
break;
case third_key:
if(led[3] == 1)
{
digitalWrite(third_led_pin, HIGH);
led[3] = 0;
}
else
{
digitalWrite(first_led_pin,LOW);
digitalWrite(second_led_pin,LOW);
digitalWrite(fourth_led_pin,LOW);
led[3] = 1;
delay(400);
}
break;
case fourth_key:

if(led[4] == 1) {
digitalWrite(fourth_led_pin, HIGH);
led[4] = 0;
}
else
{
digitalWrite(first_led_pin,LOW);
digitalWrite(second_led_pin,LOW);
digitalWrite(third_led_pin,LOW);
led[4] = 1;
delay(400);
}
break;
}
Serial.println(value);
receiver.resume();
}
}

Sir, I have project using IR sensor. I wanna control the led light. the led light only turn 4 second however, the other 3 led lights can't work in same time.
( I mentioned delay in code) but it dose not work for me)
sincerely
Thank you

  if(led[4] == 1) {A four element array has no element with the index four.
This has been mentioned.

Please remember to use code tags when posting code

int led[]{0,0,0,0,}; What did the compiler say about that?

Suppose your job is to go around the building, turning off any light that has been on more than 4 hours. How would YOU go about it?

Not turning a light off because you didn't turn it on is NOT acceptable.

Can't you see that turning OFF the lights has NOTHING to do with turning them on, except that turning them on defines WHEN to turn them off.

UKHeliBob:
Many people have problems when they use the delay() function and find that it stops anything happening for the delay() period. In your case, however, that could be used to advantage.

Turn on the required LED
delay() for 4 seconds
turn off all LEDs

Unless, of course, something else is required to happen during that 4 seconds, in which case a different method of implementing the 4 second "delay" will be needed.

firstly, I would like to thank you for your effort. Actually I need the led turn on 4 second when press the button( the button is remote )
mean when I press the button work 4 second then turn of.

Jass:
firstly, I would like to thank you for your effort. Actually I need the led turn on 4 second when press the button( the button is remote )
mean when I press the button work 4 second then turn of.

Then delay() will do what you want

AWOL:

int led[]{0,0,0,0,};

What did the compiler say about that?

nothing , work well...

Jass:
nothing , work well...

Really ?

UKHeliBob:
Really ?

see what i am getting after compiling

int led[]{0,0,0,0,};

void setup()
{
}

void loop()
{
}

This is what I got

sketch_oct27c:1: error: function definition does not declare parameters

UKHeliBob:
Then delay() will do what you want

The delay just four the light to be on. while the led light on, the others can't be on at same time. then after

finishing four second .the all the light should be off , waiting any one presses the ir remote control.

hope u understand sir

thank you

UKHeliBob:

int led[]{0,0,0,0,};

void setup()
{
}

void loop()
{
}




This is what I got



sketch_oct27c:1: error: function definition does not declare parameters

Sir,how to declare parameter?

The question is not how to declare a parameter, but how to initialise an array.