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?
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:
Use else if to ensure only one condition is executed at a time.
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
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).
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.