Advertisement
LeventeDaradici

Manually controlled LED strips with EC11 rotary encoder

Jan 6th, 2022
910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #define PIN 11
  3. #define NUMPIXELS 15
  4. Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  5.  
  6. #define encoderPinA 3
  7. #define encoderPinB 2
  8. #define clearButton 8
  9.  
  10. int encoderPos = 4;
  11. int lastReportedPos = 4;
  12. static boolean rotating=false;  
  13.  
  14. boolean A_set = false;
  15. boolean B_set = false;
  16. void setup() {
  17.   delay(2000);
  18.   pixels.begin();
  19.   pixels.setBrightness(64);
  20.   pixels.clear();
  21.  
  22.   //pixels.setPixelColor(1, pixels.Color(0, 150, 0));
  23.   //pixels.show();
  24.   pinMode(encoderPinA, INPUT_PULLUP);
  25.   pinMode(encoderPinB, INPUT_PULLUP);
  26.   pinMode(clearButton, INPUT_PULLUP);
  27.   attachInterrupt(0, doEncoderA, CHANGE);
  28.   attachInterrupt(1, doEncoderB, CHANGE);
  29.   Serial.begin(9600);
  30.   lastReportedPos = 5;
  31.   encoderPos = 5;
  32. }
  33. void loop() {
  34.   rotating = true;
  35.   if (lastReportedPos != encoderPos) {
  36.     Serial.print("Volume:");
  37.     Serial.println(encoderPos, DEC);
  38.     lastReportedPos = encoderPos;
  39.   }
  40.   if (digitalRead(clearButton) == LOW )  {
  41.     encoderPos = 0;
  42.   }
  43. }
  44.  
  45. void doEncoderA(){
  46.   if ( rotating ) delay (5);
  47.  
  48.   if( digitalRead(encoderPinA) != A_set ) {
  49.     A_set = !A_set;
  50.     if ( A_set && !B_set )
  51.       encoderPos += 1;
  52.       if (encoderPos > 14) { encoderPos = 14;}
  53.        pixels.clear();
  54.        pixels.setPixelColor(encoderPos, pixels.Color(0, 150, 0));
  55.        pixels.show();  
  56.     rotating = false;
  57.   }
  58. }
  59.  
  60. void doEncoderB(){
  61.   if ( rotating ) delay (5);
  62.   if( digitalRead(encoderPinB) != B_set ) {
  63.     B_set = !B_set;
  64.     if( B_set && !A_set )
  65.       encoderPos -= 1;
  66.       if (encoderPos <0 ) { encoderPos = 0; }
  67.        pixels.clear();
  68.        pixels.setPixelColor(encoderPos, pixels.Color(0, 150, 0));
  69.        pixels.show();  
  70.     rotating = false;
  71.   }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement