Hello,
I’m having an issue with the while function. I’m working with a timer. If the IR sensor received a result while the timer is on, than the led will be blue. When the timer is of (10.000 ms are over) and the IR sensor didn’t received anything. Than the led will be red.
My problem is that the timerfunction or the else function that I use don’t work. The while function is always active. Even when it’s over the 10.000ms.
Does anyone know what the problem is?
#include <IRremote.h>
int button = 2;
int RECV_PIN = 3; //declared for receiving pulses
int c=0; // declared if you want to control with a single button
int buttonState = 0;
boolean drukknop = LOW;
bool received_data;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long start_time;
unsigned long current_time;
const unsigned long over_time=10000;
// RGB LED
int ledrood = 5;
int ledblauw = 6;
int ledgroen =7;
void setup()
{
start_time = millis ();
pinMode (button,INPUT);
pinMode(ledrood, OUTPUT);
pinMode(ledblauw, OUTPUT);
pinMode(ledgroen, OUTPUT);
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
start_time=millis();
delay(1);
current_time = millis();
received_data = irrecv.decode(&results);
while(received_data && (current_time - start_time) < over_time) { //300.000 equals to 5min
if (irrecv.decode(&results)) {
received_data = true;
digitalWrite (ledrood, LOW);
digitalWrite (ledblauw, HIGH) ;
digitalWrite (ledgroen, LOW);
} else {
received_data = false;
digitalWrite (ledrood, HIGH);
digitalWrite (ledblauw, LOW) ;
digitalWrite (ledgroen, LOW);
}
}
}