FABLAB > potentiomètre et led RGB
On reprend le montage du potentiomètre mais nous allons utiliser une led RGB (séance de la semaine dernière).
Objectif : utiliser le potentiomètre pour changer la couleur de la LED
[bleu]
> 1er essai : passer du vert au violet[/bleu]
/// début du programme arduino
// INPUT: Potentiometer should be connected to 5V and GND
int potPin = 3; // Potentiometer output connected to analog pin 3 : A3
int potVal; // Variable to store the input from the potentiometer
// OUTPUT: Use digital pins 9-11, the Pulse-width Modulation (PWM) pins
// LED's cathodes should be connected to digital GND
int redPin = 9; // LED rouge, connected to digital pin 9
int grnPin = 10; // LED verte, connected to digital pin 10
int bluPin = 11; // LED bleue, connected to digital pin 11
// Program variables
int redVal; // Variables to store the values to send to the pins
int grnVal;
int bluVal;
void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
}
// Main program - du vert au violet
void loop()
{
{
potVal = analogRead(potPin); // lire la valeur du potentiomètre 1
redVal = map(potVal, 0, 1023, 0, 255); // la discrétiser pour l'utiliser avec une LED
grnVal = map(potVal, 0, 1023, 255, 0); // la discrétiser pour l'utiliser avec une LED
bluVal = map(potVal, 0, 1023, 0, 255); // la discrétiser pour l'utiliser avec une LED
}
analogWrite(redPin, redVal); // Write values to LED pins
analogWrite(grnPin, grnVal); // Write values to LED pins
analogWrite(bluPin, bluVal); // Write values to LED pins
}
//// fin du programme
[bleu] 2eme essai : vert rouge bleu vert[/bleu]
position du potentiomètre : de 0 à 1023 et 1023 / 3 = 341
soit de 0 à 341 : passer du vert au rouge
de 341 à 642 : passer du rouge au bleu
de 642 à 1023 : passer du bleu au vert
//// début du programme arduino
// INPUT: Potentiometer should be connected to 5V and GND
int potPin = 3; // Potentiometer output connected to analog pin 3 : A3
int potVal; // Variable to store the input from the potentiometer
// OUTPUT: Use digital pins 9-11, the Pulse-width Modulation (PWM) pins
// LED's cathodes should be connected to digital GND
int redPin = 9; // LED rouge, connected to digital pin 9
int grnPin = 10; // LED verte, connected to digital pin 10
int bluPin = 11; // LED bleue, connected to digital pin 11
// Program variables
int redVal; // Variables to store the values to send to the pins
int grnVal;
int bluVal;
void setup()
{
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
}
// Main program
//utilisant la fonction if
//vert - rouge - bleu - vert
void loop()
{
{
potVal = analogRead(potPin); // lire la valeur du potentiomètre 1
if (potVal <= 341)
{
redVal = map(potVal, 0, 341, 0, 255); // la discrétiser pour l'utiliser avec une LED
grnVal = 255 - redVal;
analogWrite(redPin, redVal); // Write values to LED pins
analogWrite(grnPin, grnVal); // Write values to LED pins
analogWrite(bluPin, 0); // Write values to LED pins
}
if (341 < potVal < 642)
{
bluVal = map(potVal, 342, 641, 0, 255); // la discrétiser pour l'utiliser avec une LED
redVal = 255 - bluVal;
analogWrite(redPin, redVal); // Write values to LED pins
analogWrite(grnPin, 0); // Write values to LED pins
analogWrite(bluPin, bluVal); // Write values to LED pins
}
if (potVal >= 642)
{
grnVal = map(potVal, 642, 1023, 0, 255); // la discrétiser pour l'utiliser avec une LED
bluVal = 255 - grnVal;
analogWrite(redPin, 0); // Write values to LED pins
analogWrite(grnPin, grnVal); // Write values to LED pins
analogWrite(bluPin, bluVal); // Write values to LED pins
}
}
delay (20);
}
//// fin du programme
Partager cette page