Skip to content

Instantly share code, notes, and snippets.

@harrison314
Created January 16, 2022 16:30
Show Gist options
  • Save harrison314/1a9c541cc4f78161a0aa623609f78cf6 to your computer and use it in GitHub Desktop.
Save harrison314/1a9c541cc4f78161a0aa623609f78cf6 to your computer and use it in GitHub Desktop.
Chrismas tree lights with RGB led and ESP8266
#include <Arduino.h>
#include <math.h>
#define LED_W 14
#define LED_R 15
#define LED_G 13
#define LED_B 12
#define BUZZER 5
#define RELAY 16
#define DTH_DATA 4
#define S2 0
#define S3 2
#define GPIO0 0
#define GPIO2 2
#define GPIO4 4
#define GPIO12 12
#define GPIO14 14
#define GPIO15 15
#define GPIO13 13
#define GPIO16 16
//#define ICACHE_RAM_ATTR
unsigned char lValue = 3;
unsigned char dValue = 1;
unsigned char valueChanged = 0;
// random register
// https://github.com/marvinroger/ESP8266TrueRandom/blob/master/ESP8266TrueRandom.cpp#L24
/*
int get_true_random(int max)
{
int value = (int)RANDOM_REG32;
if (value < 0) value = 0 - value;
return value % max;
}
*/
void setup() {
Serial.begin(115200);
pinMode(LED_R, OUTPUT);
pinMode(LED_G, OUTPUT);
pinMode(LED_B, OUTPUT);
pinMode(LED_W, OUTPUT);
pinMode(BUZZER, OUTPUT);
pinMode(RELAY, OUTPUT);
digitalWrite(BUZZER, LOW);
digitalWrite(RELAY, LOW);
//mode FALLING|CHANGE| RISING
pinMode(S2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(S2), handleS2, FALLING);
pinMode(S3, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(S3), handleS3, FALLING);
}
void loop() {
if (valueChanged == 1)
{
valueChanged = 0;
Serial.printf("Value changed. light: %i, delay: %i\n", (int)lValue, (int)dValue);
analogWrite(LED_R, 0);
analogWrite(LED_G, 0);
analogWrite(LED_B, 0);
for(int i=0;i<dValue;i++)
{
analogWrite(LED_R, 1024);
delay(500);
analogWrite(LED_R, 0);
delay(500);
}
}
#define PWM_CONST 4
#define CHECK_ABORT() if (valueChanged == 1) return
if(dValue == 1)
{
for (int val = 255; val > 0; val--) {
CHECK_ABORT();
analogWrite(LED_B, val*PWM_CONST);
analogWrite(LED_R, (255-val)*PWM_CONST);
delay(15);
}
for (int val = 0; val < 255; val++) {
CHECK_ABORT();
analogWrite(LED_B, val*PWM_CONST);
analogWrite(LED_R, (255-val)*PWM_CONST);
delay(15);
}
}
else if(dValue == 2)
{
float scale = 0.5 + ((float) secureRandom(500)) / 1000.0f;
scale *= ((float)lValue) / 9.0f;
int r = (int)(1024 * scale);
int g = (int)((secureRandom(250) + 50) * scale);
analogWrite(LED_R, r);
analogWrite(LED_G, g);
//analogWrite(LED_B, g/3);
delay(secureRandom(150)+200);
}
else if(dValue == 2)
{
float scale = 0.5 + ((float) secureRandom(500)) / 1000.0f;
scale *= ((float)lValue) / 9.0f;
int r = (int)(1024 * scale);
analogWrite(LED_R, r);
delay(secureRandom(150)+200);
}
else if(dValue == 3)
{
float scale = 0.5 + ((float) secureRandom(500)) / 1000.0f;
scale *= ((float)lValue) / 9.0f;
int r = (int)(1024 * scale);
analogWrite(LED_G, r);
delay(secureRandom(150)+200);
}
else if(dValue == 4)
{
float scale = 0.5 + ((float) secureRandom(500)) / 1000.0f;
scale *= ((float)lValue) / 9.0f;
int r = (int)(1024 * scale);
analogWrite(LED_B, r);
delay(secureRandom(150)+200);
}
}
ICACHE_RAM_ATTR void handleS2(void)
{
lValue++;
if (lValue > 10) lValue = 0;
valueChanged = 1;
}
ICACHE_RAM_ATTR void handleS3(void)
{
dValue++;
if (dValue > 4)
{
dValue = 1;
}
valueChanged = 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment