Last active
August 5, 2016 00:38
-
-
Save yock/c99576504e0d36eb1bdb00072a48066a to your computer and use it in GitHub Desktop.
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
typedef struct Color { | |
int red, green, blue; | |
}; | |
const Color red = {255, 0, 0}; | |
const Color green = {0, 255, 0}; | |
const Color white = {255, 255, 255}; | |
const Color yellow = {255, 255, 0}; | |
const Color orange = {255, 100, 0}; | |
const Color off = {0, 0, 0}; | |
typedef struct Led { | |
int pin_r, pin_g, pin_b; | |
}; | |
const Led myLed = {9, 10, 11}; | |
void set_color(Color c, Led l, float intensity) { | |
if(intensity > 1.0) { | |
intensity = 1.0; | |
} | |
else if(intensity < 0.0) { | |
intensity = 0.0; | |
} | |
analogWrite(l.pin_r, c.red * intensity); | |
analogWrite(l.pin_g, c.green * intensity); | |
analogWrite(l.pin_b, c.blue * intensity); | |
} | |
void set_color(Color c, Led l) { | |
set_color(c, l, 1); | |
} | |
void pulse(Color c, Led l) { | |
Serial.println("pulse"); | |
unsigned long start = millis(); | |
unsigned long end_millis = start + 1000; | |
while(millis() < end_millis) { | |
int elapsed = millis() - start; | |
float intensity = ((1000 - elapsed) / 1000.0); | |
set_color(c, l, intensity); | |
} | |
} | |
void pulse(Color c, Led l, int times) { | |
for(times; times > 0; times--) { | |
pulse(c, l); | |
} | |
} | |
void setup() { | |
Serial.begin(9600); | |
Serial.println("Beginning..."); | |
pinMode(myLed.pin_r, OUTPUT); | |
pinMode(myLed.pin_g, OUTPUT); | |
pinMode(myLed.pin_b, OUTPUT); | |
} | |
void loop() { | |
pulse(green, myLed, 5); | |
pulse(orange, myLed, 5); | |
set_color(red, myLed); | |
delay(10000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment