Hi,
I am trying to solve a problem with my code. So it works all well But at one point IR stops reacting. Serial doesnt print anything but the patterns continue to animate. IR reciever LED also blinks along with the remote. After restart it works again. I cannot figure it out for the whole day. Would someone be willing to look at my code?
//import the necessary libraries
#include "FastLED.h"
#include "IRremote.h"
//define some variables
#define DATA_PIN 3
#define CLK_PIN 4
#define RECV_PIN 9
#define LED_TYPE APA102
#define COLOR_ORDER BGR
#define NUM_LEDS 34
#define BRIGHTNESS 204
#define FRAMES_PER_SECOND 30
CRGB leds[NUM_LEDS];
CRGB endclr;
CRGB midclr;
int check = 0;
int b = 204;
uint32_t Previous = 0;
int mode;
//set up IR receiver information
IRrecv irrecv(RECV_PIN);
decode_results results;
//<————————————————————————————SETUP————————————————————————————>
void setup()
{
//begin serial communication
Serial.begin(9600);
//sanity delay
delay(3000);
//start the receiver
irrecv.enableIRIn();
//set up LED strip information
FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
}
//<————————————————————————————PATTERN SETUP————————————————————————————>
// List of patterns to cycle through. Each is defined as a separate function below.
typedef void (*SimplePatternList[])();
SimplePatternList gPatterns = { white, green, red, confettiWhite, confettiGreen, confettiRed, BreatheWhite, BreatheGreen, BreatheRed, Black};
uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
//<—————————————————————————LOOP———————————————————————————————>
void loop() {
// Delay
FastLED.delay(1000/FRAMES_PER_SECOND);
FastLED.setBrightness(b);
// Call the current pattern function once, updating the 'leds' array
gPatterns[gCurrentPatternNumber]();
// Decode values from IR reciever and clean up duplicates
if (irrecv.decode(&results)) {
if (results.value==0xFFFFFFFF) {
results.value=Previous;
}
if (results.value==0) {
results.value=Previous;
}
if (results.value!=Previous)
{
Serial.println (results.value, HEX); //display HEX results
}
irrecv.resume(); // Receive the next value
}
Previous=results.value;
//<——————————————————FX CONTROL—————————————————————————>
EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow
//EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
//<——————————————————BUTTON FUNCTIONS—————————————————————————>
if (results.value == 0xFFA25D){ // 1 - White
gCurrentPatternNumber = 0;
results.value=0;
}
if (results.value == 0xFF629D){ // 2 - Green
gCurrentPatternNumber = 1;
results.value=0;
}
if (results.value == 0xFFE21D){ // 3 - Red
gCurrentPatternNumber = 2;
results.value=0;
}
if (results.value == 0xFF22DD){ // 4
gCurrentPatternNumber = 3;
results.value=0;
}
if (results.value == 0xFF02FD){ gCurrentPatternNumber = 4; results.value=0;} //5
if (results.value == 0xFFC23D){ gCurrentPatternNumber = 5; results.value=0;} //6
if (results.value == 0xFFE01F){ gCurrentPatternNumber = 6; results.value=0;} //7
if (results.value == 0xFFA857){ gCurrentPatternNumber = 7; results.value=0;} //8
if (results.value == 0xFF906F){ gCurrentPatternNumber = 8; results.value=0;} //9
if (results.value == 0xFF9867){ //0
gCurrentPatternNumber = 9;
}
if (results.value == 0xFF10EF){b = b-25; if (b<=4) {b=4;} results.value=0;} //left
if (results.value == 0xFF5AA5){b = b+25; if (b>=255) {b=255;} results.value=0;} //right
/*
if (results.value == 0xFFB04F){ // Hashtag - Breathe
gCurrentPatternNumber = 11;
}
if (results.value == 0xFF6897){ // * - sparkle
gCurrentPatternNumber = 10;
}
*/
/*
if (results.value == 0xFF22DD){} //4
if (results.value == 0xFF02FD){} //5
if (results.value == 0xFFC23D){} //6
if (results.value == 0xFFE01F){} //7
if (results.value == 0xFFA857){} //8
if (results.value == 0xFF906F){} //9
if (results.value == 0xFF9867){} //0
if (results.value == 0xFF10EF){} //left
if (results.value == 0xFF5AA5){} //right
if (results.value == 0xFF18E7){} //up
if (results.value == 0xFF4AB5){} //down
if (results.value == 0xFF38C7){} //ok
*/
//<——————————————————WRTITE TO LED—————————————————————————>
FastLED.show();
//Serial.println (b);
}
//<——————————————————PATTERN ARRAY—————————————————————————>
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
void white()
{
fill_solid(leds,NUM_LEDS,CRGB::White);
}
void green()
{
fill_solid(leds,NUM_LEDS,CRGB::Green);
}
void red()
{
fill_solid(leds,NUM_LEDS,CRGB::Red);
}
void Black()
{
fill_solid(leds,NUM_LEDS,CRGB::Black);
}
void BreatheWhite()
{
fill_solid(leds,NUM_LEDS,CRGB::White);
//FastLED.setBrightness( beatsin8(4,50,255));
}
void BreatheRed()
{
fill_solid(leds,NUM_LEDS,CRGB::Red);
//FastLED.setBrightness( beatsin8(4,50,255));
}
void BreatheGreen()
{
fill_solid(leds,NUM_LEDS,CRGB::Green);
//FastLED.setBrightness( beatsin8(4,50,255));
}
void confettiWhite()
{
// random colored speckles that blink in and fade smoothly
fadeToBlackBy( leds, NUM_LEDS, 20);
int pos = random16(NUM_LEDS);
leds[pos] += CHSV(255, 0, 255);
}
void confettiGreen()
{
// random colored speckles that blink in and fade smoothly
fadeToBlackBy( leds, NUM_LEDS, 20);
int pos = random16(NUM_LEDS);
leds[pos] = CRGB::Green;
}
void confettiRed()
{
// random colored speckles that blink in and fade smoothly
fadeToBlackBy( leds, NUM_LEDS, 20);
int pos = random16(NUM_LEDS);
leds[pos] = CRGB::Red;
}
void sinelonWhite()
{
// a colored dot sweeping back and forth, with fading trails
fadeToBlackBy( leds, NUM_LEDS, 20);
int pos = beatsin16( 7, 0, NUM_LEDS-1 );
leds[pos] = CRGB::White;
}