commutateur-leds-arduino

Commutateur 3 positions + leds + arduino

Damien Monteillard | Creative Commons BY-NC-SA

Idée du code arduino

Grâce au commutateur et à ses positions, afficher la led (-1, 0, 1) correspondante et pouvoir controler sa couleur.

Elements

  • Commutateur à cames trois positions
  • (Pos1, Pos2, Pos3) connecté en analogique (A0, A1, A2) sur l'arduino

  • 3 Leds adressables
  • Elles sont alimentées 5V en parallèle.

    3 Pistes DataIn branchées respectivement sur une entrée Digitale (2, 5, 6).

    Schéma électrique commutateur

    Situation mécanique

    Arduino

    Utiliser la librairie FastLed

    Code arduino

    
    //////////////////////////
    ////// Leds facade ///////
    //////////////////////////
    
    #include 
    
    #define DATA_PIN2   2
    #define DATA_PIN3   5
    #define DATA_PIN4   6
    
    #define NUM_LEDS    3
    #define BRIGHTNESS  64
    #define LED_TYPE    WS2812B
    #define COLOR_ORDER GRB
    
    // Commutateur à came 3 positions
    #define POSITION0   A0
    #define POSITION1   A1
    #define POSITION2   A2
    int etat0;
    int etat1;
    int etat2;
    
    #define UPDATES_PER_SECOND 100
    
    // Define the array of leds
    CRGB leds[NUM_LEDS];
    
    void setup() {
      Serial.begin(9600);
    
      FastLED.addLeds(leds, NUM_LEDS);
      FastLED.setBrightness(BRIGHTNESS);
      pinMode(A0, INPUT);
    
      FastLED.addLeds(leds, NUM_LEDS);
      FastLED.setBrightness(BRIGHTNESS);
      pinMode(A1, INPUT);
    
      FastLED.addLeds(leds, NUM_LEDS);
      FastLED.setBrightness(BRIGHTNESS);
      pinMode(A2, INPUT);
    }
    
    // le commutateur est en position relachée pour chaque position et on définie une couleur
    void actionRelache() {
      if (POSITION0 < 50) {
        leds[0] = CRGB(255, 255, 0);
      }
      if (POSITION1 < 50) {
        leds[0] = CRGB(255, 255, 0);
      }
      if (POSITION2 < 50) {
        leds[0] = CRGB(0, 255, 0);
      }  
      FastLED.show();
      // delay(10);
    }
    
    // le commutateur est en position connecté pour chaque position
    void actionAppuye() {
      if (POSITION0 > 50) {
        leds[0] = CRGB( 255, 130, 0);
      }
      if (POSITION1 > 50) {
        leds[0] = CRGB( 255, 255, 255);
      }
      if (POSITION2 > 50) {
        leds[0] = CRGB( 45, 193, 255);
      }
      FastLED.show();
    }
    
    void loop() {
      etat0 = analogRead(POSITION0);
      etat1 = analogRead(POSITION1);
      etat2 = analogRead(POSITION2);
    
      switch (etat0) {
        case 0:
          actionRelache();
          break;
        case 1:
          actionAppuye();
          break;
        }
    
      switch (etat1) {
        case 0:
          actionRelache();
          break;
        case 1:
          actionAppuye();
          break;
        }
    
      switch (etat2) {
        case 0:
          actionRelache();
          break;
        case 1:
          actionAppuye();
          break;
        }
    
      Serial.print(etat0);
      Serial.print("\n");
      Serial.print(etat1);
      Serial.print("\n");
      // Serial.print(etat2);
      
      // Serial.print(A0);
      // Serial.print(DATA_PIN2);
      // Serial.print("\n");
    
      delay(500);
    }