Go Down

Topic: IR butten (Read 3 times) previous topic - next topic

timothysg1

I can make a lat light up using this code with my remote

#include <IRremote.h>

int RECV_PIN = 8;

IRrecv irrecv(RECV_PIN);

decode_results results;

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

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
            if (results.value==0x8B77887){ digitalWrite(13, HIGH); delay(100); };




    irrecv.resume(); // Receive the next value
  }
}



but how can i turn him back off again using the same button?

johnwasser

Code: [Select]

boolean lampOn = false;

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
            if (results.value==0x8B77887)
                   { lampOn = !lampOn; digitalWrite(13, lampOn); delay(100); };

    irrecv.resume(); // Receive the next value
  }
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

timothysg1

can you give me some more information how your code works?
(i am a beginner but i want to learn)

PaulS

Quote
can you give me some more information how your code works?

The code defines a boolean variable, lampOn, and gives it an initial value.

When the proper remote data is received, the statement
Code: [Select]
lampOn = !lampOn;
sets lampOn to true if it was false, or to false if it was true.

Then, the value in lampOn is written to the digital pin. This has the same affect as writing HIGH or LOW.

johnwasser


can you give me some more information how your code works?


'boolean' is a 1-bit integer type.  It can contain the value 0 or 1.
'false' is equal to 0 (LOW is also equal to 0)
'true' is equal to 1 (HIGH is also equal to 1)

The '!' operator is the logical 'not' operator.  false == 0 == !true, true == 1 == !false

The second argument of digitalWrite() can either be 0=false=LOW which means TURN THE OUTPUT PIN OFF or some non-zero value (like true or HIGH) which means TURN THE OUTPUT PIN ON (+5V).
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

PaulS

Quote
'boolean' is a 1-bit integer type.  It can contain the value 0 or 1.

No. From wiring.h:
Code: [Select]
typedef uint8_t boolean;
So, boolean is a byte. It can hold any value between 0 and 255. Typically, the type is chosen for variables whose meaning is true/false, but the range of values is not limited to 0 or 1.

WizenedEE

Well, the "arduino language" defines it as a byte, but a boolean is technically 1 bit.

Why did the arduino guys decide to make "boolean" when there's already "bool"?

johnwasser


Quote
'boolean' is a 1-bit integer type.  It can contain the value 0 or 1.

No. From wiring.h:
Code: [Select]
typedef uint8_t boolean;
So, boolean is a byte. It can hold any value between 0 and 255. Typically, the type is chosen for variables whose meaning is true/false, but the range of values is not limited to 0 or 1.


You're right.  I meant to use the C++ type 'bool' instead of 'boolean'.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

PaulS

Quote
I meant to use the C++ type 'bool' instead of 'boolean'.

Doesn't matter. The smallest unit of allocation on the Arduino is 8 bits - a byte.

johnwasser

Although an Arduino bool may take up 8 bits it still has a range of 0 to 1.

Code: [Select]

bool test;

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

void loop() {
  for (int i=-5; i<=5; i++)
    {
    test = i;
    Serial.print(i);
    Serial.print("  ");
    Serial.println(test);
    }
while(true) ;
}


Output of this sketch:
Quote

-5  1
-4  1
-3  1
-2  1
-1  1
0  0
1  1
2  1
3  1
4  1
5  1
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

PaulS

Quote
Although an Arduino bool may take up 8 bits it still has a range of 0 to 1.

Interesting.

timothysg1

thanks for all the replys but i have another question:

how do I program a loop in a loop
for examle i recive a command, how do I let the led blink contineosly until it gets the same command to stop again?

johnwasser


thanks for all the replys but i have another question:

how do I program a loop in a loop
for examle i recive a command, how do I let the led blink contineosly until it gets the same command to stop again?


Follow the File->Examples->2.Digital->BlinkWithoutDelay example and just have it not turn on the LED unless the button has turned it on.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

timothysg1

i dont see how i could use thatin the way i want, I have this:

#include <IRremote.h>

int RECV_PIN = 0;

IRrecv irrecv(RECV_PIN);
boolean lampOn = false;


decode_results results;

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

void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
   
       if (results.value==0x8B73AC5)
                   { lampOn = !lampOn; digitalWrite(13, lampOn); delay(50); };       



    irrecv.resume(); // Receive the next value
  }
}

I want to do this:
instead of  { lampOn = !lampOn; digitalWrite(13, lampOn); delay(50); }; i want  let the led 13 blink if he gets the command 8B73AC5 and stop blinking
if the recever get the same command

PaulS

First, things first. Put each { one a new line. Put each statement on a its on line.
Code: [Select]
{ lampOn = !lampOn; digitalWrite(13, lampOn); delay(50); }
should be
Code: [Select]
{
 lampOn = !lampOn;
 digitalWrite(13, lampOn);
 delay(50);
}

Now, you can see that this code causes the LED to turn on if it was off, and to turn off if it was on.

What you need to do is replace this code with some code that sets a flag, lampBlink, to the opposite of what it was (just like lampOn is manipulated).

Then, at the end of loop, add some code that checks the time and toggles the LED, as in the blink without delay example.

Try something. If it doesn't work, post your attempt (properly, using the button with the # on it).

Go Up