Hi! First, I'd like to say that if this is the wrong place to post this, I'm sorry, and I'll move it to someplace better.
I'm trying to make a computer monitor back light with Neopixels, and I want it to have 4-5 different modes, controlled by the position of a rotary encoder. I found some code to read the encoder, but I've done some searching, and haven't been able to find anyone else changing modes with an encoder. Here is my code for reading the encoder:
#define ContactA 3
#define ContactB 4
// ----- Logic
boolean LastStateA;
boolean CurrentStateA;
boolean CurrentStateB;
// ----- Counter
int Count = 0;
int LastCount = Count;
int Mode1 = (LastCount =
// ==================================
// setup()
// ==================================
void setup() {
// ----- Configure Serial port
Serial.begin(9600);
// ----- Display initial count
Serial.print("Count: ");
Serial.println(Count);
// ----- Configure encoder
pinMode(ContactA, INPUT_PULLUP);
pinMode(ContactB, INPUT_PULLUP);
LastStateA = stateContactA();
}
// ==================================
// loop()
// ==================================
void loop() {
// ----- Poll Contact A
checkEncoder();
// ----- Display the count if it has changed
if (Count != LastCount) displayCount(); // Display the number of "detents"
}
// ==================================
// checkEncoder()
// ==================================
void checkEncoder() {
/*
The encoder output comprises two 90 degree offset squarewaves.
The direction of rotation is:
- clockwise if the state of output A is opposite to output B
- counterclockwise if the state of output A is the same as output B
*/
// ----- Check encoder output
CurrentStateA = stateContactA();
// ----- Record changes
if (CurrentStateA != LastStateA) {
CurrentStateB = digitalRead(ContactB); // Check state of ContactB
if (CurrentStateA == CurrentStateB) Count++; // Clockwise rotation
if (CurrentStateA != CurrentStateB) Count--; // Counterclockwise rotation
LastStateA = CurrentStateA;
}
}
// ==================================
// stateContactA()
// ==================================
boolean stateContactA() {
/*
Two integrators are used to suppress contact bounce.
The first integrator to reach MaxCount wins
*/
// ----- Locals
int Closed = 0; // Integrator
int Open = 0; // Integrator
int MaxCount = 5; // Increase this value if you see contact bounce
// ----- Debounce Contact A
while (1) {
// ----- Check ContactA
if (digitalRead(ContactA)) {
// ----- ContactA is Open
Closed = 0; // Empty opposite integrator
Open++; // Integrate
if (Open > MaxCount) return HIGH;
} else {
// ----- ContactA is Closed
Open = 0; // Empty opposite integrator
Closed++; // Integrate
if (Closed > MaxCount) return LOW;
}
}
}
// ==================================
// displayCount()
// ==================================
void displayCount() {
/*
There are two transitions between encoder detents.
This causes the "Count" to increment twice between indent clicks.
*/
// ----- Display "indents"
if (Count % 2 == 0) {
Serial.print("Count: ");
Serial.println(Count / 2); // Display number of indent clicks
}
LastCount = Count;
}
I haven't started coding the LEDs, as they're not here yet, and I figured I could work on this for now.
This is probably very simple, but I'm a beginner, so any suggestions would be appreciated!
Thanks!
Ethan