Pages: [1] 2 3 4   Go Down
Author Topic: IR butten  (Read 2680 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8695
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
  }
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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:
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.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8695
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

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

Quote
'boolean' is a 1-bit integer type.  It can contain the value 0 or 1.
No. From wiring.h:
Code:
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.
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8695
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
'boolean' is a 1-bit integer type.  It can contain the value 0 or 1.
No. From wiring.h:
Code:
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'.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

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

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8695
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

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

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

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8695
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 44
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

First, things first. Put each { one a new line. Put each statement on a its on line.
Code:
{ lampOn = !lampOn; digitalWrite(13, lampOn); delay(50); }
should be
Code:
{
  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).
Logged

Pages: [1] 2 3 4   Go Up
Jump to: