Advertisement
granteeric

Bandeau LGBTQ en neopixel

May 22nd, 2022
1,378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 11.44 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <Adafruit_NeoPixel.h>
  3.  
  4.  
  5.  
  6. // neopixel settings
  7. #define NOMBRE_LEDS  30
  8. #define LEDS_PIN  5
  9. #define NOMBRE_LED_COLONE_RED           5
  10. #define NOMBRE_LED_COLONE_ORANGE        5
  11. #define NOMBRE_LED_COLONE_YELLOW        5
  12. #define NOMBRE_LED_COLONE_GREEN         5
  13. #define NOMBRE_LED_COLONE_BLUE          5
  14. #define NOMBRE_LED_COLONE_PURPLE        5
  15. #define NOMBRE_COLONNE_STANDARD         6
  16.  
  17.  
  18. //Bouton
  19. #define BTN_CHANGEMENT_MODE             2         //digital 2
  20. #define BTN_MARCHE_ARRET                3         //digital 3
  21. #define TEMPS_ANTI_REBOND               250       //en ms
  22. #define TIME_LECTURE_ANALOGIQUE         250       //en ms
  23.  
  24.  
  25. //potentiometre
  26. #define INTENSITE_PIN                   A0        
  27. #define VITESSE_PIN                     A1
  28. #define TRIGGER_VITESSE                 5        // 5 % de variation du potentiometre vitesse pour prendre en compte
  29.  
  30.  
  31.  
  32. // -------------------------    neopixel
  33.  
  34. Adafruit_NeoPixel strip = Adafruit_NeoPixel(NOMBRE_LEDS, LEDS_PIN, NEO_GRB + NEO_KHZ800);
  35.  
  36. int couleurSaturation = 50 ;
  37.  
  38. struct colonneStandard{
  39.   uint8_t nombreLed;
  40.   int value_R;
  41.   int value_G;
  42.   int value_B;
  43. };
  44.  
  45. colonneStandard colonneAffichageStandard[6]={
  46.   {NOMBRE_LED_COLONE_RED,couleurSaturation, 0, 0},
  47.   {NOMBRE_LED_COLONE_ORANGE,couleurSaturation, (couleurSaturation / 2 ), 0},
  48.   {NOMBRE_LED_COLONE_YELLOW,couleurSaturation, couleurSaturation , 0},
  49.   {NOMBRE_LED_COLONE_GREEN,0, couleurSaturation, 0},
  50.   {NOMBRE_LED_COLONE_BLUE,0, 0, couleurSaturation},
  51.   {NOMBRE_LED_COLONE_PURPLE, (couleurSaturation / 2) ,0,( couleurSaturation / 2)}
  52. };
  53.  
  54.  
  55.  
  56. //variables
  57. volatile unsigned long timeChangementMode=0;    
  58. uint8_t modeAffichage = 0;
  59. uint8_t affichageStandardDemarrer = 0;
  60. volatile unsigned long timeMarcheArret=0;    
  61. uint8_t marcheArret = 0;
  62. unsigned long timeVitesseDefilement=0;    
  63. unsigned long timeLectureAnalogique=0;    
  64. unsigned int vitesseDefilement = 50;               //50ms
  65. int cpteurChenillard = 0;
  66. int lectureIntensite=0;
  67. int lectureIntensitePrecedent=0;
  68. int lectureVitesseDefilement=0;
  69. int lectureVitesseDefilementPrecedent=0;
  70.  
  71.  
  72.  
  73. void affichageStandard(){
  74.   uint8_t leds = 0;
  75.   strip.setBrightness(couleurSaturation);
  76.  
  77.     for(uint8_t i = 0 ; i < NOMBRE_COLONNE_STANDARD ; i++){
  78.     for(uint8_t j = 0 ; j < colonneAffichageStandard[i].nombreLed ; j++){
  79.         strip.setPixelColor(leds, strip.Color(colonneAffichageStandard[i].value_R, colonneAffichageStandard[i].value_G, colonneAffichageStandard[i].value_B) );
  80.       leds++;
  81.     }
  82.   }
  83.   strip.show();
  84. }
  85.  
  86. void eteintLed(){
  87.   for (int i=0 ; i <=NOMBRE_LEDS ; i++){
  88.           strip.setPixelColor(i, strip.Color(0,0,0));
  89.   }
  90.   strip.show();
  91. }
  92.  
  93.  
  94. int colonneBandeau(int numLed){
  95.   int result=0;
  96.   int nbrLed = 0 ;
  97.  
  98.   for(int i = 0 ; i < NOMBRE_COLONNE_STANDARD ; i++){
  99.     nbrLed += colonneAffichageStandard[i].nombreLed;
  100.     if(numLed < nbrLed ){
  101.       result = i;
  102.       break;
  103.     }
  104.   }
  105.     return result;
  106. }
  107.  
  108. void chenillard(){
  109. if(couleurSaturation <= 60) couleurSaturation = 60;  
  110. strip.setBrightness(couleurSaturation);
  111. int couleurR=0;
  112. int couleurG=0;
  113. int couleurB=0;
  114. int numColone=0;
  115.  
  116.     for(int i = 0 ; i < NOMBRE_LEDS ; i++){
  117.  
  118.       if(cpteurChenillard > 4 && cpteurChenillard == i  ){
  119.         numColone = colonneBandeau(i);
  120.         couleurR = colonneAffichageStandard[numColone].value_R ;
  121.         couleurG = colonneAffichageStandard[numColone].value_G ;
  122.         couleurB = colonneAffichageStandard[numColone].value_B ;
  123.         strip.setPixelColor(i, strip.Color(couleurR,couleurG,couleurB) );
  124.         for(int k = 1 ; k<4 ; k++){
  125.           numColone = colonneBandeau(i-k);
  126.           couleurR = colonneAffichageStandard[numColone].value_R * ((100.0 - (k * 20 ))/100.0);
  127.           couleurG = colonneAffichageStandard[numColone].value_G * ((100.0 - (k * 20 ))/100.0);
  128.           couleurB = colonneAffichageStandard[numColone].value_B * ((100.0 - (k * 20 ))/100.0);
  129.           strip.setPixelColor(i-k, strip.Color(couleurR,couleurG,couleurB) );
  130.         }
  131.       }
  132.       else if(cpteurChenillard > 3 && cpteurChenillard == i  ){
  133.         numColone = colonneBandeau(i);
  134.         couleurR = colonneAffichageStandard[numColone].value_R ;
  135.         couleurG = colonneAffichageStandard[numColone].value_G ;
  136.         couleurB = colonneAffichageStandard[numColone].value_B ;
  137.         strip.setPixelColor(i, strip.Color(couleurR,couleurG,couleurB) );
  138.         for(int k = 1 ; k<3 ; k++){
  139.           numColone = colonneBandeau(i-k);
  140.           couleurR = colonneAffichageStandard[numColone].value_R * ((100.0 - (k * 20 ))/100.0);
  141.           couleurG = colonneAffichageStandard[numColone].value_G * ((100.0 - (k * 20 ))/100.0);
  142.           couleurB = colonneAffichageStandard[numColone].value_B * ((100.0 - (k * 20 ))/100.0);
  143.           strip.setPixelColor(i-k, strip.Color(couleurR,couleurG,couleurB) );
  144.         }
  145.       }
  146.       else if(cpteurChenillard > 2 && cpteurChenillard == i  ){
  147.         numColone = colonneBandeau(i);
  148.         couleurR = colonneAffichageStandard[numColone].value_R ;
  149.         couleurG = colonneAffichageStandard[numColone].value_G ;
  150.         couleurB = colonneAffichageStandard[numColone].value_B ;
  151.         strip.setPixelColor(i, strip.Color(couleurR,couleurG,couleurB) );
  152.         for(int k = 1 ; k<2 ; k++){
  153.           numColone = colonneBandeau(i-k);
  154.           couleurR = colonneAffichageStandard[numColone].value_R * ((100.0 - (k * 20 ))/100.0);
  155.           couleurG = colonneAffichageStandard[numColone].value_G * ((100.0 - (k * 20 ))/100.0);
  156.           couleurB = colonneAffichageStandard[numColone].value_B * ((100.0 - (k * 20 ))/100.0);
  157.           strip.setPixelColor(i-k, strip.Color(couleurR,couleurG,couleurB) );
  158.         }
  159.       }
  160.  
  161.       else if(cpteurChenillard > 1 && cpteurChenillard == i  ){
  162.         numColone = colonneBandeau(i);
  163.         couleurR = colonneAffichageStandard[numColone].value_R ;
  164.         couleurG = colonneAffichageStandard[numColone].value_G ;
  165.         couleurB = colonneAffichageStandard[numColone].value_B ;
  166.         strip.setPixelColor(i, strip.Color(couleurR,couleurG,couleurB) );
  167.         for(int k = 1 ; k<1 ; k++){
  168.           numColone = colonneBandeau(i-k);
  169.           couleurR = colonneAffichageStandard[numColone].value_R * ((100.0 - (k * 20 ))/100.0);
  170.           couleurG = colonneAffichageStandard[numColone].value_G * ((100.0 - (k * 20 ))/100.0);
  171.           couleurB = colonneAffichageStandard[numColone].value_B * ((100.0 - (k * 20 ))/100.0);
  172.           strip.setPixelColor(i-k, strip.Color(couleurR,couleurG,couleurB) );
  173.         }
  174.       }
  175.       else if(cpteurChenillard > 0 && cpteurChenillard == i  ){
  176.         numColone = colonneBandeau(i);
  177.         couleurR = colonneAffichageStandard[numColone].value_R ;
  178.         couleurG = colonneAffichageStandard[numColone].value_G ;
  179.         couleurB = colonneAffichageStandard[numColone].value_B ;
  180.         strip.setPixelColor(i, strip.Color(couleurR,couleurG,couleurB) );
  181.         numColone = colonneBandeau(0);
  182.         couleurR = colonneAffichageStandard[numColone].value_R * ((100.0 - (1 * 20 ))/100.0);
  183.         couleurG = colonneAffichageStandard[numColone].value_G * ((100.0 - (1* 20 ))/100.0);
  184.         couleurB = colonneAffichageStandard[numColone].value_B * ((100.0 - (1 * 20 ))/100.0);
  185.         strip.setPixelColor(0, strip.Color(couleurR,couleurG,couleurB) );
  186.       }
  187.       else if(cpteurChenillard == 0 && cpteurChenillard == i  ){
  188.         numColone = colonneBandeau(i);
  189.         couleurR = colonneAffichageStandard[numColone].value_R ;
  190.         couleurG = colonneAffichageStandard[numColone].value_G ;
  191.         couleurB = colonneAffichageStandard[numColone].value_B ;
  192.         strip.setPixelColor(i, strip.Color(couleurR,couleurG,couleurB) );
  193.       }
  194.       else{
  195.  
  196.         numColone = colonneBandeau(i);
  197.         couleurR = colonneAffichageStandard[numColone].value_R * ((100.0 - (4 * 20 ))/100.0);
  198.         couleurG = colonneAffichageStandard[numColone].value_G * ((100.0 - (4 * 20 ))/100.0);
  199.         couleurB = colonneAffichageStandard[numColone].value_B * ((100.0 - (4 * 20 ))/100.0);
  200.         strip.setPixelColor(i, strip.Color(couleurR,couleurG,couleurB) );
  201.       }
  202.     }
  203.   strip.show();
  204. }
  205.  
  206. void toggleChangementMode(){
  207.   if( abs((millis() - timeChangementMode)) >= TEMPS_ANTI_REBOND){
  208.     timeChangementMode=millis();
  209.     modeAffichage = !modeAffichage;
  210.   }
  211. }
  212.  
  213. void toggleMarcheArret(){
  214.   if( abs((millis() - timeMarcheArret)) >= TEMPS_ANTI_REBOND){
  215.     timeMarcheArret=millis();
  216.     marcheArret = !marcheArret;
  217.   }
  218. }
  219.  
  220.  
  221. void setup() {
  222. /*  
  223.   Serial.begin(9600);
  224.   while (!Serial);
  225.   Serial.println("demarrage...");
  226.   Serial.flush();
  227.   */
  228.  
  229.   pinMode(LEDS_PIN, OUTPUT);
  230.   pinMode(BTN_CHANGEMENT_MODE, INPUT_PULLUP);   //quand appuyer la lecture du btn vaux 0 ou low sinon il est a 1 - HIGH
  231.   pinMode(BTN_MARCHE_ARRET, INPUT_PULLUP);   //quand appuyer la lecture du btn vaux 0 ou low sinon il est a 1 - HIGH
  232.  
  233.   digitalWrite(LEDS_PIN, LOW);
  234.   digitalWrite(BTN_CHANGEMENT_MODE, HIGH);
  235.   digitalWrite(BTN_MARCHE_ARRET, HIGH);
  236.   attachInterrupt(digitalPinToInterrupt(BTN_CHANGEMENT_MODE),toggleChangementMode,FALLING);
  237.   attachInterrupt(digitalPinToInterrupt(BTN_MARCHE_ARRET),toggleMarcheArret,FALLING);
  238.  
  239.   strip.begin();
  240.   strip.show();
  241.   eteintLed();
  242.  
  243. }
  244.  
  245. void loop() {
  246.   if(!marcheArret){
  247.     if(!modeAffichage){
  248.       if( abs((millis() - timeLectureAnalogique)) >= TIME_LECTURE_ANALOGIQUE) {
  249.         timeLectureAnalogique = millis();
  250.         lectureIntensite = analogRead(INTENSITE_PIN);
  251.         float borneSuperieur =  lectureIntensite * (float) ((100.0 + TRIGGER_VITESSE)/100.0);
  252.         float borneInferieur =  lectureIntensite * (float) ((100.0 - TRIGGER_VITESSE)/100.0);
  253.         if( (borneSuperieur <= lectureIntensitePrecedent) || (borneInferieur >= lectureIntensitePrecedent)){
  254.           lectureIntensitePrecedent = lectureIntensite;
  255.           couleurSaturation = map(lectureIntensite,0,1023,0,254);
  256.         }
  257.         affichageStandard();
  258.         }
  259.       if(!affichageStandardDemarrer) affichageStandard();
  260.     }
  261.     else {
  262.       if( abs((millis() - timeLectureAnalogique)) >= TIME_LECTURE_ANALOGIQUE) {
  263.         timeLectureAnalogique = millis();
  264.         lectureIntensite = analogRead(INTENSITE_PIN);
  265.         float borneSuperieur =  lectureIntensite * (float) ((100.0 + TRIGGER_VITESSE)/100.0);
  266.         float borneInferieur =  lectureIntensite * (float) ((100.0 - TRIGGER_VITESSE)/100.0);
  267.         if( (borneSuperieur <= lectureIntensitePrecedent) || (borneInferieur >= lectureIntensitePrecedent)){
  268.  
  269.           lectureIntensitePrecedent = lectureIntensite;
  270.           couleurSaturation = map(lectureIntensite,0,1023,0,254);
  271.         }
  272.        
  273.         lectureVitesseDefilement = analogRead(VITESSE_PIN);
  274.         float vitesseBorneSuperieur =  lectureVitesseDefilement * (float) ((100.0 + TRIGGER_VITESSE)/100.0);
  275.         float vitesseBorneInferieur =  lectureVitesseDefilement * (float) ((100.0 - TRIGGER_VITESSE)/100.0);
  276.         if( (vitesseBorneSuperieur <= lectureVitesseDefilementPrecedent) || (vitesseBorneInferieur >= lectureVitesseDefilementPrecedent)){
  277.  
  278.           lectureVitesseDefilementPrecedent = lectureVitesseDefilement;
  279.           vitesseDefilement = map(lectureVitesseDefilement,0,1023,10,150);
  280.         }
  281.       }
  282.  
  283.       if(abs((millis() - timeVitesseDefilement)) >= vitesseDefilement){
  284.         timeVitesseDefilement = millis();
  285.         chenillard();
  286.         (cpteurChenillard >= NOMBRE_LEDS) ? cpteurChenillard = 0 : cpteurChenillard++;
  287.       }
  288.     }
  289.   }
  290.   else {
  291.     eteintLed();
  292.     affichageStandardDemarrer=0;
  293.   }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement