Skip to content

Instantly share code, notes, and snippets.

@amicojeko
Created November 28, 2020 18:07
Show Gist options
  • Save amicojeko/9a4f3428fffe74e9361516af78618dc5 to your computer and use it in GitHub Desktop.
Save amicojeko/9a4f3428fffe74e9361516af78618dc5 to your computer and use it in GitHub Desktop.
RGB Led effects experiment
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
int buttonPin = 2;
int buttonState = 0;
int lastButtonState = 0;
char cmdBuffer[2];
int counter = 0;
float hue, saturation, brightness;
int h,s,br;
int hTo,sTo,brTo;
int r,g,b;
int rTo,gTo,bTo;
int from = 0, to = 0;
int mode = 0;
void setup() // run once, when the sketch starts
{
Serial.begin(57600);
randomSeed(123);
pinMode(buttonPin, INPUT);
}
void loop()
{
switch(mode){
case 0:
break;
case 1:
HSBcyclicColor();
break;
case 2:
candleColor();
break;
case 3:
waterColor();
break;
case 4:
sirenColor();
break;
case 5:
redSiren();
break;
case 6:
policeLight();
break;
case 7:
moodColor();
break;
case 8:
strobeLight();
break;
}
setLedColor();
checkButtonState();
//checkSerial();
setMode(1);
}
void setMode(int m){
r=g=b=h=s=br=0;
if (m > 8) m = 0;
mode = m;
}
void setLedColor(){
analogWrite(redPin, r);
analogWrite(greenPin, g);
analogWrite(bluePin, b);
}
void checkButtonState(){
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) setMode(mode+1);
lastButtonState = buttonState;
}
}
void checkSerial(){
char incomingByte;
}
void candleColor(){
hue = random(110, 135)/1000.0;
brightness = random(500, 1000)/1000.0;
saturation = 1;
hsb2rgb();
delay(random(30, 100));
}
void waterColor(){
saturation = 1;
hue = constrain(h, 130, 160)/255.0;
hTo = constrain(hTo, 130, 160);
if (hTo == h){
brightness = random(750, 1000)/1000.0;
hTo = random(130, 160);
delay(random(30, 100));
}
HSBfade();
delay(random(5, 10));
}
void moodColor(){
brightness = brTo = 1;
saturation = sTo = 1;
if (hTo == h) hTo = random(0, 255);
HSBfade();
delay(100);
}
void sirenColor(){
if (counter > 1) counter = 0;
int colors[] = {0, 660};
hue = colors[counter]/1000.0;
saturation = 1;
for(int i = 0; i<1000; i++){
brightness = i / 1000.0;
hsb2rgb();
setLedColor();
}
for(int i = 1000; i>0; i--){
brightness = i / 1000.0;
hsb2rgb();
setLedColor();
}
counter++;
}
void redSiren(){
hue = 0;
saturation = 1;
if (br == 0) {brTo = 255;delay(100);}
if (br == 255) brTo = 0;
Serial.println(br);
HSBfade();
}
void strobeLight(){
r = g = b = 255;
setLedColor();
delay(10);
r = g = b = 0;
setLedColor();
delay(100);
}
void policeLight(){
b = 255;
r = g = 0;
setLedColor();
delay(20);
r = g = b = 0;
setLedColor();
delay(80);
b = 255;
setLedColor();
delay(20);
r = g = b = 0;
setLedColor();
delay(500);
}
void HSBcyclicColor(){
if (counter > 1000) counter = 0;
hue = (counter%1000)/1000.0;
brightness = 1;
saturation = 1;
hsb2rgb();
counter++;
delay(10);
}
void hsb2rgb(){
float h2, f, p, q, t;
if (saturation == 0) {
r = g = b = (int) (brightness * 255.0f + 0.5f);
}
else {
h2 = (hue - (int)hue) * 6.0f;
f = h2 - (int)(h2);
p = brightness * (1.0f - saturation);
q = brightness * (1.0f - saturation * f);
t = brightness * (1.0f - (saturation * (1.0f - f)));
switch ((int) h2) {
case 0:
r = (int) (brightness * 255.0f + 0.5f);
g = (int) (t * 255.0f + 0.5f);
b = (int) (p * 255.0f + 0.5f);
break;
case 1:
r = (int) (q * 255.0f + 0.5f);
g = (int) (brightness * 255.0f + 0.5f);
b = (int) (p * 255.0f + 0.5f);
break;
case 2:
r = (int) (p * 255.0f + 0.5f);
g = (int) (brightness * 255.0f + 0.5f);
b = (int) (t * 255.0f + 0.5f);
break;
case 3:
r = (int) (p * 255.0f + 0.5f);
g = (int) (q * 255.0f + 0.5f);
b = (int) (brightness * 255.0f + 0.5f);
break;
case 4:
r = (int) (t * 255.0f + 0.5f);
g = (int) (p * 255.0f + 0.5f);
b = (int) (brightness * 255.0f + 0.5f);
break;
case 5:
r = (int) (brightness * 255.0f + 0.5f);
g = (int) (p * 255.0f + 0.5f);
b = (int) (q * 255.0f + 0.5f);
break;
}
};
}
void RGBfade(){
if (r<rTo){r++;}; if (r>rTo){r--;}
if (g<gTo){g++;}; if (g>gTo){g--;}
if (b<bTo){b++;}; if (b>bTo){b--;}
}
void HSBfade(){
h = hue * 255.0f;
br = brightness * 255.0f;
s = saturation * 255.0f;
if (h<hTo){h++;}; if (h>hTo){h--;}
if (s<sTo){s++;}; if (s>sTo){s--;}
if (br<brTo){br++;}; if (br>brTo){br--;}
hue = h/255.0f;
saturation = s/255.0f;
brightness = br/255.0f;
hsb2rgb();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment