Noob at work

Hi All,

I am doing my first arduino project which is to use an IR remote to control an RC car I am building. At the moment I am testing to see if I can use an IR remote to send power to an different LEDs while I hold down one of the buttons on the remote. I will later replace the LEDs with motors but for now am just testing.

My problem is that whenever I press a button on the remote - the LED stays on and does not go off when I release the button and in addition when I press a button to light up one LED it stays on and nothing happens when I press a different button.

Any advice on code/approach will be a big help!

Thanks

#include "IRremote.h"
#include "IR.h"
#define LED1 3
#define LED2 5
#define LED3 9
#define LED4 10

IRrecv irrecv(RECEIVER);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'

void setup() {
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode");
  irrecv.enableIRIn();
  pinMode (LED1, OUTPUT);
  pinMode (LED2, OUTPUT);
  pinMode (LED3, OUTPUT);
  pinMode (LED4, OUTPUT);
}

void loop()
{
  if (irrecv.decode(&results))
  {
      while (keyValue[0] == results.value)
      {
        digitalWrite (LED1, HIGH);
      }
      while (keyValue[1] == results.value)
      {
        digitalWrite (LED2, HIGH);
      }
      while (keyValue[2] == results.value) 
      {
        digitalWrite (LED3, HIGH);
        }
      while (keyValue[3] == results.value)
      {
        digitalWrite (LED4, HIGH);
      }
   irrecv.resume();
    }
  }

If you want LEDs to switch off you're going to need to write a LOW to them at some point. When do you think would be a good time to do that?

Where did you get IR.h from and what does it contain? How do you expect to tell when remote button is released. It sends a code when you press it but what about releasing it?

Steve

Hi

So this is the IR.h code thats being included. What I want is the LOW to be written when the button is released.

I dont know if the remote sends code when the button is released but what I had in my head was that the LED should only be on whilst a signal is being received and when nothing is being receieved (as the button is not pressed) it should be written to LOW.

Do you have an recommendations on how I could do this.

Just as an FYI the code I have used is one that came with a set I bought and have edited so I'm aware it may not be the best method

Thanks in advance

#ifndef _IR_H
#define _IR_H

#define RECEIVER 11

#define KEY_VOL_ADD (0xFF629D)
#define KEY_VOL_DE (0xFFA857)
#define KEY_7 (0xFF42BD)
#define KEY_9 (0xFF52AD)
#define KEY_NUM 21
#define REPEAT 22

unsigned long keyValue[]={KEY_VOL_ADD,KEY_VOL_DE,KEY_7,KEY_9};

char keyBuf[][10]={"VOL+","VOL-","7","9"};
#endif

If you would be prepared to make your own transmitter using another Arduino you could have much greater control using a pair of nRF24L01+ transceivers for the wireless link. For example you could use a potentiometer to allow speed control.

...R
Simple nRF24L01+ Tutorial

You will probably find that when you hold the button pressed you get the original button code followed by lots of repeat codes (22). You'll need to recognise them and then when you haven't seen one for some time (probably a second or so) THEN you can switch the LED off.

Steve

Do you have any advice on how I could do that? - FYI I am open to rewriting the code completely.

What Im aiming for is the a specfic LED to be on whilst the button is held down (I will later replace these LED's with motors). Is there a way to constantly check the IR reciever to see if a signal is being sent and then use maybe an if statement to send power to the LED whilst the condition is satisfied?

Thanks

The first thing to do is to add a Serial.print to see what codes you are getting. Your IR.h has a #define for "repeat" codes but they are not included in keyValue[]. I don't know anything about your transmitter so I don't know if that's just lazy programming or if the missing codes KEY_NUM and REPEAT are never transmitted. So something like

  if (irrecv.decode(&results))
  {
  Serial.println(results.value);   // add this line

will tell you a lot about what codes are transmitted and when.

Steve

Okay so I think I am getting closer to the end result now. I have the LED turning on whenever I hold a button down - however it lights up when I hold down any button, not the just particular button one I want.

The second if statement says "if tmpValue = 21165" make the LED light up. (21165 is the code for which the IR receiver receives when I press the number 9 on my remote). However, when I hold down any button on the remote the LED still comes on. e.g. number 8 is code 19125 and the LED still comes on when I press it.

I have included a screenshot of my serial monitor so you can see whats happening. I have also noticed when I press a button once on my remote, my serial monitor displays the button code (e.g. 21165 for button 9) but when I hold down any button it prints -1 to my serial monitor.

Any help will be appreciated.

Main code and IR.h both below

#include "IRremote.h"
#include "IR.h"
#define LED1 3

IRrecv irrecv(RECEIVER);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'

void setup() {
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode");
  irrecv.enableIRIn();
  pinMode (LED1, OUTPUT);
}

void loop()
{
  digitalWrite (LED1, LOW);
  int tmpValue;
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    for (int i = 0; i < 23; i++)
    {
      if (keyValue[i] == results.value)
      {
        tmpValue = results.value;
        Serial.println (tmpValue);
        if (tmpValue == 21165); // 21165 is the code on the control for a patricular button press e.g. "Power on/off"
        {
          digitalWrite (LED1, HIGH);
        }
      }
    }
    irrecv.resume(); // receive the next value
  }
}

IR.h

#ifndef _IR_H
#define _IR_H

#define RECEIVER 11

#define KEY_POWER (0xFFA25D)
#define KEY_FUNC_STOP (0xFFE21D)
#define KEY_VOL_ADD (0xFF629D)
#define KEY_FAST_BACK (0xFF22DD)
#define KEY_PAUSE (0xFF02FD)
#define KEY_FAST_FORWARD (0xFFC23D)
#define KEY_DOWN (0xFFE01F)
#define KEY_VOL_DE (0xFFA857)
#define KEY_UP (0xFF906F)
#define KEY_EQ (0xFF9867)
#define KEY_ST_REPT (0xFFB04F)
#define KEY_0 (0xFF6897)
#define KEY_1 (0xFF30CF)
#define KEY_2 (0xFF18E7)
#define KEY_3 (0xFF7A85)
#define KEY_4 (0xFF10EF)
#define KEY_5 (0xFF38C7)
#define KEY_6 (0xFF5AA5)
#define KEY_7 (0xFF42BD)
#define KEY_8 (0xFF4AB5)
#define KEY_9 (0xFF52AD)
#define KEY_REPEAT (0xFFFFFFFF)
#define KEY_NUM 21
#define REPEAT 22

unsigned long keyValue[]={KEY_POWER,KEY_FUNC_STOP,KEY_VOL_ADD,KEY_FAST_BACK,KEY_PAUSE,KEY_FAST_FORWARD,
                KEY_DOWN,KEY_VOL_DE,KEY_UP,KEY_EQ,KEY_ST_REPT,KEY_0,KEY_1,KEY_2,KEY_3,KEY_4,KEY_5,
                KEY_6,KEY_7,KEY_8,KEY_9,KEY_REPEAT};

char keyBuf[][10]={"POWER","FUNC/STOP","VOL+","FAST BACK","PAUSE","FAST FORWARD","DOWN","VOL-",
                  "UP","EQ","ST/REPT","0","1","2","3","4","5","6","7","8","9"};
#endif

printing -1 is the same as the unsigned long value 0xFFFFFFFF which is REPEAT_KEY. You have keep track of what the previous key actual was since the remote is just telling you it is a repeat of whatever I just sent, not repeat of a specific key

Okay thats useful to know -I can see where it comes from. My question is why would -1 mean the repeat key - surely it should be 22 as Key_Repeat is the last one in the unsigned long array?

Also, I am still confused as to why any button lights up the LED as I have an if statement in which the condition is if (tmpValue =21165). It still lights up the LED when any button is pressed which is confusing me...

Thanks

paul_g19:
Okay thats useful to know -I can see where it comes from. My question is why would -1 mean the repeat key - surely it should be 22 as Key_Repeat is the last one in the unsigned long array?

Probably because your IR remote doesn't actually transmit the codes in that IR.h file. Where did that file come from and have you actually checked if all the codes are what is actually transmitted?

Also wouldn't it make more sense for your if statements to use result.value directly and compare it against the declared names in IR.h e.g. KEY_POWER, KEY_9 etc? Casting results value into an int and then comparing against a decimal value just confuses things.

Steve

        if (tmpValue == 21165); // 21165 is the code on the control for a patricular button press e.g. "Power on/off"
        {
          digitalWrite (LED1, HIGH);
        }

The semicolon after that if condition is an empty statement and concludes the if block.
The following block with digitalWrite is unconditional.

Also - try removing KEY_REPEAT from the values that you test for. Since its last in the array you could just reduce your for loop limit from 23 to 22.
But why not just test for the specific key code you need?

paul_g19:
Okay thats useful to know -I can see where it comes from. My question is why would -1 mean the repeat key - surely it should be 22 as Key_Repeat is the last one in the unsigned long array?

Also, I am still confused as to why any button lights up the LED as I have an if statement in which the condition is if (tmpValue =21165). It still lights up the LED when any button is pressed which is confusing me...

Thanks

You are not testing the index into the array, you are testing the values. REPEAT_KEY is defined as 0xFFFFFFFF. It does not matter the order within the array since you looping through all the keys in the array.

printing -1 is the same as the unsigned long value 0xFFFFFFFF which is REPEAT_KEY. You have keep track of what the previous key actual was since the remote is just telling you it is a repeat of whatever I just sent, not repeat of a specific key

Do you have any idea how I can keep track of what the previous value is. The issue I have is that the control sends a -1 whenever any button is being held down.

I need to have LED1 light up when button 9 is being pressed and held. At the moment it lights up when I press 9 but I am not sure how to tell the Arduino to also light up when 9 is held down, as the signal received changes to -1 when 9 is held. this is made trickier by the fact that a -1 is received when any button is held.

The vision is to have a specific LED light up when I press and hold a specific button on the control. I will after replace the LED's with motors to power the wheels

Do you have any advice on how I could rewrite my code to achieve this? - any advice on new functions will be helpful - I was thinking to maybe use a Switch(case)

Thanks

My current code below

#include "IRremote.h"
#include "IR.h"
#define LED1 3

IRrecv irrecv(RECEIVER);     // create instance of 'irrecv'
decode_results results;      // create instance of 'decode_results'

void setup() {
  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode");
  irrecv.enableIRIn();
  pinMode (LED1, OUTPUT);
}

void loop()
{
  digitalWrite (LED1, LOW);
  int tmpValue;
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    for (int i = 0; i < 22; i++)
    {
      if (keyValue[i] == results.value)
      {
        tmpValue = results.value; 
        if (tmpValue == 21165) // 21165 is the code on the control for a patricular button press e.g. "Power on/off"
        {
         digitalWrite (LED1, HIGH);
         Serial.println(tmpValue);
        }
       }
   }
    irrecv.resume(); // receive the next value
  }
}

IR.h ( I plan to also remove all of the irrelevant code from this block below after)

#ifndef _IR_H
#define _IR_H

#define RECEIVER 11

#define KEY_POWER (0xFFA25D)
#define KEY_FUNC_STOP (0xFFE21D)
#define KEY_VOL_ADD (0xFF629D)
#define KEY_FAST_BACK (0xFF22DD)
#define KEY_PAUSE (0xFF02FD)
#define KEY_FAST_FORWARD (0xFFC23D)
#define KEY_DOWN (0xFFE01F)
#define KEY_VOL_DE (0xFFA857)
#define KEY_UP (0xFF906F)
#define KEY_EQ (0xFF9867)
#define KEY_ST_REPT (0xFFB04F)
#define KEY_0 (0xFF6897)
#define KEY_1 (0xFF30CF)
#define KEY_2 (0xFF18E7)
#define KEY_3 (0xFF7A85)
#define KEY_4 (0xFF10EF)
#define KEY_5 (0xFF38C7)
#define KEY_6 (0xFF5AA5)
#define KEY_7 (0xFF42BD)
#define KEY_8 (0xFF4AB5)
#define KEY_9 (0xFF52AD)
#define KEY_REPEAT (0xFFFFFFFF)
#define KEY_NUM 21


unsigned long keyValue[]={KEY_POWER,KEY_FUNC_STOP,KEY_VOL_ADD,KEY_FAST_BACK,KEY_PAUSE,KEY_FAST_FORWARD,
                KEY_DOWN,KEY_VOL_DE,KEY_UP,KEY_EQ,KEY_ST_REPT,KEY_0,KEY_1,KEY_2,KEY_3,KEY_4,KEY_5,
                KEY_6,KEY_7,KEY_8,KEY_9,KEY_REPEAT};

char keyBuf[][10]={"POWER","FUNC/STOP","VOL+","FAST BACK","PAUSE","FAST FORWARD","DOWN","VOL-",
                  "UP","EQ","ST/REPT","0","1","2","3","4","5","6","7","8","9"};
#endif