Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <Adafruit_NeoPixel.h>
- // neopixel settings
- #define NOMBRE_LEDS 30
- #define LEDS_PIN 5
- #define NOMBRE_LED_COLONE_RED 5
- #define NOMBRE_LED_COLONE_ORANGE 5
- #define NOMBRE_LED_COLONE_YELLOW 5
- #define NOMBRE_LED_COLONE_GREEN 5
- #define NOMBRE_LED_COLONE_BLUE 5
- #define NOMBRE_LED_COLONE_PURPLE 5
- #define NOMBRE_COLONNE_STANDARD 6
- //Bouton
- #define BTN_CHANGEMENT_MODE 2 //digital 2
- #define BTN_MARCHE_ARRET 3 //digital 3
- #define TEMPS_ANTI_REBOND 250 //en ms
- #define TIME_LECTURE_ANALOGIQUE 250 //en ms
- //potentiometre
- #define INTENSITE_PIN A0
- #define VITESSE_PIN A1
- #define TRIGGER_VITESSE 5 // 5 % de variation du potentiometre vitesse pour prendre en compte
- // ------------------------- neopixel
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(NOMBRE_LEDS, LEDS_PIN, NEO_GRB + NEO_KHZ800);
- int couleurSaturation = 50 ;
- struct colonneStandard{
- uint8_t nombreLed;
- int value_R;
- int value_G;
- int value_B;
- };
- colonneStandard colonneAffichageStandard[6]={
- {NOMBRE_LED_COLONE_RED,couleurSaturation, 0, 0},
- {NOMBRE_LED_COLONE_ORANGE,couleurSaturation, (couleurSaturation / 2 ), 0},
- {NOMBRE_LED_COLONE_YELLOW,couleurSaturation, couleurSaturation , 0},
- {NOMBRE_LED_COLONE_GREEN,0, couleurSaturation, 0},
- {NOMBRE_LED_COLONE_BLUE,0, 0, couleurSaturation},
- {NOMBRE_LED_COLONE_PURPLE, (couleurSaturation / 2) ,0,( couleurSaturation / 2)}
- };
- //variables
- volatile unsigned long timeChangementMode=0;
- uint8_t modeAffichage = 0;
- uint8_t affichageStandardDemarrer = 0;
- volatile unsigned long timeMarcheArret=0;
- uint8_t marcheArret = 0;
- unsigned long timeVitesseDefilement=0;
- unsigned long timeLectureAnalogique=0;
- unsigned int vitesseDefilement = 50; //50ms
- int cpteurChenillard = 0;
- int lectureIntensite=0;
- int lectureIntensitePrecedent=0;
- int lectureVitesseDefilement=0;
- int lectureVitesseDefilementPrecedent=0;
- void affichageStandard(){
- uint8_t leds = 0;
- strip.setBrightness(couleurSaturation);
- for(uint8_t i = 0 ; i < NOMBRE_COLONNE_STANDARD ; i++){
- for(uint8_t j = 0 ; j < colonneAffichageStandard[i].nombreLed ; j++){
- strip.setPixelColor(leds, strip.Color(colonneAffichageStandard[i].value_R, colonneAffichageStandard[i].value_G, colonneAffichageStandard[i].value_B) );
- leds++;
- }
- }
- strip.show();
- }
- void eteintLed(){
- for (int i=0 ; i <=NOMBRE_LEDS ; i++){
- strip.setPixelColor(i, strip.Color(0,0,0));
- }
- strip.show();
- }
- int colonneBandeau(int numLed){
- int result=0;
- int nbrLed = 0 ;
- for(int i = 0 ; i < NOMBRE_COLONNE_STANDARD ; i++){
- nbrLed += colonneAffichageStandard[i].nombreLed;
- if(numLed < nbrLed ){
- result = i;
- break;
- }
- }
- return result;
- }
- void chenillard(){
- if(couleurSaturation <= 60) couleurSaturation = 60;
- strip.setBrightness(couleurSaturation);
- int couleurR=0;
- int couleurG=0;
- int couleurB=0;
- int numColone=0;
- for(int i = 0 ; i < NOMBRE_LEDS ; i++){
- if(cpteurChenillard > 4 && cpteurChenillard == i ){
- numColone = colonneBandeau(i);
- couleurR = colonneAffichageStandard[numColone].value_R ;
- couleurG = colonneAffichageStandard[numColone].value_G ;
- couleurB = colonneAffichageStandard[numColone].value_B ;
- strip.setPixelColor(i, strip.Color(couleurR,couleurG,couleurB) );
- for(int k = 1 ; k<4 ; k++){
- numColone = colonneBandeau(i-k);
- couleurR = colonneAffichageStandard[numColone].value_R * ((100.0 - (k * 20 ))/100.0);
- couleurG = colonneAffichageStandard[numColone].value_G * ((100.0 - (k * 20 ))/100.0);
- couleurB = colonneAffichageStandard[numColone].value_B * ((100.0 - (k * 20 ))/100.0);
- strip.setPixelColor(i-k, strip.Color(couleurR,couleurG,couleurB) );
- }
- }
- else if(cpteurChenillard > 3 && cpteurChenillard == i ){
- numColone = colonneBandeau(i);
- couleurR = colonneAffichageStandard[numColone].value_R ;
- couleurG = colonneAffichageStandard[numColone].value_G ;
- couleurB = colonneAffichageStandard[numColone].value_B ;
- strip.setPixelColor(i, strip.Color(couleurR,couleurG,couleurB) );
- for(int k = 1 ; k<3 ; k++){
- numColone = colonneBandeau(i-k);
- couleurR = colonneAffichageStandard[numColone].value_R * ((100.0 - (k * 20 ))/100.0);
- couleurG = colonneAffichageStandard[numColone].value_G * ((100.0 - (k * 20 ))/100.0);
- couleurB = colonneAffichageStandard[numColone].value_B * ((100.0 - (k * 20 ))/100.0);
- strip.setPixelColor(i-k, strip.Color(couleurR,couleurG,couleurB) );
- }
- }
- else if(cpteurChenillard > 2 && cpteurChenillard == i ){
- numColone = colonneBandeau(i);
- couleurR = colonneAffichageStandard[numColone].value_R ;
- couleurG = colonneAffichageStandard[numColone].value_G ;
- couleurB = colonneAffichageStandard[numColone].value_B ;
- strip.setPixelColor(i, strip.Color(couleurR,couleurG,couleurB) );
- for(int k = 1 ; k<2 ; k++){
- numColone = colonneBandeau(i-k);
- couleurR = colonneAffichageStandard[numColone].value_R * ((100.0 - (k * 20 ))/100.0);
- couleurG = colonneAffichageStandard[numColone].value_G * ((100.0 - (k * 20 ))/100.0);
- couleurB = colonneAffichageStandard[numColone].value_B * ((100.0 - (k * 20 ))/100.0);
- strip.setPixelColor(i-k, strip.Color(couleurR,couleurG,couleurB) );
- }
- }
- else if(cpteurChenillard > 1 && cpteurChenillard == i ){
- numColone = colonneBandeau(i);
- couleurR = colonneAffichageStandard[numColone].value_R ;
- couleurG = colonneAffichageStandard[numColone].value_G ;
- couleurB = colonneAffichageStandard[numColone].value_B ;
- strip.setPixelColor(i, strip.Color(couleurR,couleurG,couleurB) );
- for(int k = 1 ; k<1 ; k++){
- numColone = colonneBandeau(i-k);
- couleurR = colonneAffichageStandard[numColone].value_R * ((100.0 - (k * 20 ))/100.0);
- couleurG = colonneAffichageStandard[numColone].value_G * ((100.0 - (k * 20 ))/100.0);
- couleurB = colonneAffichageStandard[numColone].value_B * ((100.0 - (k * 20 ))/100.0);
- strip.setPixelColor(i-k, strip.Color(couleurR,couleurG,couleurB) );
- }
- }
- else if(cpteurChenillard > 0 && cpteurChenillard == i ){
- numColone = colonneBandeau(i);
- couleurR = colonneAffichageStandard[numColone].value_R ;
- couleurG = colonneAffichageStandard[numColone].value_G ;
- couleurB = colonneAffichageStandard[numColone].value_B ;
- strip.setPixelColor(i, strip.Color(couleurR,couleurG,couleurB) );
- numColone = colonneBandeau(0);
- couleurR = colonneAffichageStandard[numColone].value_R * ((100.0 - (1 * 20 ))/100.0);
- couleurG = colonneAffichageStandard[numColone].value_G * ((100.0 - (1* 20 ))/100.0);
- couleurB = colonneAffichageStandard[numColone].value_B * ((100.0 - (1 * 20 ))/100.0);
- strip.setPixelColor(0, strip.Color(couleurR,couleurG,couleurB) );
- }
- else if(cpteurChenillard == 0 && cpteurChenillard == i ){
- numColone = colonneBandeau(i);
- couleurR = colonneAffichageStandard[numColone].value_R ;
- couleurG = colonneAffichageStandard[numColone].value_G ;
- couleurB = colonneAffichageStandard[numColone].value_B ;
- strip.setPixelColor(i, strip.Color(couleurR,couleurG,couleurB) );
- }
- else{
- numColone = colonneBandeau(i);
- couleurR = colonneAffichageStandard[numColone].value_R * ((100.0 - (4 * 20 ))/100.0);
- couleurG = colonneAffichageStandard[numColone].value_G * ((100.0 - (4 * 20 ))/100.0);
- couleurB = colonneAffichageStandard[numColone].value_B * ((100.0 - (4 * 20 ))/100.0);
- strip.setPixelColor(i, strip.Color(couleurR,couleurG,couleurB) );
- }
- }
- strip.show();
- }
- void toggleChangementMode(){
- if( abs((millis() - timeChangementMode)) >= TEMPS_ANTI_REBOND){
- timeChangementMode=millis();
- modeAffichage = !modeAffichage;
- }
- }
- void toggleMarcheArret(){
- if( abs((millis() - timeMarcheArret)) >= TEMPS_ANTI_REBOND){
- timeMarcheArret=millis();
- marcheArret = !marcheArret;
- }
- }
- void setup() {
- /*
- Serial.begin(9600);
- while (!Serial);
- Serial.println("demarrage...");
- Serial.flush();
- */
- pinMode(LEDS_PIN, OUTPUT);
- pinMode(BTN_CHANGEMENT_MODE, INPUT_PULLUP); //quand appuyer la lecture du btn vaux 0 ou low sinon il est a 1 - HIGH
- pinMode(BTN_MARCHE_ARRET, INPUT_PULLUP); //quand appuyer la lecture du btn vaux 0 ou low sinon il est a 1 - HIGH
- digitalWrite(LEDS_PIN, LOW);
- digitalWrite(BTN_CHANGEMENT_MODE, HIGH);
- digitalWrite(BTN_MARCHE_ARRET, HIGH);
- attachInterrupt(digitalPinToInterrupt(BTN_CHANGEMENT_MODE),toggleChangementMode,FALLING);
- attachInterrupt(digitalPinToInterrupt(BTN_MARCHE_ARRET),toggleMarcheArret,FALLING);
- strip.begin();
- strip.show();
- eteintLed();
- }
- void loop() {
- if(!marcheArret){
- if(!modeAffichage){
- if( abs((millis() - timeLectureAnalogique)) >= TIME_LECTURE_ANALOGIQUE) {
- timeLectureAnalogique = millis();
- lectureIntensite = analogRead(INTENSITE_PIN);
- float borneSuperieur = lectureIntensite * (float) ((100.0 + TRIGGER_VITESSE)/100.0);
- float borneInferieur = lectureIntensite * (float) ((100.0 - TRIGGER_VITESSE)/100.0);
- if( (borneSuperieur <= lectureIntensitePrecedent) || (borneInferieur >= lectureIntensitePrecedent)){
- lectureIntensitePrecedent = lectureIntensite;
- couleurSaturation = map(lectureIntensite,0,1023,0,254);
- }
- affichageStandard();
- }
- if(!affichageStandardDemarrer) affichageStandard();
- }
- else {
- if( abs((millis() - timeLectureAnalogique)) >= TIME_LECTURE_ANALOGIQUE) {
- timeLectureAnalogique = millis();
- lectureIntensite = analogRead(INTENSITE_PIN);
- float borneSuperieur = lectureIntensite * (float) ((100.0 + TRIGGER_VITESSE)/100.0);
- float borneInferieur = lectureIntensite * (float) ((100.0 - TRIGGER_VITESSE)/100.0);
- if( (borneSuperieur <= lectureIntensitePrecedent) || (borneInferieur >= lectureIntensitePrecedent)){
- lectureIntensitePrecedent = lectureIntensite;
- couleurSaturation = map(lectureIntensite,0,1023,0,254);
- }
- lectureVitesseDefilement = analogRead(VITESSE_PIN);
- float vitesseBorneSuperieur = lectureVitesseDefilement * (float) ((100.0 + TRIGGER_VITESSE)/100.0);
- float vitesseBorneInferieur = lectureVitesseDefilement * (float) ((100.0 - TRIGGER_VITESSE)/100.0);
- if( (vitesseBorneSuperieur <= lectureVitesseDefilementPrecedent) || (vitesseBorneInferieur >= lectureVitesseDefilementPrecedent)){
- lectureVitesseDefilementPrecedent = lectureVitesseDefilement;
- vitesseDefilement = map(lectureVitesseDefilement,0,1023,10,150);
- }
- }
- if(abs((millis() - timeVitesseDefilement)) >= vitesseDefilement){
- timeVitesseDefilement = millis();
- chenillard();
- (cpteurChenillard >= NOMBRE_LEDS) ? cpteurChenillard = 0 : cpteurChenillard++;
- }
- }
- }
- else {
- eteintLed();
- affichageStandardDemarrer=0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement