First of all, I'm very new to this so sorry if it's a dumb question.
I have an IR module which I'm using to control an LED strip. I have set buttons 1–3 on the remote to select three basic static colours and up and down to adjust brightness. So far so good.
I have set button 4 to gradually shift through different colours. Once I activate this code, the IR receiver appears to stop working, and Serial.print shows that it's now reporting random values whatever button I press on the remote.
I have tried Vishay TSOP4838 and KY-022 type receivers, and tried varying combinations of resistors and capacitors on the receiver power supply. I've tried powering it from 3.3V and 5V.
The code that I'm using is only changing numerical values to adjust the colour, so short of there being 'noise' somewhere in the hardware I'm at a loss as to why only this code breaks it. The LED is powered by a 12V adaptor. The IR receiver is the only thing powered by the Arduino.
My full code is as follows, I know it's untidy. I am a noob:
Thank you
#include "IRremote.h"
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
//To change the LED/IR reciever pins, you just need to change these values
#define PIN 13 //LED strip
int receiver = 10; //IR receiver
unsigned long debouncing = 0;
unsigned long rotatedelay = 6;
unsigned long fadegap = 5;
byte state = 0; //maintains function based on last button press
byte red = 0;
byte blue = 0;
byte green = 0;
byte brightness = 250;
byte rotate = 0; //used for cycling through colours in state 4
Adafruit_NeoPixel strip = Adafruit_NeoPixel(50, PIN, NEO_GRB + NEO_KHZ800);
//Declare IR objects
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results; // create instance of 'decode_results'
//Setup IR buttons with names
void translateIR() // takes action based on IR code received
// Naming remote IR codes and assigning a 'state'
{
switch(results.value)
{
case 0xFFA25D: state = 10; Serial.println("POWER"); break;
case 0xFF629D: state = 11; Serial.println("VOL+"); break;
case 0xFFE21D: state = 12; Serial.println("FUNC/STOP"); break;
case 0xFF22DD: state = 13; Serial.println("FAST BACK"); break;
case 0xFF02FD: state = 14; Serial.println("PLAY/PAUSE"); break;
case 0xFFC23D: state = 15; Serial.println("FAST FORWARD"); break;
case 0xFFE01F: Serial.println("DOWN"); break;
case 0xFFA857: state = 17; Serial.println("VOL-"); break;
case 0xFF906F: Serial.println("UP"); break;
case 0xFF9867: state = 19; Serial.println("EQ"); break;
case 0xFFB04F: state = 20; Serial.println("ST/REPT"); break;
case 0xFF6897: state = 0; Serial.println("0"); break;
case 0xFF30CF: state = 1; Serial.println("1"); break;
case 0xFF18E7: state = 2; Serial.println("2"); break;
case 0xFF7A85: state = 3; Serial.println("3"); break;
case 0xFF10EF: state = 4; Serial.println("4"); break;
case 0xFF38C7: state = 5; Serial.println("5"); break;
case 0xFF5AA5: state = 6; Serial.println("6"); break;
case 0xFF42BD: state = 7; Serial.println("7"); break;
case 0xFF4AB5: state = 8; Serial.println("8"); break;
case 0xFF52AD: state = 9; Serial.println("9"); break;
case 0xFFFFFFFF: Serial.println("REPEAT");break;
default:
Serial.println(" other button ");
}// End Case
} //END translateIR
void setup() {
Serial.begin(9600);
irrecv.enableIRIn(); // Start the IR receiver
strip.begin();
colorBlock(strip.Color(0, 0, 0));
strip.show(); // Initialize all pixels to 'off'
//Fades LED strip light in at startup
for(uint8_t b = 0; b <255; b++) {
for(uint8_t i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 255 * b/255, 255 * b/255, 255 * b/255);
}
strip.show();
delay(5); //speed of fade in (20 x 255 = total time)
};
}
void loop() {
//IR detection code
//if (irrecv.decode(&results) && millis() - debouncing > 1000) // have we received an IR signal? ignores button press if less than a second after previous
if (irrecv.decode(&results)) // have we received an IR signal?
{
translateIR();
irrecv.resume(); // receive the next value
Serial.println(results.value, HEX);
if (state == 1){
rotate = 0;
red = 0; blue = 255; green = 0;
colorBlock(strip.Color(red, blue, green));
}
if (state == 2){
rotate = 0;
red = 0; blue = 0; green = 255;
colorBlock(strip.Color(red, blue, green));
}
if (state == 3){
rotate = 0;
red = 255; blue = 0; green = 255;
colorBlock(strip.Color(red, blue, green));
}
//brightness
if (results.value == 16769055 && brightness > 0){
brightness = brightness - 10;
strip.setBrightness(brightness); strip.show();
Serial.println(brightness);
}
if (results.value == 16748655 && brightness < 241){
brightness = brightness + 10;
strip.setBrightness(brightness); colorBlock(strip.Color(red, blue, green)); strip.show();
Serial.println(brightness);
}
}
//rotating colours
if (state == 4){
//Serial.println (state);
//Serial.println (rotate);
//Serial.print ("red"); Serial.print (red); Serial.print ("blue"); Serial.print (blue); Serial.print ("green"); Serial.println (green);
//Serial.println (rotatedelay);
}
if (state == 4 && rotate == 0 && red < 255 && millis() - rotatedelay > 1){
red = red + fadegap;
if (blue >= 1){
blue = blue - fadegap;}
if (green >= 1){
green = green - fadegap;}
colorBlock(strip.Color(red, blue, green));
rotatedelay = millis();
}
if (state == 4 && rotate == 0 && green > 0 && millis() - rotatedelay > 1){
green = green - fadegap;
colorBlock(strip.Color(red, blue, green));
rotatedelay = millis();
}
if (state == 4 && rotate == 0 && red == 255 && green == 0 && blue == 0 && millis() - rotatedelay > 1){
rotate = 1;
}
if (state == 4 && rotate == 1 && blue < 255 && millis() - rotatedelay > 1){
blue = blue + fadegap;
if (red >= 1){
red = red - fadegap;}
if (green >= 1){
green = green - fadegap;}
colorBlock(strip.Color(red, blue, green));
rotatedelay = millis();
}
if (state == 4 && rotate == 1 && red == 0 && green == 0 && blue == 255 && millis() - rotatedelay > 1){
rotate = 2;
}
if (state == 4 && rotate == 2 && green < 255 && millis() - rotatedelay > 1){
green = green + fadegap;
if (red >= 1){
red = red - fadegap;}
if (blue >= 1){
blue = blue - fadegap;}
colorBlock(strip.Color(red, blue, green));
rotatedelay = millis();
}
if (state == 4 && rotate == 2 && blue == 0 && red == 0 && green == 255 && millis() - rotatedelay > 1){
rotate = 0;
}
}
//Solid colour
void colorBlock(uint32_t c) { //This seems to be the way to colour the whole strip
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
}
}
if (state == 4 &&
statements and insertSerial.println(
statements for debuggingif (blue >= 1) blue -= fadegap;