Hi. as a newbie I am having a problem with infrared, my first task is to have a green LED on then after receiving the remote signal it turns off the green LED & turns on the REDLED and starts blinking. I have no problem getting it working, the receiver is accepting the signals and it does the case up to the point of the code]red LED , it should blink but it doesn't, I have tried basic HIGH & LOW commands but it seems to be blocking the code, so I tried the blink no delay code, but I fear the IR control is blocking the code, any help, guidance would be appreciated.
#include <IRremote.h>
const int redled_pin = 5 ;// the number of the LED pin
const int greenled_pin = 3;
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)
//#define first_key 25245
//#define second_key 43095
int receiver_pin = 6;
//int led[] = {0,0,0,0};
IRrecv receiver(receiver_pin);
decode_results output;
void setup()
{
Serial.begin(9600);
receiver.enableIRIn();
pinMode(redled_pin, OUTPUT);
pinMode(greenled_pin, OUTPUT);
digitalWrite(greenled_pin, HIGH);
}
void loop() {
if (receiver.decode(&output)) {
unsigned int value = output.value;
switch(value) {
case 25245:
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(redled_pin, ledState);
digitalWrite (greenled_pin, LOW);
}
}
Serial.println(value);
receiver.resume();
}
}
The value member of the decode_results struct instance is NOT an unsigned int. Do NOT save the value in an unsigned int.
but I fear the IR control is blocking the code
No, it is not.
Use Tools + Auto Format, to properly indent your code. Then, think about what happens on each pass through loop().
On some pass, the
if (receiver.decode(&output))
statement evaluates to true. So, you do some stuff, if the proper button on the remote was pressed. Then, you resume reading from the remote.
On subsequent passes through loop(), when there is no new input from the remote, what do you do? If the answer you come up with isn't "nothing but enable the IR to receive another value", you got the wrong answer.
Lcautos:
Thank you Karma, I think I understand , any chance you could edit my code to do what you suggest, many thanks.
I did not suggest anything. I described what your code actually does. I do not understand what you want it to do differently.
It seems that you want to press one button and have the led start blinking. If that is the case, you need to separate the "blink the LED" code from the "the LED should be blinking" code. In the part of the code that determines that the LED should be blinking, just set a flag that says that the LED should be blinking.
Then, at the same level as the if(receiver.decode(&output)) statement, add
if(ledShouldBeBlinking)
{
// code to blink the LED goes here
}
Hi I will try that tonight when I get home , this is a level of understanding I aspire to, many thanks will let you know how I get on, it seems a very simple task but difficult to do. Thanks.
I have resolved the issue with your help, thanks, although not bullet proof I can make it work buy commenting out the last line of the code "receiver.resume" this allows the LED to blink but it stops me adding another "case" where as I wanted to be able to send a reset command or get it back to the first part of the code. I got around this by adding a switch connected to the reset pin on the board, it doesn't require any coding and resets back to the start as I want.
If any one else needs more info please contact through this channel.
Many thanks.
Hi. I agree that it should be a last resort, and I'm sure there is another way of achieving what I want, but as simple as it should be, I couldn't get it to stop the first code and reset back to the first stage, unless I put back the last line,then it wouldn't blink as I needed. I have included the latest code as requested, many thanks.
#include <IRremote.h>
const int redled_pin = 5 ;// the number of the LED pin
const int greenled_pin = 3;
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds)
//#define first_key 25245
//#define second_key 43095
int receiver_pin = 6;
int buzz = 7;
//int led[] = {0,0,0,0};
IRrecv receiver(receiver_pin);
decode_results output;
void setup()
{
Serial.begin(9600);
receiver.enableIRIn();
pinMode(redled_pin, OUTPUT);
pinMode(greenled_pin, OUTPUT);
digitalWrite(greenled_pin, HIGH);
pinMode (buzz, OUTPUT),
digitalWrite (buzz, LOW);
}
void loop() {
if (receiver.decode(&output)) {
unsigned int value = output.value;
switch (value) {
case 25245:
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(redled_pin, ledState);
digitalWrite (greenled_pin, LOW);
digitalWrite (buzz, HIGH);
delay(1500); // ...for 1 sec
digitalWrite (buzz, LOW); // Stop sound...
//delay(1500); // ...for 1sec
}
}
Serial.println(value);
//receiver.resume();
}
}
I am pretty sure that putting every { on a line BY ITSELF, and using Tools + Auto Format to properly indent your code would go a long way to identifying the problem.
Your code does something when it receives a signal from the remote. It does NOT start doing something. There is a huge amount of difference between those two.
I suspect that you want it to start doing something when one button is pressed, and do something else when another button is pressed.
It would also help if you built a function to do each thing. You can more easily see that blinkTheLED() is not going to be called over and over, than you can see that a big block of code is not going to be executed over and over.
The value member of the struct that output is an instance of is STILL NOT an unsigned int.