How to turn off after fading LED

Hello, i am new to the World of Arduino and try a project.

i build a mask with leds with IR control. It works when i turn it on and off, but when i try to turn off after fading, it doesnt go off. Where is my mistake here?

I hope someone can help and explain it to me :slight_smile:

#include <IRremote.h>

IRrecv IR(11);
int mask = 10;
int speed = 50;

void setup() 
{
  IR.enableIRIn();
  pinMode(mask, OUTPUT);
  Serial.begin(9600);
}

void loop()  {
  if(IR.decode()){
    Serial.println(IR.decodedIRData.decodedRawData, HEX);
    if(IR.decodedIRData.decodedRawData == 0xED127F80){
      digitalWrite(mask, HIGH);
    } 
    if(IR.decodedIRData.decodedRawData == 0xE51A7F80){
      digitalWrite(mask, LOW);
    }
    while(IR.decodedIRData.decodedRawData == 0xE11E7F80){
      for(int i = 50 ; i <= 240 ; i++){
      analogWrite(mask, i);
      delay(speed);
    }
for (int i = 240 ; i >= 50 ; i--){
    analogWrite(mask, i);
    delay(speed);
     }
    }
    IR.resume();
    }
}

Where do you tell it to switch off after fading? The brightness is 50 after the fading. The only "off" that I can find is a command from the IR.

#include <IRremote.h>

IRrecv IR(11);
int mask = 10;
int speed = 50;

void setup()
{
  IR.enableIRIn();
  pinMode(mask, OUTPUT);
  Serial.begin(9600);
}

void loop()  {
  if (IR.decode()) {
    Serial.println(IR.decodedIRData.decodedRawData, HEX);
    if (IR.decodedIRData.decodedRawData == 0xED127F80) {
      digitalWrite(mask, HIGH);
    }
    if (IR.decodedIRData.decodedRawData == 0xE51A7F80) {
      digitalWrite(mask, LOW);
    }
    if (IR.decodedIRData.decodedRawData == 0xE11E7F80) {
      for (int i = 50 ; i <= 240 ; i++) {
        analogWrite(mask, i);
        delay(speed);
      }
      for (int i = 240 ; i >= 50 ; i--) {
        analogWrite(mask, i);
        delay(speed);
      }
      digitalWrite(mask, LOW);
    }
    IR.resume();
  }
}

Oh I mean I want it to turn off after fading with the IR Off command, it should fading until I press the off button.. but it doesn't. After I press fading the other commands not working. I hope now it is understandable what I want

The issue you’re encountering might be due to the way the while loop is structured. Once the fading sequence starts, it doesn’t exit the loop until the fading is complete, which might be causing the problem when you try to turn off the LEDs.

Here’s a revised version of your code with some adjustments:

  1. Use else if to ensure only one condition is executed at a time.
  2. Add a condition to break out of the while loop if a different IR signal is received.
#include <IRremote.h>

IRrecv IR(11);
int mask = 10;
int speed = 50;

void setup() {
  IR.enableIRIn();
  pinMode(mask, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if (IR.decode()) {
    Serial.println(IR.decodedIRData.decodedRawData, HEX);
    if (IR.decodedIRData.decodedRawData == 0xED127F80) {
      digitalWrite(mask, HIGH);
    } else if (IR.decodedIRData.decodedRawData == 0xE51A7F80) {
      digitalWrite(mask, LOW);
    } else if (IR.decodedIRData.decodedRawData == 0xE11E7F80) {
      for (int i = 50; i <= 240; i++) {
        analogWrite(mask, i);
        delay(speed);
        if (IR.decode() && IR.decodedIRData.decodedRawData != 0xE11E7F80) {
          break;
        }
      }
      for (int i = 240; i >= 50; i--) {
        analogWrite(mask, i);
        delay(speed);
        if (IR.decode() && IR.decodedIRData.decodedRawData != 0xE11E7F80) {
          break;
        }
      }
    }
    IR.resume();
  }
}

This should help your mask turn off properly after fading. Give it a try and let me know if it works!

Hello and thanks for the code, but I want to turn it off manually from a loop with my Off button. With your code it fades 1 time and turn off from itself :slight_smile:

It does not work! And, you did not follow-up with @alexinsane92

@alexinsane92 ... your sketch needed a non-blocking fade rather than a looping fade. One method for non-blocking fade is 1. signal when you want to fade. 2. Check if it is time to fade. 3. Check fade top/bottom boundaries. 4. Change directions at boundaries. 5. fade.

This should turn on with one button, turn off with a second button, fade with a third button and only turn off with the second button.

#include <IRremote.h>

IRrecv IR(11);
int mask = 10;
int speed = 50;

bool flag = 0; // signals "time to fade"
int i = 50; // fade counter
int direction = 1; // direction of fade (+1 up, -1 down)
unsigned long timer; // comparison timer for fade

void setup()
{
  IR.enableIRIn();
  pinMode(mask, OUTPUT);
  Serial.begin(9600);
}

void loop()  {
  if (IR.decode()) {
    Serial.println(IR.decodedIRData.decodedRawData, HEX);
    if (IR.decodedIRData.decodedRawData == 0xED127F80) {
      digitalWrite(mask, HIGH);
    }
    if (IR.decodedIRData.decodedRawData == 0xE51A7F80) {
      digitalWrite(mask, LOW);
      flag = 0; // do not run fade
    }
    if (IR.decodedIRData.decodedRawData == 0xE11E7F80) {
      flag = 1;
    }
    IR.resume();
  }

  // fade routine
  if (flag) { // "flag" starts fade in/out
    if (millis() - timer > speed) { // if "speed" time elapsed...
      timer = millis(); // reset the "speed" comparison counter
      i += direction; // add fade in the correct direction
      if (i < 50 || i > 240) { // if at fade boundaries
        direction = -direction; // change directions
      }
      analogWrite(mask, i); // show fade on the LED
    }
  }
}

Verify the IR key codes are correct for your IR Remote (I have different key codes).

1 Like

Thank you very much! Now it works like i want it :slight_smile: Many many Thanks!

1 Like

Do you see how this "fade function" lets all the other IR code execute while looking at the "speed" timer to "fade" at the right speed.... all in one big loop. Study different methods of "non-blocking" code, because you will want to use it often in your programming. You will find another method in search terms "arduino blink without delay()". I hope you can understand my comments in this example. Ask any questions.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.