#include <simpleBouton.h> // http://forum.arduino.cc/index.php?topic=375232.0
#include <Adafruit_NeoPixel.h>
const byte NUMPIXELS = 18;
Adafruit_NeoPixel pixelsClignoGauche = Adafruit_NeoPixel(NUMPIXELS, 5, NEO_RGBW + NEO_KHZ800);
Adafruit_NeoPixel pixelsClignoDroit = Adafruit_NeoPixel(NUMPIXELS, 6, NEO_RGBW + NEO_KHZ800);
uint32_t noir, vert;
const byte pin_ClignoGauche = 2;
const byte pin_ClignoDroit = 7;
simpleBouton boutonGauche(pin_ClignoGauche); //Cablage : pin---BP---GND (le bouton sera en INPUT_PULLUP)
simpleBouton boutonDroite(pin_ClignoDroit); //Cablage : pin---BP---GND (le bouton sera en INPUT_PULLUP)
enum : byte {FINIR_CLIGNOTE_DROITE, FINIR_CLIGNOTE_GAUCHE, REPOS} etat;
unsigned long chrono;
const unsigned long periodeClignotement = 30ul; // ici 1s pour qu'on voit ce qu'il se passe, 20ul pour 20ms
void afficherPixels()
{
pixelsClignoGauche.show();
pixelsClignoDroit.show();
}
void etapeSuivante(Adafruit_NeoPixel& bandeDePixel)
{
int unPixel;
for (unPixel = 0; unPixel < NUMPIXELS; unPixel++)
if (bandeDePixel.getPixelColor(unPixel) != noir) break; // on en a trouvé un allumé
if (unPixel == NUMPIXELS) bandeDePixel.setPixelColor(0, vert); // allumé s'ils sont tous éteint c'est qu'on commence
else {
// si le premier pixel est allumé et pas le dernier c'est qu'on "monte"
if ((bandeDePixel.getPixelColor(0) != noir) && (bandeDePixel.getPixelColor(NUMPIXELS - 1) == noir)) {
// on cherche le premier pas allumé et on l'allume
for (unPixel = 0; unPixel < NUMPIXELS; unPixel++) {
if (bandeDePixel.getPixelColor(unPixel) != vert) {
bandeDePixel.setPixelColor(unPixel, vert); // allumé
break;
}
}
} else {
// c'est qu'on descend
// on cherche le premier allumé et on l'éteint
for (unPixel = 0; unPixel < NUMPIXELS; unPixel++) {
if (bandeDePixel.getPixelColor(unPixel) != noir) {
bandeDePixel.setPixelColor(unPixel, noir); //éteint
break;
}
}
}
}
// on rend visible notre modif
afficherPixels();
}
void mettreAuRepos()
{
pixelsClignoGauche.clear();
pixelsClignoDroit.clear();
etat = REPOS;
afficherPixels();
}
void actualiserLesBoutons()
{
boutonGauche.actualiser();
boutonDroite.actualiser();
}
void setup() {
Serial.begin(115200);
pixelsClignoGauche.begin();
pixelsClignoDroit.begin();
noir = pixelsClignoGauche.Color(0, 0, 0);
vert = pixelsClignoGauche.Color(80, 255, 0);
mettreAuRepos();
}
void loop() {
// on teste les boutons
actualiserLesBoutons();
// on prend une décision
switch (etat) {
case REPOS:
if (boutonGauche.estEnfonce()) {
chrono = millis();
etapeSuivante(pixelsClignoGauche);
etat = FINIR_CLIGNOTE_GAUCHE;
}
else if (boutonDroite.estEnfonce()) {
chrono = millis();
etapeSuivante(pixelsClignoDroit);
etat = FINIR_CLIGNOTE_DROITE;
}
break;
case FINIR_CLIGNOTE_GAUCHE:
if (boutonGauche.vientDEtreRelache())
if (millis() - chrono >= periodeClignotement) {
if (etapeSuivante(pixelsClignoGauche)) mettreAuRepos;
else chrono += periodeClignotement;
}
else if (boutonDroite.vientDEtreEnfonce()) {
mettreAuRepos();
chrono = millis();
etapeSuivante(pixelsClignoDroit);
etat = FINIR_CLIGNOTE_DROITE;
}
break;
case FINIR_CLIGNOTE_DROITE:
if (boutonDroite.estRelache())
if (millis() - chrono >= periodeClignotement) {
if (etapeSuivante(pixelsClignoDroit))mettreAuRepos;
else chrono += periodeClignotement;
}
else if (boutonGauche.vientDEtreEnfonce()) {
mettreAuRepos();
chrono = millis();
etapeSuivante(pixelsClignoGauche);
etat = FINIR_CLIGNOTE_GAUCHE;
}
break;
}
}
Pour l'instant, quand je lache le bouton, les leds s'éteignent toutes en meme temps.
J'aimerais que quand je lache le bouton, les leds s'éteignent comme elles se sont allumées.(avec du dynamisme).
Dans le futur, j'aimerais ajouter au code, la fonction (frein), (marche arrière), (4 cligno warning) (dynamisme des cligno et des phares lors du verrouillage et deverouillage)... mais ce sera dans un futur lointain a mon avis.
j'ai fait la modif FINIR_CLIGNO. Je pense que c'est bon, mais pour la fonction void etatpeSuivante() je comprend pas comment faire.