Created
May 1, 2016 19:28
-
-
Save newdigate/a3aaa3aed7aeb02299774d5d906fc31b to your computer and use it in GitHub Desktop.
Arduino - Rotary encoder using Neopixel ring indicator
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
#include <Adafruit_NeoPixel.h> | |
/* Read Quadrature Encoder | |
* Connect Encoder to Pins encoder0PinA, encoder0PinB, and +5V. | |
* | |
* Sketch by max wolf / www.meso.net | |
* v. 0.1 - very basic functions - mw 20061220 | |
* | |
*/ | |
#define PIN 9 | |
#define NUMPIXELS 16 | |
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); | |
int val; | |
int encoder0PinA = 2; | |
int encoder0PinB = 3; | |
int encoder0Pos = 0; | |
int encoder0PinALast = LOW; | |
int n = LOW; | |
void setup() { | |
pinMode (encoder0PinA,INPUT); | |
pinMode (encoder0PinB,INPUT); | |
pinMode (5, OUTPUT); | |
pinMode (6, OUTPUT); | |
digitalWrite (5, HIGH); | |
digitalWrite (6, LOW ); | |
Serial.begin (9600); | |
pixels.begin(); // This initializes the NeoPixel library. | |
} | |
void loop() { | |
n = digitalRead(encoder0PinA); | |
if ((encoder0PinALast == LOW) && (n == HIGH)) { | |
if (digitalRead(encoder0PinB) == LOW) { | |
encoder0Pos--; | |
} else { | |
encoder0Pos++; | |
} | |
Serial.print (encoder0Pos); | |
Serial.print ("\n"); | |
for (byte i=0; i<16; i++) { | |
if (encoder0Pos == 0) { | |
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); | |
} else if (encoder0Pos > 0) { | |
uint32_t pixelColor = (encoder0Pos > i)? Wheel((15-i)*16) : 0; | |
pixels.setPixelColor(i,pixelColor); | |
} else { | |
uint32_t pixelColor = (encoder0Pos < -i)? Wheel((i+1)*16) : 0; | |
pixels.setPixelColor(15-i, pixelColor); | |
} | |
} | |
pixels.show(); | |
} | |
encoder0PinALast = n; | |
} | |
// Input a value 0 to 255 to get a color value. | |
// The colours are a transition r - g - b - back to r. | |
uint32_t Wheel(byte WheelPos) { | |
WheelPos = 255 - WheelPos; | |
if(WheelPos < 85) { | |
return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3); | |
} | |
if(WheelPos < 170) { | |
WheelPos -= 85; | |
return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3); | |
} | |
WheelPos -= 170; | |
return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment