Created
November 25, 2018 17:26
-
-
Save kosso/e0d238aee088c7acbc7b9244acb679df to your computer and use it in GitHub Desktop.
Initial PoC sketch to 'write' words with LEDs while rolling a rotary encoder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define ENCODER_DO_NOT_USE_INTERRUPTS | |
#include <Encoder.h> | |
Encoder myEnc(12, 13); | |
#define ENC_SW 14 // Encoder Switch | |
long newapos = 0; | |
long lastapos = 0; | |
unsigned char words[]={ | |
B00000000, | |
B01111111, | |
B00001001, | |
B00001001, | |
B00001001, | |
B00000000, | |
B00111111, | |
B01000000, | |
B01000000, | |
B00111111, | |
B00000000, | |
B00111110, | |
B01000001, | |
B01000001, | |
B00100010, | |
B00000000, | |
B01111111, | |
B00011100, | |
B00100010, | |
B01000001, | |
B00000000 | |
}; | |
// Reset button | |
int buttonState; | |
int lastButtonState = LOW; | |
unsigned long lastDebounceTime = 0; | |
unsigned long debounceDelay = 50; | |
#include <Adafruit_NeoPixel.h> | |
#define LED_PIN 15 | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, LED_PIN, NEO_GRB + NEO_KHZ800); | |
// blue | |
uint32_t col = strip.Color(0,0,120); | |
void setup() { | |
Serial.begin(115200); | |
Serial.println(""); | |
Serial.println("ROCK STEADY LEDDY ready..."); | |
//pinMode(0, INPUT_PULLUP); // flash button | |
pinMode(14, INPUT_PULLUP); // Encoder SW button | |
strip.begin(); | |
strip.show(); // Initialize all pixels to 'off' | |
} | |
void loop() { | |
int reading = digitalRead(ENC_SW); | |
if (reading != lastButtonState) { | |
lastDebounceTime = millis(); | |
} | |
if((millis() - lastDebounceTime) > debounceDelay) { | |
if (reading != buttonState) { | |
buttonState = reading; | |
if (buttonState == HIGH) { | |
Serial.println("RESET ENCODER"); | |
newapos = -1; | |
lastapos = -1; | |
myEnc.write(-1); | |
// All LEDs off. | |
for(int i = 0; i < 8; i++){ | |
strip.setPixelColor(i, 0); | |
} | |
} | |
} | |
} | |
lastButtonState = reading; | |
// Read Encoder position. | |
long newPos = myEnc.read(); | |
newapos = newPos / 6; // 'resolution'. There are 4 pulses per encoder detent. Higher number keeps LEDs on longer as the roller rols. | |
if(newapos != lastapos){ | |
lastapos = newapos; | |
Serial.print(newapos % 21); // loop through 0-20 | |
Serial.print(" : "); | |
// get the row | |
byte bf = reverse(words[newapos % 21]); // reversing helps readability of initial byte array 'characters' when creating them for now. | |
// Set each of the 8 pixels depending on each BIT value in this BYTE of the array. | |
for(int i = 0; i < 8; i++){ | |
Serial.print(bitRead(bf,i)); | |
if( bitRead(bf,i) ){ | |
strip.setPixelColor(i, col); // ON | |
} else { | |
strip.setPixelColor(i, 0); // OFF | |
} | |
} | |
Serial.println(""); | |
// update strip | |
strip.show(); | |
} | |
} | |
// Reverse the bits in a byte | |
unsigned char reverse(unsigned char b) { | |
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4; | |
b = (b & 0xCC) >> 2 | (b & 0x33) << 2; | |
b = (b & 0xAA) >> 1 | (b & 0x55) << 1; | |
return b; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment