czwartek, 8 grudnia 2016

[29] Arduino - Enkoder. WS2812B

Ten post chciałbym poświęcić na przygotowanie drugiego programu, który w nieco szerszym zakresie zaprezentuje możliwości diod programowalnych WS2812B.

Podłączenie:


W przykładzie wykorzystam enkoder bez przycisku oraz diody WS2812B. Drugi przykład natomiast będzie obsługiwał enkoder z przyciskiem.


Jeśli chodzi o podłączenie to wygląda ono następująco:

Enkoder:


  • Pin 2;
  • Pin 3;
  • Wspólny (przeważnie środkowy) do GND;
  • Przycisk od Enkodera Pin 4; 


Diody:

  • GND - NC
  • DIN - 6 Pin Arduino Uno
  • 4-7VDC - 5V
  • GND - GND


Program 1:


W tym programie podłączony będzie jeden enkoder pod piny umożliwiające przerwanie czyli pin 2 oraz 3. Zmiana enkodera będzie powodowała zmiany wszystkich składowych koloru od 0 do 255. Większą ilość enkoderów pod przerwania można by było podłączyć dla układu Mega, Mega2560 lub MegaADK gdzie dostępne jest 6 pinów pod przerwania. Program obsługujący jeden enkoder wygląda następująco:

  1. #include <Adafruit_NeoPixel.h>
  2.  
  3. #define LICZBA_DIOD 8
  4. #define PIN_GPIO_DIODE_DATA 6
  5. #define ENC_PIN_1 2
  6. #define ENC_PIN_2 3
  7. #define ENC_PIN_BUTTON 8
  8.  
  9. volatile int Last_Encoded = 0;
  10.  
  11. volatile int encoderValue_GREEN = 0;
  12. volatile int encoderValue_RED = 0;
  13. volatile int encoderValue_BLUE = 0;
  14.  
  15. int lastMSB = 0;
  16. int lastLSB = 0;
  17.  
  18. Adafruit_NeoPixel strip = Adafruit_NeoPixel(LICZBA_DIOD, PIN_GPIO_DIODE_DATA, NEO_GRB + NEO_KHZ800);
  19.  
  20. void setup() {
  21.   Serial.begin (9600);
  22.   while (!Serial)
  23.  
  24.   Serial.println();
  25.   Serial.println("Inicjalizacja...");
  26.   Serial.flush();
  27.  
  28.   strip.begin();
  29.   strip.show();
  30.  
  31.   Serial.println();
  32.   Serial.println("OK");
  33.  
  34.   pinMode(ENC_PIN_1, INPUT);
  35.   pinMode(ENC_PIN_2, INPUT);
  36.  
  37.   //Pin1 i Pin2 na HIGH, z rezystor podciągającym
  38.   digitalWrite(ENC_PIN_1, HIGH);
  39.   digitalWrite(ENC_PIN_2, HIGH);
  40.  
  41.   //Dolaczenie przerwanie,
  42.   attachInterrupt(0, Encoder_Check, CHANGE);
  43.   attachInterrupt(1, Encoder_Check, CHANGE);
  44. }
  45.  
  46. void loop()
  47. {
  48.   Serial.print("R: ");
  49.   Serial.print(encoderValue_RED);
  50.   Serial.print(" G: ");
  51.   Serial.print(encoderValue_GREEN);
  52.   Serial.print(" B: ");
  53.   Serial.println(encoderValue_BLUE);
  54.   delay(1000);
  55. }
  56.  
  57. void Encoder_Check(){
  58.   int MSB = digitalRead(ENC_PIN_1);
  59.   int LSB = digitalRead(ENC_PIN_2);
  60.  
  61.   int encoded = (MSB << 1) |LSB; //konwersja wartosci do jednej wartosci
  62.   int sum  = (Last_Encoded << 2) | encoded; //dodawanie wartosci do wczesniej zdekodowanej wartosci
  63.  
  64.   if(sum == 0x0D || sum == 0x04 || sum == 0x02 || sum == 0x0B)
  65.   {
  66.     if(encoderValue_RED >= 255)
  67.     {
  68.       encoderValue_RED = 0;
  69.     }
  70.     else
  71.     {
  72.       encoderValue_RED++;
  73.       encoderValue_GREEN++;
  74.       encoderValue_BLUE++;
  75.       for(int i=0; i<LICZBA_DIOD; i++)
  76.       {
  77.         strip.setPixelColor(i, encoderValue_RED, encoderValue_GREEN, encoderValue_BLUE);
  78.         strip.show();
  79.       }
  80.     }
  81.   }
  82.   if(sum == 0x0E || sum == 0x07 || sum == 0x01 || sum == 0x08)
  83.   {
  84.     if(encoderValue_RED <= 0)
  85.     {
  86.       encoderValue_RED = 0;
  87.     }
  88.     else
  89.     {
  90.        encoderValue_RED--;
  91.        encoderValue_GREEN--;
  92.        encoderValue_BLUE--;
  93.        for(int i=0; i<LICZBA_DIOD; i++)
  94.        {
  95.           strip.setPixelColor(i, encoderValue_RED, encoderValue_GREEN, encoderValue_BLUE);
  96.           strip.show();
  97.        }
  98.     }
  99.   }
  100.  
  101.   Last_Encoded = encoded;
  102. }

Program 2:


  1. #include <Adafruit_NeoPixel.h>
  2.  
  3. #define LICZBA_DIOD 8
  4. #define PIN_GPIO_DIODE_DATA 6
  5. #define ENC_PIN_1 2
  6. #define ENC_PIN_2 3
  7. #define ENC_PIN_BUTTON 8
  8.  
  9. volatile int Last_Encoded = 0;
  10.  
  11. volatile int encoderValue_GREEN = 0;
  12. volatile int encoderValue_RED = 0;
  13. volatile int encoderValue_BLUE = 0;
  14.  
  15. volatile int test = 0;
  16.  
  17. int lastMSB = 0;
  18. int lastLSB = 0;
  19.  
  20. int color_value = 0;
  21.  
  22. typedef enum{
  23.   Color_NONE = 0,
  24.   Color_RED = 1,
  25.   Color_GREEN = 2,
  26.   Color_BLUE = 3
  27. }Color_Change_t;
  28.  
  29. Adafruit_NeoPixel strip = Adafruit_NeoPixel(LICZBA_DIOD, PIN_GPIO_DIODE_DATA, NEO_GRB + NEO_KHZ800);
  30.  
  31. void Color_Value_Up();
  32. void Color_Value_Down();
  33. int Check_Value(int Value);
  34. void Update_Diod();
  35. void Print_RGB_Data();
  36.  
  37. void setup()
  38. {
  39.   Serial.begin (9600);
  40.   while (!Serial)
  41.  
  42.   Serial.println();
  43.   Serial.println("Inicjalizacja...");
  44.   Serial.flush();
  45.  
  46.   strip.begin();
  47.   strip.show();
  48.  
  49.   Serial.println();
  50.   Serial.println("OK");
  51.  
  52.   pinMode(ENC_PIN_1, INPUT);
  53.   pinMode(ENC_PIN_2, INPUT);
  54.  
  55.   pinMode(ENC_PIN_BUTTON, INPUT);
  56.  
  57.   digitalWrite(ENC_PIN_1, HIGH);
  58.   digitalWrite(ENC_PIN_2, HIGH);
  59.  
  60.   digitalWrite(ENC_PIN_BUTTON, HIGH);
  61.  
  62.   attachInterrupt(0, Encoder_Check, CHANGE);
  63.   attachInterrupt(1, Encoder_Check, CHANGE);
  64. }
  65.  
  66. void loop()
  67. {
  68.   if(digitalRead(ENC_PIN_BUTTON))
  69.   {;}
  70.   else{
  71.     if(Color_BLUE==color_value) { color_value=0;}
  72.     color_value++;
  73.     Serial.println(color_value);
  74.   }
  75.  
  76.   Print_RGB_Data();
  77. }
  78.  
  79. void Encoder_Check()
  80. {
  81.   int MSB = digitalRead(ENC_PIN_1); //MSB = most significant bit
  82.   int LSB = digitalRead(ENC_PIN_2); //LSB = least significant bit
  83.  
  84.   int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
  85.   int sum  = (Last_Encoded << 2) | encoded; //adding it to the previous encoded value
  86.  
  87.   if(sum == 0x0D || sum == 0x04 || sum == 0x02 || sum == 0x0B) { Color_Value_Up(); }
  88.   if(sum == 0x0E || sum == 0x07 || sum == 0x01 || sum == 0x08) { Color_Value_Down(); }
  89.  
  90.   Last_Encoded = encoded; //store this value for next time
  91. }
  92.  
  93. void Color_Value_Up()
  94. {
  95.   if(color_value == Color_NONE)
  96.   { }
  97.   else if(color_value == Color_RED)
  98.   {
  99.     encoderValue_RED++;
  100.     encoderValue_RED = Check_Value(encoderValue_RED);
  101.     Update_Diod();
  102.   }
  103.   else if(color_value == Color_GREEN)
  104.   {
  105.     encoderValue_GREEN++;
  106.     encoderValue_GREEN = Check_Value(encoderValue_GREEN);
  107.     Update_Diod();
  108.   }
  109.   else if(color_value == Color_BLUE)
  110.   {
  111.     encoderValue_BLUE++;
  112.     encoderValue_BLUE = Check_Value(encoderValue_BLUE);
  113.     Update_Diod();
  114.   }
  115. }
  116.  
  117. void Color_Value_Down()
  118. {
  119.   if(color_value == Color_NONE)
  120.   { }
  121.   else if(color_value == Color_RED)
  122.   {
  123.     encoderValue_RED--;
  124.     encoderValue_RED = Check_Value(encoderValue_RED);
  125.     Update_Diod();
  126.   }
  127.   else if(color_value == Color_GREEN)
  128.   {
  129.     encoderValue_GREEN--;
  130.     encoderValue_GREEN = Check_Value(encoderValue_GREEN);
  131.     Update_Diod();
  132.   }
  133.   else if(color_value == Color_BLUE)
  134.   {
  135.     encoderValue_BLUE--;
  136.     encoderValue_BLUE = Check_Value(encoderValue_BLUE);
  137.     Update_Diod();
  138.   }
  139. }
  140.  
  141. int Check_Value(int Value)
  142. {
  143.   if(Value >= 255) { Value = 255; }
  144.   else if(Value <= 0) {Value = 0; }
  145.   return Value;
  146. }
  147.  
  148. void Update_Diod()
  149. {
  150.     for(int i=0; i<LICZBA_DIOD; i++)
  151.     {
  152.      strip.setPixelColor(i, encoderValue_RED, encoderValue_GREEN, encoderValue_BLUE);
  153.      strip.show();
  154.     }
  155. }
  156.  
  157. void Print_RGB_Data()
  158. {
  159.   Serial.print("R: ");
  160.   Serial.print(encoderValue_RED);
  161.   Serial.print(" G: ");
  162.   Serial.print(encoderValue_GREEN);
  163.   Serial.print(" B: ");
  164.   Serial.println(encoderValue_BLUE);
  165.   delay(1000);
  166. }

W tym przykładzie wykorzystam enkoder obrotowy z przyciskiem. Wciśnięcie przycisku będzie powodowało zmianę koloru, który będzie ustawiany:

To zadania można także całkiem fajnie wykonać za pomocą trzech potencjometrów. Gdzie wartość z przetwornika. Jest on 10 bitowy (0 do 1023) wobec tego dobrze byłoby zmapować te wartości na akceptowane dla diod (0 do 255). Dzięki temu można by było dynamicznie regulować podawanymi wartościami za pomocą trzech pokręteł. Wydaje mi się, że takie rozwiązanie byłoby bardziej wygodne niż za pomocą enkodera z przyciskiem.

Jeśli chodzi o zapalanie ich pojedynczo to wykorzystuje się tą samą funkcję czyli setPixelColor oraz show. Jedyną różnicą jest wywołanie jej ponownie dla diody którą chcemy zgasić z wartościami kolorów 0,0,0.