Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_NeoPixel.h>
- #define PIN 11
- #define NUMPIXELS 15
- Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
- #define encoderPinA 3
- #define encoderPinB 2
- #define clearButton 8
- int encoderPos = 4;
- int lastReportedPos = 4;
- static boolean rotating=false;
- boolean A_set = false;
- boolean B_set = false;
- void setup() {
- delay(2000);
- pixels.begin();
- pixels.setBrightness(64);
- pixels.clear();
- //pixels.setPixelColor(1, pixels.Color(0, 150, 0));
- //pixels.show();
- pinMode(encoderPinA, INPUT_PULLUP);
- pinMode(encoderPinB, INPUT_PULLUP);
- pinMode(clearButton, INPUT_PULLUP);
- attachInterrupt(0, doEncoderA, CHANGE);
- attachInterrupt(1, doEncoderB, CHANGE);
- Serial.begin(9600);
- lastReportedPos = 5;
- encoderPos = 5;
- }
- void loop() {
- rotating = true;
- if (lastReportedPos != encoderPos) {
- Serial.print("Volume:");
- Serial.println(encoderPos, DEC);
- lastReportedPos = encoderPos;
- }
- if (digitalRead(clearButton) == LOW ) {
- encoderPos = 0;
- }
- }
- void doEncoderA(){
- if ( rotating ) delay (5);
- if( digitalRead(encoderPinA) != A_set ) {
- A_set = !A_set;
- if ( A_set && !B_set )
- encoderPos += 1;
- if (encoderPos > 14) { encoderPos = 14;}
- pixels.clear();
- pixels.setPixelColor(encoderPos, pixels.Color(0, 150, 0));
- pixels.show();
- rotating = false;
- }
- }
- void doEncoderB(){
- if ( rotating ) delay (5);
- if( digitalRead(encoderPinB) != B_set ) {
- B_set = !B_set;
- if( B_set && !A_set )
- encoderPos -= 1;
- if (encoderPos <0 ) { encoderPos = 0; }
- pixels.clear();
- pixels.setPixelColor(encoderPos, pixels.Color(0, 150, 0));
- pixels.show();
- rotating = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement