Skip to content

Instantly share code, notes, and snippets.

@rab
Created January 20, 2013 03:13
Show Gist options
  • Save rab/4576462 to your computer and use it in GitHub Desktop.
Save rab/4576462 to your computer and use it in GitHub Desktop.
Arduino Digispark Sketch for cycling the RGB Shield
const int ledRed = 0;
const int ledGreen = 1;
const int ledBlue = 2;
const unsigned long delayTime = 20;
// Base the cycle on the graph ( http://upload.wikimedia.org/wikipedia/commons/5/5d/HSV-RGB-comparison.svg )
// at http://en.wikipedia.org/wiki/HSL_and_HSV#From_HSV
int maxVal = 127;
int minVal = 0;
int redVal = maxVal;
int greenVal = minVal;
int blueVal = minVal;
int phases[][3] = { // [ delta_r, delta_g, delta_b ]
{ 0, 1, 0 }
,{-1, 0, 0 }
,{ 0, 0, 1 }
,{ 0,-1, 0 }
,{ 1, 0, 0 }
,{ 0, 0,-1 }
};
int phase = 0;
int max_phase = sizeof(phases) / sizeof(phases[0]);
unsigned long last_step;
void setup() {
// initialize the LED pins as outputs
pinMode(ledRed, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledBlue, OUTPUT);
set_rgb(redVal, greenVal, blueVal);
last_step = millis();
}
void set_rgb(int red, int green, int blue) {
analogWrite( ledRed, red );
analogWrite( ledGreen, green );
analogWrite( ledBlue , blue );
}
void step(void) {
int dr = phases[phase][0];
int dg = phases[phase][1];
int db = phases[phase][2];
redVal += dr;
greenVal += dg;
blueVal += db;
if ((redVal == maxVal || redVal == minVal) &&
(greenVal == maxVal || greenVal == minVal) &&
(blueVal == maxVal || blueVal == minVal)) {
phase = (phase + 1) % max_phase;
}
}
void loop() {
unsigned long now = millis();
if (now - last_step >= delayTime) {
step();
set_rgb(redVal, greenVal, blueVal);
last_step = now;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment