-
-
Save jimsynz/766994 to your computer and use it in GitHub Desktop.
const int redPin = 11; | |
const int greenPin = 10; | |
const int bluePin = 9; | |
void setup() { | |
// Start off with the LED off. | |
setColourRgb(0,0,0); | |
} | |
void loop() { | |
unsigned int rgbColour[3]; | |
// Start off with red. | |
rgbColour[0] = 255; | |
rgbColour[1] = 0; | |
rgbColour[2] = 0; | |
// Choose the colours to increment and decrement. | |
for (int decColour = 0; decColour < 3; decColour += 1) { | |
int incColour = decColour == 2 ? 0 : decColour + 1; | |
// cross-fade the two colours. | |
for(int i = 0; i < 255; i += 1) { | |
rgbColour[decColour] -= 1; | |
rgbColour[incColour] += 1; | |
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]); | |
delay(5); | |
} | |
} | |
} | |
void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) { | |
analogWrite(redPin, red); | |
analogWrite(greenPin, green); | |
analogWrite(bluePin, blue); | |
} |
very nice to use make the prototype. and I can save time by using this function.
I will make a led lamp using contained register led(NeoPixel)that operate by data but
previous products are consist of normal type led to controlled ON or OFF voltage
I've implemented a non-blocking version of it using @michalmonday's algorithm for Arduino:
https://gist.github.com/surik00/6eb44d102d207bc94b9f9bd1c7e87c42
You can also use the HSB model for your colors (if your mc is fast enough) then cycling through all colors will start to feel very natural. C++ for this can be found here https://github.com/rvt/Arilux_AL-LC0X
i have this, and i want to implement this into my code using one of the 'case' statements i have. now, i have no clue what i'm doing so help would be appreciated. i want to add it into the lowest one (play/pause key)
=]ppm #include <IRremote.h>
// Define sensor pin
const int RECV_PIN = 4;
// Define LED pin constants
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;
// Define integer to remember toggle state
int togglestate = 0;
// Define IR Receiver and Results Objects
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup(){
// Enable the IR Receiver
irrecv.enableIRIn();
// Set LED pins as Outputs
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop(){
if (irrecv.decode(&results)){
switch(results.value){
case 0xFF30CF: //1 key
// Toggle LED On or Off
if(togglestate==0){
digitalWrite(redPin, HIGH);
togglestate=1;
}
else {
digitalWrite(redPin, LOW);
togglestate=0;
}
break;
case 0xFF10EF: //4 key
// Toggle LED On or Off
if(togglestate==0){
digitalWrite(bluePin, HIGH);
togglestate=1;
}
else {
digitalWrite(bluePin, LOW);
togglestate=0;
}
break;
case 0xFF18E7: //2 key
// Toggle LED On or Off
if(togglestate==0){
digitalWrite(greenPin, HIGH);
togglestate=1;
}
else {
digitalWrite(greenPin, LOW);
togglestate=0;
}
break;
case 0xFFC23D: //play-pause key
if togglestate==0){
}
}
irrecv.resume();
}
}
Otherwise the LED doesn't ever hit a pure red output.
??? WHY ???
for (int decColour = 0; decColour < 3; decColour += 1) {
// When `decColour ==2`
int incColour = decColour == 2 ? 0 : decColour + 1;
// `incColour = 0` at this moment
After finish the i loop
,
rgbColour[0]=255;
and rgbColour[1]=rgbColour[2]=0; ... back to pure red
again...
right ?
Another kinds of colour cycle
it might ignore darker shade problem
but it's total different spectrum with original
void loop() {
unsigned int rgbColour[3];
// Start off with red.
rgbColour[0] = 255;
rgbColour[1] = 255; //// Different place !
rgbColour[2] = 0;
// Choose the colours to increment and decrement.
for (int decColour = 0; decColour < 3; decColour += 1) {
int incColour = (0==decColour ) ? 2 : decColour - 1; //// Different place !
// cross-fade the two colours.
for(int i = 0; i < 255; i += 1) {
rgbColour[decColour] -= 1;
rgbColour[incColour] += 1;
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
delay(5);
}
}
}
for states :
255, 0, 0
255, 255, 0
0, 255, 0
0, 255, 255
0, 0, 255
255, 0, 255
255, 0, 0
void loop() {
unsigned int rgbColour[3];
// Start off with red.
rgbColour[0] = 255;
rgbColour[1] = 0;
rgbColour[2] = 0;
int decColour =0;
int incColour =1;
void loop() {
if (255<=rgbColour[incColour]) {
if (0>=rgbColour[decColour]) {
decColour++; decColour %= 3;
incColour++; incColour %= 3;
} else // if (0==rgbColour[decColour]) {
rgbColour[decColour] -= 15; // --
} else // if (255==rgbColour[incColour]) {
rgbColour[incColour] += 15; // ++
setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
delay(5);
}
}
Simultaneous increasing and decreasing of 2 colours leads to the situation where the colors "meet" at midpoint between 0 and 255 so the RGB status is like 127,128,0 which gives a darker shade. If you take a look at "color picker" in google, set the color to be the brightest and play with the slider you'll notice that it always changes 1 of the RGB variables at a time leading it to reach 6 main states that gradually switch between themselves, these states are:
Your implementation is providing the following states:
Here's a python implementation which shows how to loop through RGB without getting the darker shade:
https://github.com/michalmonday/RGB_rainbowLoop/blob/master/RGB_rainbowLoop.py
I guess it would be better without the section list but it does the thing... On the other side it allows for other operations being executed without the need to complete the whole cycle from red to blue, also it would be easy to implement custom sequences of colours with it.