Created
May 24, 2018 08:49
-
-
Save mahenzon/6eb44d102d207bc94b9f9bd1c7e87c42 to your computer and use it in GitHub Desktop.
Rainbow gleam Arduino / esp8266
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Metro library for timing: https://github.com/surik00/Arduino-Metro | |
#include <Metro.h> | |
#define RED_PIN 16 | |
#define GREEN_PIN 14 | |
#define BLUE_PIN 12 | |
#define ARR_LEN 6 | |
#define RGB_MAX 255 | |
#define MAX_STEPS ARR_LEN * RGB_MAX | |
int rgbRainbowMap[ARR_LEN][3] = { | |
{ 1, 0, 0 }, | |
{ 1, 1, 0 }, | |
{ 0, 1, 0 }, | |
{ 0, 1, 1 }, | |
{ 0, 0, 1 }, | |
{ 1, 0, 1 }, | |
}; | |
int gleamStep = MAX_STEPS; | |
Metro gleamMetro = Metro(10); // 200: 5 minutes // 2353: 1 hour | |
void setup() { | |
// Serial.begin(115200); | |
// Serial.println("Hello world!"); | |
analogWriteRange(256); | |
pinMode(RED_PIN, OUTPUT); | |
pinMode(GREEN_PIN, OUTPUT); | |
pinMode(BLUE_PIN, OUTPUT); | |
} | |
void loop(){ | |
moodLight(); | |
} | |
void moodLight() { | |
if (gleamMetro.check()) { | |
if (gleamStep >= MAX_STEPS) { | |
// Serial.print("Reset time: "); | |
// Serial.println(millis()); | |
gleamStep = 0; | |
}; | |
moodRgb(gleamStep++); | |
}; | |
}; | |
void moodRgb(int step) { | |
// Serial.print("Mood RGB step "); | |
// Serial.println(step); | |
int rgb[3] = {0, 0, 0}; | |
const int index = step / RGB_MAX; | |
const int _mod = step % RGB_MAX; | |
const int next = (index + 1 < ARR_LEN) ? index + 1 : 0; | |
for (int i = 0; i < 3; i++) { | |
const int section = rgbRainbowMap[index][i]; | |
const int nextSection = rgbRainbowMap[next][i]; | |
if (section == nextSection) | |
rgb[i] = section * RGB_MAX; | |
else if (section > nextSection) | |
rgb[i] = RGB_MAX - _mod; | |
else | |
rgb[i] = _mod; | |
}; | |
// Serial.print("["); | |
// Serial.print(rgb[0]); | |
// Serial.print(", "); | |
// Serial.print(rgb[1]); | |
// Serial.print(", "); | |
// Serial.print(rgb[2]); | |
// Serial.println("]"); | |
setRgb(rgb[0], rgb[1], rgb[2]); | |
} | |
void setRgb(int red, int green, int blue) { | |
analogWrite(RED_PIN, red); | |
analogWrite(GREEN_PIN, green); | |
analogWrite(BLUE_PIN, blue); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment