IR butten

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?

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
  }
}

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

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

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.

timothysg1:
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).

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

No. From wiring.h:

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.

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"?

PaulS:

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

No. From wiring.h:

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'.

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.

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

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:

-5 1
-4 1
-3 1
-2 1
-1 1
0 0
1 1
2 1
3 1
4 1
5 1

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

Interesting.

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?

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?

Follow the File->Examples->2.Digital->BlinkWithoutDelay example and just have it not turn on the LED unless the button has turned it on.

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

First, things first. Put each { one a new line. Put each statement on a its on line.

{ lampOn = !lampOn; digitalWrite(13, lampOn); delay(50); }

should be

{ 
  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).

I did what you asked and I cleand up the code,

#include <IRremote.h>

int RECV_PIN = 0;
IRrecv irrecv(RECV_PIN);
boolean lampOn = false;
decode_results results;

void setup()
{
Serial.begin(9600);
irrecv.enableIRIn();
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();
}
}

but as I said I am a real newbie, I have no idea what a flag is
not to mention to set one, and what is the purpos (and how do you make it) of:
" add some code that checks the time and toggles the LED" ?
plz help me

It has been explained that lampOn is a flag. It can have one of two values. Surely you can figure out how to define a similar variable that defines whether the LED should be blinking, or not.

The whole purpose of the blink without delay example is to show how to do things without needing to use delay(). Have you even looked at the example?

of corse i loked at it, i am looking at it right now
but I don't understand what it has to do with what I want... I dont think the delay is important i just want to let it blink when it recives the command to ... and stop blinking if it gets the same command

You must be aware, then, that LEDs don't blink on their own. You must turn the LED on if it is time, and turn it off if it is time.

You have code that know when a particular IR signal has been received. You've been told how to change that to just record that the LED needs to blink, or not.

You have code that turns and LED on of it is time, and turns it off if it is time.

Put them together. Try something, even if it is wrong.

to i have this:

#include <IRremote.h>

int RECV_PIN = 0;
IRrecv irrecv(RECV_PIN);
boolean lampOn = false;
decode_results results;
int ledState = LOW;
long previousMillis = 0;
long interval = 1000;
const int ledPin = 13;

void setup()
{
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(ledPin, OUTPUT);

}

void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
if (results.value==0x8B73AC5)
{ lampOn = !lampOn;
unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;

digitalWrite(ledPin, ledState);
}
}

irrecv.resume();
}
}

but ti doesn't blink
it only goes on when it gest the command to
and turns of when it gets the same command

i probably made somthing silly, did i foget sommething? i tried to
change the 1000 in "long interval = 1000; " but when i tried the command to turn it on it didn't do anything