Hi guys,
I'm trying to be able to define custom colors for RGB LEDs using Neopixel library and a rotary encoders.
I'd like to be able to run through the whole color spectrum with the encoder as if it was a rainbow mode, and once found the color, to save those values.
I didn't know how I could do it so I tried to adapt a code already existing for a rainbow mode. This is the source: Speed up rgb led strip rainbow wave
Does this code make sense and is it functional?
#include <EEPROM.h>
#define mAddress 0 //Address for mode
#define cmAddress 1 //Address for colorMode
#define brAddress 2 //Address for bright
#define rAddress 3 //Address for red
#define gAddress 4 //Address for green
#define bAddress 5 //Address for blue
include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 15
#define NUMPIXELS 16
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
bool colorUpdate = false;
#include <Encoder.h>
Encoder encoder1(A5, A4); //Encoder channels for RE1 (Left)
Encoder encoder2(A2, A1); //Encoder channels for RE2 (Right)
const int encoder1Button = A3; //ER1 Pushbutton pin Left
const int encoder2Button = A0; //ER2 Pushbutton pin Right
unsigned long timer = 2000;
unsigned long prevMillis1;
unsigned long prevMillis2;
unsigned long currentMillis;
int mode = 0; //counter for left encoder button ER1
int colorMode = 0; //counter for right encoder button ER2
int button1State = 0; //current state of left encoder button
int lastButton1State; //last state of left encoder button
int button2State = 0; //current state of right encoder button
int lastButton2State; //last state of right encoder button
long encoder1Pos = -999; //ER1A Left position variable
long encoder2Pos = -999; //ER1B Right position variable
uint8_t r; //Red color
uint8_t g; //Green color
uint8_t b; //Blue color
uint8_t bright = 126; //Led brightness
void setup() {
pinMode(encoder1Button, INPUT_PULLUP);
pinMode(encoder2Button, INPUT_PULLUP);
Serial.begin(9600);
pixels.begin();
prevMillis1 = millis();
prevMillis2 = millis();
mode = EEPROM.read(mAddress); //Read values saved in EEPROM memory
colorMode = EEPROM.read(cmAddress);
bright = EEPROM.read(brAddress);
r = EEPROM.read(rAddress);
g = EEPROM.read(gAddress);
b = EEPROM.read(bAddress);
}
void loop() {
brightness();
checkColorMode();
}
void brightness() {
while (colorUpdate){
long newPos = encoder1.read() / 4;
if (newPos != encoder1Pos && newPos > encoder1Pos) {
encoder1Pos = newPos;
bright += 32;
if (bright > 255) {bright = 255;}
setBrightness(bright);
}
if (newPos != encoder1Pos && newPos < encoder1Pos) {
encoder1Pos = newPos;
bright -= 32;
if (bright < 0) {bright = 0;}
setBrightness(bright);
}
EEPROM.update(bAddress, bright);
}
}
void checkColorMode() {
button2State = digitalRead(encoder2Button);
currentMillis = millis();
bool activation = false;
if (button2State != lastButton2State && (unsigned long)(currentMillis - prevMillis2) >= timer && !activation) {
long newPos = encoder2.read() / 4;
activation = true;
colorUpdate = true;
uint16_t j = 0;
if (newPos != encoder2Pos && newPos > encoder2Pos) {
encoder2Pos = newPos;
if (j < 256) {
for (uint16_t i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, Wheel((i + j) & 255));
}
pixels.show();
delay(20);
j += 16;
}
else { j = 0;}
}
if (newPos != encoder2Pos && newPos < encoder2Pos) {
encoder2Pos = newPos;
if (j > 0) {
for (uint16_t i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, Wheel((i + j) & 255));
}
pixels.show();
delay(20);
j -= 16;
}
else { j = 240;}
}
prevMillis2 = millis();
}
if (button2State != lastButton2State && (unsigned long)(currentMillis - prevMillis2) >= timer && activation ) {
activation = false;
colorUpdate = false;
}
if ( button2State != lastButton2State && (unsigned long)(currentMillis - prevMillis2) <= timer ) {
if (button2State == LOW) {
colorMode++;
}
}
lastButton2State = button2State;
if (colorMode > 10) {
colorMode = 0;
}
EEPROM.update(rAddress, r);
EEPROM.update(gAddress, g);
EEPROM.update(bAddress, b);
uint32_t c;
switch(colorMode){
case 0: //Red
c = pixels.Color(255, 0, 0);
break;
case 1: //Orange
c = pixels.Color(255, 128, 0);
break;
case 2: //Yellow
c = pixels.Color(255, 255, 0);
break;
case 3: //Green
c = pixels.Color(0, 255, 0);
break;
case 4: //Cyan
c = pixels.Color(0, 255, 255);
break;
case 5: //Blue
c = pixels.Color(0, 0, 255);
break;
case 6: //Purple
c = pixels.Color(127, 0, 255);
break;
case 7: //Pink
c = pixels.Color(255, 0, 255);
break;
case 8: //White
c = pixels.Color(255, 255, 255);
break;
case 9: //Custom color
c = pixels.Color(r, g, b);
break;
case 10: //Off
c = pixels.Color(0, 0, 0);
break;
}
for (int i = 0 < NUMPIXELS; i++) {
pixels.setPixelColor(i, c);
pixels.show();
delay(25);
}
EEPROM.update(cmAddress, colorMode);
}
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
r = 255 - WheelPos + 3;
g = 0;
b = WheelPos * 3;
return pixels.Color(r, g, b);
}
if(WheelPos < 170) {
WheelPos -= 85;
r = 0;
g = WheelPos * 3,
b = 255 - WheelPos * 3;
return pixels.Color(r, g, b);
}
WheelPos -= 170;
r = WheelPos + 3;
g = 255 - WheelPos * 3;
b = 0;
return pixels.Color(r, g, b);
}