Hello!
I am using an IR sensor with NeoPixels. I've read that they don't work together, so it's totally fine if it never works. By pressing the buttons on the IR remote, the Arduino reads it as random values. I don't want to buy a new/different microcontroller, and just use the Arduino Uno I'm using right now.
Results (using the
button on a ELEGOO remote - correct result: FF02FD):
B373A3D9
56683DF2
4F1A0EEB
20B77E35
700C4478
FF70FB3F
EA3C030C
B056623
FF70FB3F
14F91DFA
DD10C53A
8B918E56
9D0F72DC
5F3354E0
5F3354E0
56683DF2
EA3C030C
320A3AE1
639A63C6
2C72AD18
D69F0B8D
C4E3E9F0
E6ABD73E
636BFEED
69D1242C
FCEA6E4D
A35E290C
636BFEED
5F6AB56
DBA21AD0
869BD7E3
3B49FBFB
6F55DF52
DD10C53A
8A7C984D
639A63C6
E31807CD
C446ACCA
36D77AAC
6FB11D68
DD10C53A
70017C6F
2A52E734
2FA9B7E5
EEFF3B17
A04EF9CC
915D309
DD10C53A
52D96E8E
F42E2A88
FFFFFFFF
4428A2B4
9BED9C35
73C96121
8A7C984D
DAE299D
9E4601F0
70C225F7
89E0486A
3E3A7424
FC4C5AE
7B1D4243
Full code:
/*
NeoPixel Hue Rotater
by Max Parisi
Credit to Adrianotiger for the NeoPixel code (generated from https://adrianotiger.github.io/Neopixel-Effect-Generator/)
Rotates a hue around a 52-led NeoPixel strip connected to pin 8. Use button connected to pin 2 for 'on/off,' pot connected pin A0 for brightness, pot connected to A1 for speed. Wiring on https://www.tinkercad.com/things/jzduDRLzSIJ-neopixel-hue-rotater.
*/
#include <Adafruit_NeoPixel.h>
#include <dht.h>
#include <IRremote.h>
class Strip
{
public:
uint8_t effect;
uint8_t effects;
uint16_t effStep;
unsigned long effStart;
Adafruit_NeoPixel strip;
Strip(uint16_t leds, uint8_t pin, uint8_t toteffects, uint16_t striptype) : strip(leds, pin, striptype) {
effect = -1;
effects = toteffects;
Reset();
}
void Reset() {
effStep = 0;
effect = (effect + 1) % effects;
effStart = millis();
}
};
struct Loop
{
uint8_t currentChild;
uint8_t childs;
bool timeBased;
uint16_t cycles;
uint16_t currentTime;
Loop(uint8_t totchilds, bool timebased, uint16_t tottime) {
currentTime = 0;
currentChild = 0;
childs = totchilds;
timeBased = timebased;
cycles = tottime;
}
};
const int neoLeds = 50;
const int neoPin = 8;
const int neo2Leds = 5;
const int neo2Pin = 7;
const int btnPin = 2;
const int brightnessPin = 0;
const int pot2Pin = 1;
const int ledPin = 5;
const int buzzPin = 3;
const int dhtPin = 4;
const int irPin = 6;
const int switchLedPin = LED_BUILTIN;
int btnState, brightnessState, pot2State, brightness, neoSpeed, millisOffset, pixelNum, chk, prePixelNum, hue;
int mode = 1;
byte rgb[3];
float temp, rh, preTemp, preRh;
Strip NeoPixel(neoLeds, neoPin, neoLeds, NEO_GRB + NEO_KHZ800);
struct Loop strip0loop0(1, false, 1);
Strip NeoPixel2(neo2Leds, neo2Pin, neo2Leds, NEO_GRB + NEO_KHZ800);
dht DHT;
IRrecv irrecv(irPin);
decode_results results;
void setup() {
pinMode(btnPin, INPUT_PULLUP);
pinMode(brightnessPin, INPUT);
pinMode(pot2Pin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(switchLedPin, OUTPUT);
pinMode(irPin, INPUT);
Serial.begin(9600);
Serial.println("Power On\n");
NeoPixel.strip.begin();
NeoPixel2.strip.begin();
irrecv.enableIRIn();
digitalWrite(switchLedPin, HIGH);
delay(1000);
digitalWrite(switchLedPin, LOW);
led(0);
strip_off();
strip2_off();
test();
strip_off();
strip2_off();
pixel2_up_to(1, 0, 0, 255);
}
void loop() {
chk = DHT.read11(dhtPin);
temp = DHT.temperature;
rh = DHT.humidity;
brightnessState = pot_to_value(analogRead(brightnessPin));
brightness = value_to_brightness(brightnessState);
pot2State = pot_to_value(analogRead(pot2Pin));
btnState = !digitalRead(btnPin);
if (btnState) {
while (btnState) {
btnState = digitalRead(btnPin);
}
mode++;
while (mode > 5) {
mode -= 5;
}
strip2_off();
pixel2_up_to(mode, 0, 0, 255);
if (mode == 3) {
hue = 0;
}
if (mode == 5) {
preTemp = temp + 1;
preRh = rh + 1;
}
buzz(500);
delay(500);
}
switch (mode) {
case 1:
all(255, 255, 255);
led(value_to_led(pot2State));
break;
case 2:
led(0);
value_to_rgb(pot2State, rgb);
all(rgb[0], rgb[1], rgb[2]);
break;
case 3:
led(value_to_led(pot2State));
value_to_rgb(hue, rgb);
all(rgb[0], rgb[1], rgb[2]);
hue += 8;
if (hue > 1023) {
hue -= 1023;
}
break;
case 4:
strips_loop();
led(value_to_led(pot2State));
break;
case 5:
led(value_to_led(pot2State));
if (!(temp == preTemp) || !(rh == preRh)) {
all(5, 5, 0);
pixel(temp_to_pixel(temp), 255, 0, 0);
pixel(rh_to_pixel(rh), 0, 0, 255);
preTemp = temp;
preRh = rh;
}
break;
default:
error("The variable 'mode' went over maximum value.");
break;
}
NeoPixel.strip.setBrightness(brightness);
NeoPixel.strip.show();
if (brightness <= 0) {
NeoPixel2.strip.setBrightness(1);
} else {
NeoPixel2.strip.setBrightness(brightness);
}
NeoPixel2.strip.show();
if (irrecv.decode(&results)) {
translateIR();
irrecv.resume();
}
}
void strips_loop() {
if (strip0_loop0() & 0x01)
NeoPixel.strip.show();
}
uint8_t strip0_loop0() {
uint8_t ret = 0x00;
switch (strip0loop0.currentChild) {
case 0:
ret = strip0_loop0_eff0(); break;
}
if (ret & 0x02) {
ret &= 0xfd;
if (strip0loop0.currentChild + 1 >= strip0loop0.childs) {
strip0loop0.currentChild = 0;
if (++strip0loop0.currentTime >= strip0loop0.cycles) {
strip0loop0.currentTime = 0;
ret |= 0x02;
}
}
else {
strip0loop0.currentChild++;
}
};
return ret;
}
uint8_t strip0_loop0_eff0() {
// Strip ID: 0 - Effect: Rainbow - LEDS: 52
// Steps: 52 - Delay: 20
// Colors: 3 (255.0.0, 0.255.0, 0.0.255)
// Options: rainbowlen=52, toLeft=true,
if (millis() - NeoPixel.effStart < 52 * (NeoPixel.effStep)) return 0x00;
float factor1, factor2;
uint16_t ind;
for (uint16_t j = 0; j < 52; j++) {
ind = NeoPixel.effStep + j * 1;
switch ((int)((ind % 52) / 17.333333333333332)) {
case 0: factor1 = 1.0 - ((float)(ind % 52 - 0 * 17.333333333333332) / 17.333333333333332);
factor2 = (float)((int)(ind - 0) % 52) / 17.333333333333332;
NeoPixel.strip.setPixelColor(j, 255 * factor1 + 0 * factor2, 0 * factor1 + 255 * factor2, 0 * factor1 + 0 * factor2);
break;
case 1: factor1 = 1.0 - ((float)(ind % 52 - 1 * 17.333333333333332) / 17.333333333333332);
factor2 = (float)((int)(ind - 17.333333333333332) % 52) / 17.333333333333332;
NeoPixel.strip.setPixelColor(j, 0 * factor1 + 0 * factor2, 255 * factor1 + 0 * factor2, 0 * factor1 + 255 * factor2);
break;
case 2: factor1 = 1.0 - ((float)(ind % 52 - 2 * 17.333333333333332) / 17.333333333333332);
factor2 = (float)((int)(ind - 34.666666666666664) % 52) / 17.333333333333332;
NeoPixel.strip.setPixelColor(j, 0 * factor1 + 255 * factor2, 0 * factor1 + 0 * factor2, 255 * factor1 + 0 * factor2);
break;
}
}
if (NeoPixel.effStep >= 52) {
NeoPixel.Reset();
return 0x03;
}
else NeoPixel.effStep++;
return 0x01;
}
int value_to_brightness(int value) {
if (value >= 1000) {
return 255;
} else {
int brightness = map(value, 0, 1024, 0, 100);
if (brightness < 0) {
error("In 'value_to_brightness()', 'brightness' is less than 0.");
}
if (brightness > 255) {
error("In 'value_to_brightness()', 'brightness' is greater than 255.");
}
return brightness;
}
}
int pot_to_value(int pot) {
int value = map(pot, 200, 800, 0, 1024);
if (value < 0) {
value = 0;
}
if (value > 1024) {
value = 1024;
}
return value;
}
int value_to_led(int value) {
return map(value, 0, 1024, 0, 100);
}
int value_to_rgb(int value, byte rgb[]) {
float H = map(value, 0, 1024, 0, 360);
float C = 1;
float X = 1 - abs(fmod(H / 60.0, 2) - 1);
float m = 0;
float r, g, b;
if (H >= 0 && H < 60) {
r = C, g = X, b = 0;
} else if (H >= 60 && H < 120) {
r = X, g = C, b = 0;
} else if (H >= 120 && H < 180) {
r = 0, g = C, b = X;
} else if (H >= 180 && H < 240) {
r = 0, g = X, b = C;
} else if (H >= 240 && H < 300) {
r = X, g = 0, b = C;
} else {
r = C, g = 0, b = X;
}
r = r * 255;
g = g * 255;
b = b * 255;
if (r < 0) {
error("In 'value_to_rgb()', 'r' is less than 0.");
}
if (r > 255) {
error("In 'value_to_rgb()', 'r' is greater than 255.");
}
if (g < 0) {
error("In 'value_to_rgb()', 'r' is less than 0.");
}
if (g > 255) {
error("In 'value_to_rgb()', 'r' is greater than 255.");
}
if (b < 0) {
error("In 'value_to_rgb()', 'r' is less than 0.");
}
if (b > 255) {
error("In 'value_to_rgb()', 'r' is greater than 255.");
}
rgb[0] = r;
rgb[1] = g;
rgb[2] = b;
}
int time_left_to_pixel(int milli, int milli_left) {
int pixels = map(milli_left, 0, milli, 1, (neoLeds - 1));
if (pixels < 0) {
error("In 'time_left_to_pixels()', 'pixel' is less than 1.");
}
if (pixels > (neoLeds - 1)) {
error("In 'time_to_pixel()', 'pixels' is greater than neoLeds (minus 1 due to zero indexing).");
}
return pixels;
}
int temp_to_pixel(int temp) {
int pixel = map(temp, 15, 40, 0, (neoLeds - 1));
if (pixel < 0) {
pixel = 0;
}
if (pixel > (neoLeds - 1)) {
pixel = neoLeds - 1;
}
return pixel;
}
int rh_to_pixel(int rh) {
int pixel = map(rh, 30, 90, 0, (neoLeds - 1));
if (pixel < 0) {
pixel = 0;
}
if (pixel > (neoLeds - 1)) {
pixel = neoLeds - 1;
}
return pixel;
}
int pixel(int pixel, int r, int g, int b) {
if (pixel < 0) {
error("On function 'pixel()' first parameter (the pixel #) was above 0.");
}
if (pixel > (neoLeds - 1)) {
error("On function 'pixel()' first parameter (the pixel #) was above LEDs on the NeoPixel Strip (minus 1 because of zero indexing).");
}
NeoPixel.strip.setPixelColor(pixel, NeoPixel.strip.Color(r, g, b));
NeoPixel.strip.show();
}
int pixel2(int pixel, int r, int g, int b) {
if (pixel < 0) {
error("On function 'pixel2()' first parameter (the pixel #) was above 0.");
}
if (pixel > (neo2Leds - 1)) {
error("On function 'pixel2()' first parameter (the pixel #) was above LEDs on the NeoPixel Strip #2 (minus 1 because of zero indexing).");
}
NeoPixel2.strip.setPixelColor(pixel, NeoPixel2.strip.Color(r, g, b));
NeoPixel2.strip.show();
}
int pixel_up_to(int pixels, int r, int g, int b) {
for (int i = 0; i < pixels; i++) {
NeoPixel.strip.setPixelColor(i, NeoPixel.strip.Color(r, g, b));
}
NeoPixel.strip.show();
}
int pixel2_up_to(int pixels, int r, int g, int b) {
for (int i = 0; i < pixels; i++) {
NeoPixel2.strip.setPixelColor(i, NeoPixel2.strip.Color(r, g, b));
}
NeoPixel2.strip.show();
}
int buzz(int duration) {
if (duration < 0) {
error("On function 'buzz()' 'duration' is less than 0..");
}
tone(buzzPin, 100, duration);
}
void translateIR() {
Serial.println(results.value, HEX);
}
int error(String info) {
NeoPixel.strip.setBrightness(255);
NeoPixel2.strip.setBrightness(255);
led(0);
Serial.print("ERROR: ");
Serial.println(info);
while (!(digitalRead(btnPin))) {
buzz(1000);
all(255, 0, 0);
all2(255, 0, 0);
digitalWrite(switchLedPin, HIGH);
delay(1000);
strip_off();
strip2_off();
digitalWrite(switchLedPin, LOW);
delay(1000);
}
Serial.println("Error Ignored");
}
void strip_off() {
all(0, 0, 0);
NeoPixel.strip.show();
}
int all(int r, int g, int b) {
for (int i = 0; i < neoLeds; i++) {
NeoPixel.strip.setPixelColor(i, NeoPixel.strip.Color(r, g, b));
}
NeoPixel.strip.show();
}
void strip2_off() {
all2(0, 0, 0);
NeoPixel2.strip.show();
}
int all2(int r, int g, int b) {
for (int i = 0; i < neo2Leds; i++) {
NeoPixel2.strip.setPixelColor(i, NeoPixel2.strip.Color(r, g, b));
}
NeoPixel2.strip.show();
}
int led(int dutyCycle) {
analogWrite(ledPin, map(dutyCycle, 0, 100, 0, 255));
}
void test() {
Serial.println("~~~ Led Test ~~~");
NeoPixel.strip.setBrightness(255);
NeoPixel2.strip.setBrightness(255);
buzz(500);
Serial.println(" Red");
for (int i = 0; i < neoLeds; i++) {
pixel(i, 255, 0, 0);
delay(500 / neoLeds);
}
delay(250);
buzz(500);
Serial.println(" Green");
for (int i = 0; i < neoLeds; i++) {
pixel(i, 0, 255, 0);
delay(500 / neoLeds);
}
delay(250);
buzz(500);
Serial.println(" Blue");
for (int i = 0; i < neoLeds; i++) {
pixel(i, 0, 0, 255);
delay(500 / neoLeds);
}
delay(250);
buzz(500);
Serial.println(" Yellow");
for (int i = 0; i < neoLeds; i++) {
pixel(i, 255, 255, 0);
delay(500 / neoLeds);
}
delay(250);
buzz(500);
Serial.println(" White");
for (int i = 0; i < neoLeds; i++) {
pixel(i, 255, 255, 255);
delay(500 / neoLeds);
}
Serial.println(" LED");
led(100);
delay(1000);
strip_off();
strip2_off();
led(0);
buzz(500);
delay(1000);
Serial.println("Done with test\n");
}
Any help would be appreciated!
tinkerer9