Issue with while - else function

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

It seems to me that your (current_time - start_time) is always 1 milli

Dr_G:
It seems to me that your (current_time - start_time) is always 1 milli

Thank you. This was one of the problems. But the else function still does not work.

#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;

// case 3 startijd,...
const unsigned long start_time =0;
unsigned long current_time;
const unsigned long over_time=10000;
//long current_time=0;
 
// 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() {     
  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); 
        }
      }
}[code]

THe while loop is only entered when there is data received.

Therefor I expect that irrecv.decode(&results) always return true?

Try removing the while altogether as loop is called again and again

void loop()
{
  current_time = millis();

  if (irrecv.decode(&results) && (current_time - start_time) < over_time)
  {
    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);
  }
}

PS if it is easier to talk in Dutch please check the Dutch section (Assumption from var names)

I found the solution. I needed to add a if function in the else function.

#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;

// case 3 startijd,...
const unsigned long start_time= 0;
unsigned long current_time;
const unsigned long over_time=10000;
//long current_time=0;
 
// 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() {  
 
  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); 
        }
      
}while((current_time-start_time) >=over_time){
    if( received_data == false){
          digitalWrite (ledrood, HIGH); 
          digitalWrite (ledblauw, LOW) ;
          digitalWrite (ledgroen, LOW); 
    }
  }
}
[code]
  delay(1);
   current_time = millis();

These two functions do not belong in the same sketch.

PaulS:

  delay(1);

current_time = millis();



These two functions do not belong in the same sketch.

I understand why you say that , but I expect the OP might need some more explanation