Created
December 17, 2019 04:40
-
-
Save BaReinhard/8ff2fe697ed5be24b03c996ae5fa78c7 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
/* | |
* LedBrightness sketch | |
* controls the brightness of LEDs on "analog" (PWM) output ports. | |
*/ | |
#include <LiquidCrystal.h> | |
LiquidCrystal lcd(12,11,9,4,8,7); | |
class rgb_color { | |
private: | |
int my_r; | |
int my_g; | |
int my_b; | |
public: | |
rgb_color (int red, int green, int blue) | |
: | |
my_r(red), | |
my_g(green), | |
my_b(blue) | |
{ | |
} | |
int r() const {return my_r;} | |
int b() const {return my_b;} | |
int g() const {return my_g;} | |
}; | |
/*instances of fader can fade between two colors*/ | |
class fader { | |
private: | |
int r_pin; | |
int g_pin; | |
int b_pin; | |
public: | |
/* construct the fader for the pins to manipulate. | |
* make sure these are pins that support Pulse | |
* width modulation (PWM), these are the digital pins | |
* denoted with a tilde(~) common are ~3, ~5, ~6, ~9, ~10 | |
* and ~11 but check this on your type of arduino. | |
*/ | |
fader( int red_pin, int green_pin, int blue_pin) | |
: | |
r_pin(red_pin), | |
g_pin(green_pin), | |
b_pin(blue_pin) | |
{ | |
} | |
/*fade from rgb_in to rgb_out*/ | |
void fade( const rgb_color& in, | |
const rgb_color& out, | |
unsigned n_steps = 256, //default take 256 steps | |
unsigned time = 25) //wait 10 ms per step | |
{ | |
int red_diff = out.r() - in.r(); | |
int green_diff = out.g() - in.g(); | |
int blue_diff = out.b() - in.b(); | |
for ( unsigned i = 0; i < n_steps; ++i){ | |
/* output is the color that is actually written to the pins | |
* and output nicely fades from in to out. | |
*/ | |
rgb_color output ( in.r() + i * red_diff / n_steps, | |
in.g() + i * green_diff / n_steps, | |
in.b() + i * blue_diff/ n_steps); | |
/*put the analog pins to the proper output.*/ | |
analogWrite( r_pin, output.r() ); | |
analogWrite( g_pin, output.g() ); | |
analogWrite( b_pin, output.b() ); | |
if(output.r() == 255 && output.g() == 0 && output.b() == 0){ | |
delay(time*50); | |
}else{ | |
delay(time); | |
} | |
} | |
} | |
}; | |
void setup() | |
{ | |
lcd.begin(16,2); | |
lcd.print("RGB"); | |
//pins driven by analogWrite do not need to be declared as outputs | |
} | |
void loop() | |
{ | |
lcd.setCursor(0,1); | |
fader f (3, 5, 6); | |
/*colors*/ | |
// rgb_color yellow( 250, 105, 0 ); | |
// rgb_color orange( 250, 40, 0 ); | |
// rgb_color red ( 255, 0, 0 ); | |
// rgb_color blue ( 10, 10, 255 ); | |
// rgb_color pink ( 255, 0, 100 ); | |
// rgb_color purple( 200, 0, 255 ); | |
// rgb_color green ( 0, 255, 0 ); | |
// rgb_color white ( 255, 255, 255 ); | |
lcd.print("yellow"); | |
rgb_color yellow( 255, 255, 0 ); | |
lcd.print("red"); | |
rgb_color red ( 255, 0, 0 ); | |
lcd.print("blue"); | |
rgb_color blue ( 0, 0, 255 ); | |
lcd.print("teal"); | |
rgb_color teal ( 0, 255, 255 ); | |
lcd.print("purple"); | |
rgb_color purple( 255, 0, 255 ); | |
lcd.print("green"); | |
rgb_color green ( 0, 255, 0 ); | |
/*fade colors*/ | |
f.fade( red, yellow); | |
f.fade( yellow, green); | |
f.fade( green, teal); | |
f.fade( teal, blue); | |
f.fade( blue, purple); | |
f.fade( purple, red); | |
// f.fade( white, yellow); | |
// f.fade( yellow, orange); | |
// f.fade( orange, red); | |
// f.fade( red, blue); | |
// f.fade( blue, pink); | |
// f.fade( pink, purple); | |
// f.fade( purple, green); | |
// f.fade( green, white); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment