while loop only run once

Hey!

I’m using the IRLIB2 library for controlling an IR reciever with an IR remote that is hooked up to the Arduino. I get the remote and receiver to work and I can turn a led on and off with a simple if(“buttonpushed”){“turn led on”}. My problem is when I do the same thing but with a while-loop that includes analogWrite(); instead of digitalWrite it will only run once!

So what I’m trying to do is get this while-loop to fade this LED back and forth, exiting the while-loop is something I deal with later. The fading code works perfectly if I do it by itself, but not in this loop:

if(myDecoder.value == threeButton){
      while(1){
        analogWrite(ledPin, brightness);
        brightness = brightness + fadeAmount;
        if (brightness <= 0 || brightness >= 255) 
          fadeAmount = -fadeAmount;  
        delay(30);
      }
}

But if I do it like this the while-loop will actually run like it should:

if(myDecoder.value == threeButton){
      while(1){
        digitalWrite(ledPin, HIGH);
        delay(100);
        digitalWrite(ledPin, LOW);
        delay(100);
      }
}

It somehow works. I suspect there is something the analogWrite() function does that I do not understand. Anyone got any ideas? Thanks in advance.

How does reset change in the while() to indicate that you should exit?

My mistake, I edit it! It's just a endless while loop. The point is not to get it to exist, right now it's to get it to run at all!

If the while() condition is false when you try to enter the loop, it will not run. That is why they added the do-while() loop.

It enters the loop and it runs once, the problem it doesn't run more than once. I've made it an infinite loop while(1) so that it would never exit.

  • I know it enters the loop
  • The loop is while(1) aka infinite so it should never exit
  • It only runs once and then I can't use the rest of the program either

I really don't understand what's happening here..

Well, it is hard to tell from a snippet, show all the code. are you running low on RAM?

#include <IRLibDecodeBase.h> // First include the decode base
#include <IRLib_P01_NEC.h>   // Include only the protocols you wish
IRdecodeNEC myDecoder;
// Include a receiver either this or IRLibRecvPCI or IRLibRecvLoop
#include <IRLibRecv.h>
IRrecv myReceiver(2); //pin number for the receiver

//*** ALL BUTTON CODES ***
#define upButton     0xFF629D
#define downButton   0xFFA857
#define rightButton  0xFFC23D
#define leftButton   0xFF22DD
#define okButton     0xFF02FD
#define oneButton    0xFF6897
#define twoButton    0xFF9867
#define threeButton  0xFFB04F
#define fourButton   0xFF30CF
#define fiveButton   0xFF18E7
#define sixButton    0xFF7A85
#define sevenButton  0xFF10EF
#define eightButton  0xFF38C7
#define nineButton   0xFF5AA5
#define zeroButton   0xFF4AB5
#define starButton   0xFF42BD
#define hashButton   0xFF52AD

int ledPin = 11;          // connected LED pin
int brightness = 255;     // how bright the LED is
int fadeAmount = 5;       // how many points to fade the LED by

void setup() {
 /pinMode(ledPin, OUTPUT);  // set ledPin to OUTPUT
  myReceiver.enableIRIn();  // start the receiver
} 

void loop() {
  //Continue looping until you get a complete signal received
  if (myReceiver.getResults()) {  // check if IR signal received
    myDecoder.decode();           // decode it
    
    if(myDecoder.value == oneButton) // if button 1 pushed
      digitalWrite(ledPin, HIGH);    // LED on
    
      
    if(myDecoder.value == twoButton)  // if button 2 pushed 
      digitalWrite(ledPin, LOW);      // LED off

    if(myDecoder.value == threeButton){ // if button 3 pushed
      while(1){             // endless while loop for fading LED
        analogWrite(ledPin, brightness);        
        brightness = brightness + fadeAmount;
        if (brightness <= 0 || brightness >= 255) 
          fadeAmount = -fadeAmount;
      }
    }                                 
    myReceiver.enableIRIn();      // restart receiver
  }                               // end of if(myReceiver.getResults()
}                                 // end of void loop()

If I hower try to do this in the while-loop:

while(1){
   digitalWrite(ledPin, HIGH);
   delay(100);
   digitalWrite(ledPin, LOW);
   delay(100);
}

it works perfectly…

analogWrite() uses a timer. The IR library uses a timer. PWM is disabled on two pins while you are using the IR library. Try a different PWM pin for the LED.