I am trying to make two LEDs flash on and off using an infra-red remote. I am creating a quadruped robot for a university project and eventually instead of flashing LEDs I plan to use the infra-red remote to control servos to make the robot walk forward, left etc. The problem however occurring with the code shown below is that no matter what button is pressed, it will always flash the LED stated within the while {forward = true) statement.
I am new to coding so not sure where I am going wrong, any help would be much appreciated.
#include <IRremote.h>
#include <Servo.h>
int IRpin = 11;
int LED = 13;
int LEDred = 9;
IRrecv irrecv(IRpin);
decode_results results;
boolean Forward = false;
boolean Left = false;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(LED, OUTPUT);
pinMode(LEDred, OUTPUT);
}
void loop()
{
if (irrecv.decode(&results))
{
Serial.println(results.value, DEC);
irrecv.resume(); // Receive the next value
}
switch(results.value)
{
case 16736925:
Forward = true;
Left = false;
pressMovement();
break;
case 16720605:
Forward = false;
Left = true;
pressMovement();
break;
}
}
void pressMovement()
{
while (Forward = true)
{
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(1000);
}
while (Left = true)
{
digitalWrite(LEDred, HIGH);
delay(1000);
digitalWrite(LEDred, LOW);
delay(1000);
}
}
Hi there, thanks for replying. This has fixed one of my problems, where now I can flash either one of the LEDs on and off.
However I was also wondering how I would use the remote to switch between flashing the red LED (LEDred) and the blue LED (LED) on and off? At the moment I press the forward button, and it flashes the blue LED on and off. If I then press the left button, nothing happens and the blue LED will continue to flash and off. If I reset the arduino and press the left button, it will flash the red LED on and off and if I then press the forward button, the red LED will continue to flash and off.
What I want to happen is for me to press the forward button and for the blue LED to flash on and off, and when I then press the Left button the blue LED should stop flashing and the red LED should start.
The way it works here is that you make changes and re post the code with your changes so we can see if you have done them right.
As it is you have this:-
Now look at that, is there any way that once the 'Forward' variable is true it can ever possibly become false? Once in that while loop you are stuck their forever.
I was under the impression that when another button on the infrared was pressed, another case statement is called, which would set the Forward boolean to false and the while loop associatedwould stop executing? The other while loop would then start executing as the left boolean has been set to true.
#include <IRremote.h>
#include <Servo.h>
int IRpin = 11; // pin for the IR sensor
int LED = 13; // LED pin
int LEDred = 9;
IRrecv irrecv(IRpin);
decode_results results;
boolean Forward = false;
boolean Left = false;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(LED, OUTPUT);
pinMode(LEDred, OUTPUT);
}
void loop()
{
if (irrecv.decode(&results))
{
Serial.println(results.value, DEC);
irrecv.resume(); // Receive the next value
}
switch(results.value)
{
case 16736925:
Forward = true;
Left = false;
pressMovement();
break;
case 16720605:
Forward = false;
Left = true;
pressMovement();
break;
}
}
void pressMovement()
{
while (Forward == true)
{
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(1000);
}
while (Left == true)
{
digitalWrite(LEDred, HIGH);
delay(1000);
digitalWrite(LEDred, LOW);
delay(1000);
}
}
I was under the impression that when another button on the infrared was pressed, another case statement is called,
No.
You still have the same problem, after any press there are two seconds where the program will not respond to anything. And your variable has no neutral position so once set to one thing it will keep on doing that until you press the another direction.
this means it is hard to catch the message in the brief micro second of looking at it between two seconds of total shut down of your IR receiver
I have read through that article a number of times and understand how it gets the LED to flash, however I am unsure as to how to apply it to my problem. I plan to replace the code to make LEDs flash on and off with code to rotate servos and yhence why I chose to structure the code this way.