Advertisement
granteeric

Bandeau LGBTQ en neopixel v2.0

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