Pages: 1 [2]   Go Down
Author Topic: Turning led on and off remotelly  (Read 2210 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


This is best I can make. The led turns on and off in intervals, only the on interval is much longer then off. I'm quite sure that the problem is speed, because state in between two consecutive 0xF is the same as the state when button is released. I think it could be solved with some timers included and to tell the arduino that it should turn led off only if it has passed enough time from last command, and to reset that timer when program receives 0xF again. But i don't know how to include timers in whole story.

Here is the program i've build so far using all your recommendations. Thanks for solutions so far  smiley-mr-green

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

unsigned long value;


void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(13,OUTPUT); //pin for driving the led
}

void loop() {
  static boolean key_pressed;
  int k;
  results.value = 0;
  if (irrecv.decode(&results)) {
     Serial.println(results.value, HEX);
   
     if (results.value == 0x8B712ED) {
        key_pressed = true;
     }
     if ((results.value == 0xFFFFFFFF) && key_pressed) {
      digitalWrite (13, HIGH);
     delay(400);
      k = 1;
    }
    if (results.value != 0x8B712ED && results.value != 0xFFFFFFFF) {
     key_pressed = false;
    }
   irrecv.resume(); 
   }
   if (k == 1 && results.value == 0) {
   digitalWrite (13, LOW);
   k = 0;
   }
   }



Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
  static boolean key_pressed;
This is a local variable. Unless you provide an initial value, the value is whatever garbage happened to be in the memory location that was assigned to that variable. How is that garbage remotely useful to you?

Local variables MUST be initialized.
Logged

perpignan, france
Offline Offline
Sr. Member
****
Karma: 0
Posts: 280
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

archangel -> I think your "k" variable should be declared as "static", otherway it will be reset each time you enter in the loop fonction.

Static allow the variable to be conserved between several function calls.

PaulS -> what is the behaviour when you declare something like
static int k = 0; ?
k is initialised to 0 only on the first function call ? Or each time you call the function ? (so "static" will be quite useless...)
In the arduino reference manual, in the exemple use for "static" variable is not initialized.
Logged

-tu savais que si tu passe le CD de windows à l'envers, tu entends une chanson satanique ?
-non, mais il y a pire : à l'endroit, ça l'instal

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
what is the behaviour when you declare something like
static int k = 0; ?
k is initialised to 0 only on the first function call ?
The variable is declared and given an initial value ONCE. After that, it keeps whatever value it is given between function calls.

Quote
In the arduino reference manual, in the exemple use for "static" variable is not initialized.
That doesn't make it right.
Logged

perpignan, france
Offline Offline
Sr. Member
****
Karma: 0
Posts: 280
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good to know. Thank you very much. I think this must be added to the static reference documentation page.
Logged

-tu savais que si tu passe le CD de windows à l'envers, tu entends une chanson satanique ?
-non, mais il y a pire : à l'endroit, ça l'instal

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24321
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Insert Quote
Quote
  static boolean key_pressed;
This is a local variable. Unless you provide an initial value, the value is whatever garbage happened to be in the memory location that was assigned to that variable.
No, it is given the value zero (in this case, representing "false"), exactly the same as if you had declared it with global scope without an explicit initialiser.
« Last Edit: October 08, 2012, 08:04:57 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

perpignan, france
Offline Offline
Sr. Member
****
Karma: 0
Posts: 280
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok so to be really clear, something like

static int abcd;

is ok ? no need to explicitly set abcd to 0 ?

and if we write

static int abcd=0;

will this work, or will abcd be reset to 0 on each function call ?

I really think the documentation need to be clearer on this point.
Logged

-tu savais que si tu passe le CD de windows à l'envers, tu entends une chanson satanique ?
-non, mais il y a pire : à l'endroit, ça l'instal

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Explicitly setting the initial value is allowed, and highly recommended. It happens ONCE.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24321
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you give it any value explicitly, it will be set to that value before "setup" is run (before "init" or "main" even).
If you don't give it a value, it will be given the value zero, more or less at the same time as if you had given it a value.
This is only done once per reset.

This is standard C/C++ behaviour.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

perpignan, france
Offline Offline
Sr. Member
****
Karma: 0
Posts: 280
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, thanks to you two for the explanations smiley
Logged

-tu savais que si tu passe le CD de windows à l'envers, tu entends une chanson satanique ?
-non, mais il y a pire : à l'endroit, ça l'instal

Pages: 1 [2]   Go Up
Jump to: