|
| 1 | +#include <Adafruit_NeoPixel.h> |
| 2 | +#include <RotaryEncoder.h> |
| 3 | + |
| 4 | +#define PIN_ENCODER_A 12 |
| 5 | +#define PIN_ENCODER_B 13 |
| 6 | +#define COM_A 11 // for wiring simplicity we set these output LOW |
| 7 | +#define COM_B SDA // ... but you can just wire these to GND instead! |
| 8 | +#define BUTTON_UP SCL |
| 9 | +#define BUTTON_LEFT 5 |
| 10 | +#define BUTTON_DOWN 6 |
| 11 | +#define BUTTON_RIGHT 9 |
| 12 | +#define BUTTON_IN 10 |
| 13 | + |
| 14 | +RotaryEncoder encoder(PIN_ENCODER_A, PIN_ENCODER_B, RotaryEncoder::LatchMode::TWO03); |
| 15 | +// This interrupt will do our encoder reading/checking! |
| 16 | +void checkPosition() { |
| 17 | + encoder.tick(); // just call tick() to check the state. |
| 18 | +} |
| 19 | +int last_rotary = 0; |
| 20 | + |
| 21 | + |
| 22 | +#define NUMPIXELS 12 |
| 23 | +Adafruit_NeoPixel pixels(NUMPIXELS, A0, NEO_GRB + NEO_KHZ800); |
| 24 | + |
| 25 | + |
| 26 | +void setup(void) { |
| 27 | + Serial.begin(115200); |
| 28 | +// while (!Serial); |
| 29 | + Serial.println("ANO encoder + NeoPixel test"); |
| 30 | + |
| 31 | + pinMode(COM_A, OUTPUT); |
| 32 | + digitalWrite(COM_A, LOW); |
| 33 | + pinMode(COM_B, OUTPUT); |
| 34 | + digitalWrite(COM_B, LOW); |
| 35 | + |
| 36 | + attachInterrupt(PIN_ENCODER_A, checkPosition, CHANGE); |
| 37 | + attachInterrupt(PIN_ENCODER_B, checkPosition, CHANGE); |
| 38 | + |
| 39 | + pinMode(BUTTON_UP, INPUT_PULLUP); |
| 40 | + pinMode(BUTTON_DOWN, INPUT_PULLUP); |
| 41 | + pinMode(BUTTON_LEFT, INPUT_PULLUP); |
| 42 | + pinMode(BUTTON_RIGHT, INPUT_PULLUP); |
| 43 | + pinMode(BUTTON_IN, INPUT_PULLUP); |
| 44 | + pixels.begin(); |
| 45 | + pixels.setBrightness(30); |
| 46 | + pixels.show(); |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +void loop(void) { |
| 51 | + // read encoder |
| 52 | + int curr_rotary = encoder.getPosition(); |
| 53 | + RotaryEncoder::Direction direction = encoder.getDirection(); |
| 54 | + |
| 55 | + pixels.clear(); |
| 56 | + if (curr_rotary != last_rotary) { |
| 57 | + Serial.print("Encoder value: "); |
| 58 | + Serial.print(curr_rotary); |
| 59 | + Serial.print(" direction: "); |
| 60 | + Serial.println((int)direction); |
| 61 | + } |
| 62 | + last_rotary = curr_rotary; |
| 63 | + |
| 64 | + pixels.setPixelColor((curr_rotary + (1000*NUMPIXELS)) % NUMPIXELS, pixels.Color(0, 150, 0)); |
| 65 | + |
| 66 | + if (! digitalRead(BUTTON_UP)) { |
| 67 | + pixels.setPixelColor(0, pixels.Color(150, 0, 0)); |
| 68 | + } |
| 69 | + if (! digitalRead(BUTTON_LEFT)) { |
| 70 | + pixels.setPixelColor(NUMPIXELS/4, pixels.Color(150, 0, 0)); |
| 71 | + } |
| 72 | + if (! digitalRead(BUTTON_DOWN)) { |
| 73 | + pixels.setPixelColor(NUMPIXELS/2, pixels.Color(150, 0, 0)); |
| 74 | + } |
| 75 | + if (! digitalRead(BUTTON_RIGHT)) { |
| 76 | + pixels.setPixelColor(NUMPIXELS*3/4, pixels.Color(150, 0, 0)); |
| 77 | + } |
| 78 | + if (! digitalRead(BUTTON_IN)) { |
| 79 | + pixels.fill(pixels.Color(50, 50, 50)); |
| 80 | + } |
| 81 | + pixels.show(); |
| 82 | + |
| 83 | + delay(20); |
| 84 | +} |
0 commit comments