Skip to content

Instantly share code, notes, and snippets.

@Ndrinta
Last active December 18, 2015 13:09
Show Gist options
  • Save Ndrinta/5787601 to your computer and use it in GitHub Desktop.
Save Ndrinta/5787601 to your computer and use it in GitHub Desktop.
Tramite un potenziometro è possibile muoversi lungo tutta la gamma di colori. Verificare il funzionamento del tuo led RGB non sarà più un problema!
/*
cntr_RGB.pde
Sketch per il test di un LED tipo RGB ad anodo comune
Pin Utilizzati:
----------------------------------
Pin +5V -> +5V
Pin GND -> GND
Pin Digital 9 -> Led VERDE
Pin Digital 10 -> Led BLU
Pin Digital 11 -> Led ROSSO
Pin Analocico 0 -> Potenziometro
----------------------------------
Creato il 6/5/2013
by Pasquero Pietro <http://www.ndrinta.jimdo.com>
CC BY-NC-ND 3.0 IT
*/
//Definizione pin
#define LED_GREEN 9
#define LED_BLUE 10
#define LED_RED 11
#define POT 0
unsigned long interval = 1000;
unsigned long oldMillis = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
agg_RGB();
}
void setLed(int red_led, int green_led, int blue_led){
analogWrite(LED_RED, red_led);
analogWrite(LED_GREEN, green_led);
analogWrite(LED_BLUE, blue_led);
}
void agg_RGB(){
int red = 255;
int green = 0;
int blue = 0;
int offset;
int val = map(analogRead(POT), 0, 1023, 0, 1535);
if( val <= 255 ) //Rosso al max + Verde in crescita
green = val; // ottengo al max il Giallo
else if( val <= 511 ) //Verde al max + Rosso in diminuzione
{
red = abs(val - 511); //Rosso va da 255 a 0
green = 255; //Verde lo tengo al max
}
else if( val <= 767 ) //Verde al max + Blu in crescita
{ //(0, 255, 255) = Ciano
red = 0;
green = 255; //Verde al max
blue = val - 511; //Blu in crescita da 0 a 255
}
else if( val <= 1023 ) //Blu al max + Verde in diminuzione
{ //(0, 0, 255) = Blu
red = 0;
green = abs(val - 1023); //Verde in diminuzione da 255 a 0
blue = 255; //Blu fisso al max
}
else if( val <= 1279 ) //Blu al max + Rosso in crescita
{ //(255, 0, 255) = Magenta
red = val - 1023; //Rosso aumenta da 0 a 255
blue = 255; //Blu fisso al max
}
else { //Rosso al max + Blu in diminuzione
blue = abs(val - 1535);
}
if(millis() - oldMillis > interval)
{
oldMillis = millis();
Serial.print("Potenziometro: ");
Serial.print(val);
Serial.print(", Rosso: ");
Serial.print(red);
Serial.print(", Verde: ");
Serial.print(green);
Serial.print(", Blu: ");
Serial.println(blue);
}
setLed(red, green, blue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment