Hi Arduino community,
i keep on updating my first small LED project and am now trying to replace my 3 physical buttons which have been switching in between 2 led animations (3rd button was for off), with buttons on a IR remote.
Im using the IRremote library and things are pretty stright forward. I am getting clean readouts in the serial monitor from the remote. But when i try to plug in the remote code into my sketch suddenly the serial monitor starts showing random values even while pressing the same button.
This is the full sketch.
#include <FastLED.h>
#include <IRremote.h>
int IR_Recv = 12;
IRrecv irrecv(IR_Recv);
decode_results results;
#define DATA_PIN 2
#define LED_TYPE WS2812
#define COLOR_ORDER GRB
#define NUM_LEDS 72
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 96
#define FRAMES_PER_SECOND 180
int ledspeed;
int ledcolorR;
int ledcolorG;
int ledcolorB;
const unsigned long eventInterval1 = 26000;
const unsigned long eventInterval2 = 21000;
unsigned long startTime1 = 0;
unsigned long startTime2 = 0;
void setup() {
Serial.begin(9600);
delay(3000); // 3 second delay for recovery
irrecv.enableIRIn(); // Starts the receiver
// tell FastLED about the LED strip configuration
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
//FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
// set master brightness control
FastLED.setBrightness(BRIGHTNESS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 500);
}
void loop()
{
if (irrecv.decode(&results)) { //Wenn Daten empfangen wurden,
Serial.println(results.value, DEC); //werden sie als Dezimalzahl (DEC) an den Serial-Monitor ausgegeben.
irrecv.resume(); //Der nächste Wert soll vom IR-Empfänger eingelesen werden
}
unsigned long currentTime = millis();
if (results.value == 16753245) {
startTime1 = currentTime;
ledspeed=80;
ledcolorR = 128;
ledcolorG = 255;
ledcolorB = 200;
}
if (startTime1 > startTime2 && currentTime - startTime1 >= eventInterval1) {
ledspeed=0;
ledcolorR = 0;
ledcolorG = 0;
ledcolorB = 0;
}
if (irrecv.decode(&results) == 16736925) {
startTime2 = currentTime;
ledspeed=100;
ledcolorR = 128;
ledcolorG = 255;
ledcolorB = 200;
}
if (startTime2 > startTime1 && currentTime - startTime2 >= eventInterval2) {
ledspeed=0;
ledcolorR = 0;
ledcolorG = 0;
ledcolorB = 0;
}
if (digitalRead(button3) == LOW) {
ledcolorR = 0;
ledcolorG = 0;
ledcolorB = 0;
}
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy(leds, NUM_LEDS, 254);
int pos = beatsin16(ledspeed, 0, NUM_LEDS-1 );
leds[pos] += CRGB(ledcolorR, ledcolorG, ledcolorB);
// send the 'leds' array out to the actual LED strip
FastLED.show();
// insert a delay to keep the framerate modest
FastLED.delay(50/FRAMES_PER_SECOND);
}
I already identified that when i remove the last part of the code I again get a clean readout in the serial monitor. So removing this helps getting a clean readout again but of course breaks the sketch.
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy(leds, NUM_LEDS, 254);
int pos = beatsin16(ledspeed, 0, NUM_LEDS-1 );
leds[pos] += CRGB(ledcolorR, ledcolorG, ledcolorB);
// send the 'leds' array out to the actual LED strip
FastLED.show();
// insert a delay to keep the framerate modest
FastLED.delay(50/FRAMES_PER_SECOND);
Been stuck with this for the past 3 hours and i can not see where the issue might be. Initially replacing physical buttons with remote buttons seemed like a very easy task. Looks like it isnt.
Appreciate any help.
Thanks already.