J'ai trouvé cette idée de contrôler des leds WS2812b sur la vidéo de présentation d'un des derniers racers : le Vortex.
Dans la vidéo, on voit une personne, qui, en cliquant sur un bouton, fait changer le mode de clignotement des leds.
J'avais envie de comprendre comment cela fonctionnait, et notamment le contrôle de plusieurs leds par seulement 1 petit fils de signal et une alim en 5V... trop top.
Le code suivant propose une solution pour le contrôle de ces leds. Dans mon montage, j'ai utilisé une barre de 8 leds acheter chez Adafruit.
Le principe : à chaque "clic" sur le bouton poussoir, on déclenche un mode différent. On peut tout de suite imaginer plein d'application et d'évolution, notamment remplacer le bouton poussoir par un interrupteur.
Fonction utiliser dans le programme : Je profite de ce post pour présenter de manière rapide une fonction qui fonctionne très bien lorsque l'on souhaite coder différent mode. la fonction "switch" + "case"
Les utilisations sont diverses :
- code pour un afficheur lcd ( ex : un menu ...)
- différent mode pour une télécommande ( ex : si j'appuie sur "1" alors .... si j'appuie sur "2" alors ...)
- dans le cas présent : plusieurs mode en fonction d'un compteur = variable qui compte le nombre de fois que l'on clique sur le bouton
- et surement plein d'autres possibilités ...
Câblage en pièce jointe.
Ici le code :
ATTENTION : PENSEZ A INSTALLER LA LIBRAIRIE NEOPIXEL : https://github.com/adafruit/Adafruit_NeoPixel" onclick="window.open(this.href);return false;
Code : Tout sélectionner
#include <Adafruit_NeoPixel.h>
#include <avr/power.h>
#define PIN 6 // Which pin on the Arduino is connected to the NeoPixels ; On a Trinket or Gemma we suggest changing this to 1
#define NUMPIXELS 8 // How many NeoPixels are attached to the Arduino?
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
// PushButton Stat
boolean PushButtonStat = false;
// PushbuttonPin
const int PushbuttonPin = 7;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int NumberOfMode = 9; // 7 + 2 mode OFF
long Mode1previousMillis = 0;
long Mode2previousMillis = 0;
long Mode3previousMillis = 0;
long PushButtonpreviousMillis = 0;
unsigned long currentMillis = 0;
long Mode1Interval = 50; // Timer (Delay) Mode 1
long Mode2Interval = 500; // Timer (Delay) Mode 2
long Mode3Interval = 50; // Timer (Delay) Mode 3
long Pushinterval = 750;
long blinkMillis = 300; // Blinker delay
// Color Set Mode 1
int R = 250;
int G = 250;
int B = 250;
// Color Set Mode 2
int R_2 = 0;
int G_2 = 0;
int B_2 = 50;
// Color Set Mode 3
int R_3 = 0;
int G_3 = 50;
int B_3 = 0;
// Color Set Mode 4
int R_4 = 250;
int G_4 = 0;
int B_4 = 250;
// Color Set Mode 5
int R_5 = 0;
int G_5 = 20;
int B_5 = 25;
// Color Set Mode 6
int R_6 = 25;
int G_6 = 20;
int B_6 = 0;
// Color Set Mode 7
int R_7 = 0;
int G_7 = 0;
int B_7 = 30;
// Color Set Mode 8
int R_8 = 0;
int G_8 = 0;
int B_8 = 30;
// **** Test ******************
long FirstcurrentMillis = 0;
long FirstpreviousMillis = 0;
long SeccurrentMillis = 0;
long SecpreviousMillis = 0;
long ThirdcurrentMillis = 0;
long ThirdpreviousMillis = 0;
int testvalue = 0;
int ledState = LOW;
int LedPos = 0;
int LedPos2 = 0;
boolean LedOff = false;
// ************Test***********
// Program Start //
void setup() {
pinMode(PushbuttonPin, INPUT_PULLUP); // initialize the button pin as a input:
Serial.begin(9600); // initialize serial communication:
pixels.begin(); // This initializes the NeoPixel library.
}
void loop() {
currentMillis = millis();
// Push Button Counter
if(currentMillis - PushButtonpreviousMillis > Pushinterval)
{
buttonState = digitalRead(PushbuttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState)
{
// if the state has changed, increment the counter
if (buttonState == HIGH)
{
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else
{
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
if (buttonPushCounter == NumberOfMode)
{
buttonPushCounter = 0; // reset buttonPushCounter
}
} // Push Button Counter
//***************** Start Modes **********************//
switch (buttonPushCounter)
{
case 0:
for (int i = 0; i<NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(0,0,0)); // All Leds OFF.
pixels.show(); // This sends the updated pixel color to the hardware.
}
break;
case 1:
for (int i = 0; i<NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(R,G,B));
pixels.show(); // This sends the updated pixel color to the hardware.
}
break;
case 2:
for (int i = 0; i<NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(R_2,G_2,B_2));
pixels.show(); // This sends the updated pixel color to the hardware.
}
break;
case 3:
for (int i = 0; i<NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(R_3,G_3,B_3));
pixels.show(); // This sends the updated pixel color to the hardware.
}
break;
case 4:
for (int i = 0; i<NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(R_4,G_4,B_4));
pixels.show(); // This sends the updated pixel color to the hardware.
}
break;
case 5 : // Blink Mode
FirstcurrentMillis = millis();
if(FirstcurrentMillis - FirstpreviousMillis > blinkMillis)
{
FirstpreviousMillis = FirstcurrentMillis;
if (ledState == LOW)
{
for (int i = 0; i<NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(R_5,G_5,B_5));
pixels.show(); // This sends the updated pixel color to the hardware.
ledState = HIGH;
}
}
else
{
for (int i = 0; i<NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(0,0,0));
pixels.show(); // This sends the updated pixel color to the hardware.
ledState = LOW;
}
}
}
break;
case 6 : //
SeccurrentMillis = millis();
if(SeccurrentMillis - SecpreviousMillis > 40)
{
SecpreviousMillis = SeccurrentMillis;
pixels.setPixelColor(LedPos, pixels.Color(R_6,G_6,B_6));
pixels.show(); // This sends the updated pixel color to the hardware.
pixels.setPixelColor(LedPos - 1 , pixels.Color(0,0,0)); // turn off previous led
pixels.show(); // This sends the updated pixel color to the hardware.
LedPos = LedPos + 1 ;
}
if (LedPos > NUMPIXELS)
{
LedPos = 0;
}
break;
case 7 :
for (int i = 0; i<NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(0,0,0)); // All Leds OFF.
pixels.show(); // This sends the updated pixel color to the hardware.
}
LedPos2 = 0;
break;
case 8 :
ThirdcurrentMillis = millis();
if(ThirdcurrentMillis - ThirdpreviousMillis > 350)
{
ThirdpreviousMillis = ThirdcurrentMillis;
pixels.setPixelColor(0, pixels.Color(R_8,G_8,B_8)); // start from the first led
pixels.show(); // This sends the updated pixel color to the hardware.
pixels.setPixelColor(0 + LedPos2, pixels.Color(R_8,G_8,B_8)); // turn on each led one by one
pixels.show(); // This sends the updated pixel color to the hardware.
LedPos2 = LedPos2 + 1 ;
Serial.println("LedPos2");
Serial.println(LedPos2);
//LedOff = false;
}
if (LedPos2 >= NUMPIXELS)
{
LedPos2 = 0;
}
break;
}
} // end of loop
PS : Video coming soon !