Last active
December 22, 2015 02:29
-
-
Save shaiwininger/6404161 to your computer and use it in GitHub Desktop.
The code used to create a Spectral Radiation Cell using Arduino and RGB LED
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
//set the color pins | |
#define RED 0 | |
#define GREEN 1 | |
#define BLUE 2 | |
//set the pause button pins... | |
#define PAUSE_BUTTON 8 | |
//MicroSeconds between each color... | |
#define SPEED 3000 | |
boolean locked; | |
int last_color[] = {0,0,0}; | |
int last_btn_state; | |
int power_multiplier = 9; | |
void setup () { | |
//set the color pins | |
//set the RED pin | |
pinMode(RED, OUTPUT); | |
//set the GREEN pin | |
pinMode(GREEN, OUTPUT); | |
//set the BLUE pin | |
pinMode(BLUE, OUTPUT); | |
//set the button... | |
//set the VOLTAGE pin | |
//set the pin of the serial INPUT | |
pinMode(PAUSE_BUTTON,INPUT); | |
clear_all(); | |
//Serial.begin(9600); | |
} | |
void clear_all(){ | |
digitalWrite(RED, HIGH); | |
digitalWrite(GREEN, HIGH); | |
digitalWrite(BLUE, HIGH); | |
} | |
void light_color(int red , int green , int blue , int brightness) { | |
if(red > 0){ | |
digitalWrite(RED, LOW); | |
delayMicroseconds(red * power_multiplier); | |
digitalWrite(RED, HIGH); | |
} | |
if (green > 0) { | |
digitalWrite(GREEN, LOW); | |
delayMicroseconds(green * power_multiplier); | |
digitalWrite(GREEN, HIGH); | |
} | |
if (blue > 0){ | |
digitalWrite(BLUE, LOW); | |
delayMicroseconds(blue * power_multiplier); | |
digitalWrite(BLUE, HIGH); | |
} | |
// add white (brightness) | |
if (brightness > 0) { | |
digitalWrite(RED, LOW); | |
digitalWrite(GREEN, LOW); | |
digitalWrite(BLUE, LOW); | |
delayMicroseconds(brightness); | |
} | |
clear_all(); | |
} | |
void set_last_color(int r, int g, int b){ | |
last_color[0] = r; | |
last_color[1] = g; | |
last_color[2] = b; | |
} | |
boolean detect_pause_click(){ | |
int btn_state = digitalRead(PAUSE_BUTTON); | |
if(btn_state == HIGH && last_btn_state != btn_state){ | |
locked = !locked; | |
last_btn_state = btn_state; | |
return true; | |
} | |
last_btn_state = btn_state; | |
return false; | |
} | |
void loop () { | |
if (locked){ | |
light_color(last_color[0],last_color[1],last_color[2],0); | |
delayMicroseconds (SPEED); | |
detect_pause_click(); | |
return; | |
} | |
// from r -> rb | |
for(int i = 0;i<256;i++){ | |
// blue value | |
light_color(255,0,i,0); | |
set_last_color(255,0,i); | |
if (detect_pause_click()){return;} | |
delayMicroseconds (SPEED); | |
} | |
// from rb -> b | |
for(int i = 255;i>-1;i--){ | |
// blue value | |
light_color(i,0,255,0); | |
set_last_color(i,0,255); | |
if (detect_pause_click()){return;} | |
delayMicroseconds (SPEED); | |
} | |
// from b -> bg | |
for(int i = 0;i<256;i++){ | |
// blue value | |
light_color(0,i,255,0); | |
set_last_color(0,i,255); | |
if (detect_pause_click()){return;} | |
delayMicroseconds (SPEED); | |
} | |
// from bg -> g | |
for(int i = 255;i>-1;i--){ | |
// blue value | |
light_color(0,255,i,0); | |
set_last_color(0,255,i); | |
if (detect_pause_click()){return;} | |
delayMicroseconds (SPEED); | |
} | |
// from g -> gr | |
for(int i = 0;i<256;i++){ | |
// blue value | |
light_color(i,255,0,0); | |
set_last_color(i,255,0); | |
if (detect_pause_click()){return;} | |
delayMicroseconds (SPEED); | |
} | |
// from gr -> r | |
for(int i = 255;i>-1;i--){ | |
// blue value | |
light_color(255,i,0,0); | |
set_last_color(255,i,0); | |
if (detect_pause_click()){return;} | |
delayMicroseconds (SPEED); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment