Go Down

Topic: iRremote keep and hold press on button (Read 374 times) previous topic - next topic

mansouri

Jan 08, 2018, 11:17 am Last Edit: Jan 10, 2018, 09:21 am by mansouri
high huys i am starting to work with iRremote on arduino i started to read the signals from ir led
and use them to turn on or off a led but i didn't figure how to keep the led on while i am pressing the button and goes off when i release it this is the code :

Code: [Select]

#include <IRremote.h>
#include <IRremoteInt.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;

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

void loop()
{
 if (irrecv.decode(&results))
   {
    Serial.println(results.value);
    irrecv.resume(); // Receive the next value
   }

if (results.value==16753245)


  digitalWrite(13,HIGH);

else(digitalWrite(13,LOW));



delay(500);
}

slipstick

#1
Jan 08, 2018, 01:47 pm Last Edit: Jan 08, 2018, 01:48 pm by slipstick
So what happen now? The LED comes but then goes straight off again?

Have you checked exactly what codes are coming from whatever remote control you're using? Many of them send a repeat code (different from the button code) when a button is held down continually. If you're not checking for that you'll never know when the button is held down and when it's released.

Plus with that delay(500) in there you're only checking every 1/2 second so there's a good chance of you missing any changes in the buttons.

Steve

mansouri

the led is on when i press the up key and its off when i press any other key
butt i didn't fint out the proper way to chek for the repeat key i used many ways but i didnt work 

slipstick

Your Serial.print should show exactly what codes are being received when each button is pressed or held. What does it show?

Steve

mansouri

you didn't get me i know the value of the repeat key and any value from i wont from the remote but i didn't find a solution on how to use the hold function 

slipstick

It's not exactly difficult. When you see any key other than the repeat key save it in a variable. Use the variable in your if (it's the up key) statement. Then if you then see a repeat key don't change the variable so it will seem like the previous key had turned up again.

Give it a try and if it doesn't immediately work post your code and we can help you to get it working.

Steve

mansouri

this the code but the led stays on forever i tried to type somthing to the serial inside the while loop and its in the serial monitor with no stop i dont know why the code is stuck in the while loop
Code: [Select]

#define up 16753245
#define dwn 16749165
#define right 16756815
#define left 16736925
#define rep 4294967295



#include <IRremote.h>


int RECV_PIN = 11;
long int x;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn();
}

void loop()
{

if(irrecv.decode(&results))
 {
    if(results.value != rep){ x=results.value;}
   
    while(x==up)
     {
      digitalWrite(13,1);
     
     }
    digitalWrite(13,0);

    irrecv.resume();

 }
   
 

slipstick

#7
Jan 12, 2018, 08:02 pm Last Edit: Jan 12, 2018, 08:07 pm by slipstick
You're getting closer but think about this:
Code: [Select]

    while(x==up)
     {
      digitalWrite(13,1);
     
     }

Is there anything in that while loop that changes x so the loop can end ?

You don't need a while loop. Try changing it to a simple if/else and let loop() handle the looping.

Steve

mansouri

 i tried this modification and it worked only after i addded a the  delay when i started with a small value i can see the led blinking very fast and when i increase the delay the blinking slows down and it only kept lighting when i put a 100 although i can notice that its not a continuos on i assume it has somthing with ir frequency 
Code: [Select]

#define up 16753245
#define dwn 16749165
#define right 16756815
#define left 16736925
#define rep 4294967295



#include <IRremote.h>


int RECV_PIN = 11;
long int x;
IRrecv irrecv(RECV_PIN);
decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn();
}

void loop()
{

if(irrecv.decode(&results))
 {
    if(results.value != rep){ x=results.value;}
    Serial.println(results.value);
    Serial.println(x);
   

if (x==up){digitalWrite(13,1);
}


    irrecv.resume();
delay(100) ;
 }
     digitalWrite(13,0);
   

mansouri


wildbill

It's flashing because you unconditionally turn it off right after you turn it on. Larger delays let you see that. Try this instead:
Code: [Select]

if (x==up)
  {
  digitalWrite(13,1);
  }
else
  {
  digitalWrite(13,0);
  }

and get rid of the digitalWrite(13,0); further down.

slipstick

As wildbill says needing the delay is because of where you've put the digitalWrite() to switch the LED off. If you use his code it will work better but the LED will stay on until you press a different key.

You've actually chosen one of the more interesting things that you can do with a basic IR remote. The problem is finding out when you've released a key. IR remotes don't send key pressed/key released codes. When you release the key it just stops sending anything. So you have to get the receiver to time out when it hasn't seen a code for a while. That means you need to remember when you last received any code and if you haven't seen another code arrive in a certain time then you want to switch the LED off.

Basically you need to use millis() to remember when you've received a code. Then you can do something like " if no code has arrived and some time has passed then switch LED off". You'll have to sort out a timeout value so it's a bit more than the repeat time between code sends when the key is being held down.

When I get time to play with my Arduinos I can test some code but it won't be for at least a couple of days.

Steve


mansouri

#12
Jan 16, 2018, 09:51 am Last Edit: Jan 16, 2018, 02:31 pm by mansouri
thank you guys for all the help but i was thinking that if(irrecv.decode(&results))
means if i i am reiceiving signals from the remote so inside of it i will put what i want to do with a specific button press exapmle button up pressed  light the led and outside that (if) means if i am not receivieng a code  so i thought puting a statement there will be executed  if i am not pressing any buttons so when i am not pressing any buttons it will turn off the led .
but then i read somthing about ir signals and i understanded that they are sending pulses not continuos signals so i knew that the  led will still be blinking even if i keep pressing but in very fast way.     

Go Up