Hi, I'm not a professional programmer, just a hobbyist and I want to make sure I'm going about my programming statements and methods in the right way. I appreciate any help and I aim to not waste anyone's time. That said, here is probably too much information. code & questions are at the end.
What it does:
- A rotary encoder sequentially turns on one led at a time out of an array of ten. very exciting.
Wiring details:
- here's a photo. I didn't make a diagram because it's pretty simple and my questions are all software related.
PJRC & other products used:
- teensy 3.2
- pjrc audio shield(not being used in this sketch)
- PEC11 rotary encoder https://cdn-shop.adafruit.com/datasheets/pec11.pdf
- 3mm blue LEDs https://www.adafruit.com/product/780
- resistors 2.2k ohm(all I had on hand)
Software setup:
- building via Arduino 1.8.5 on OSX 10.12.6
Code questions:
1. is this an okay way to debounce a rotary encoder(line 29 & 36)?
2. in the switch case is there a way to avoid repeating: digitalWrite(ledPins[ledSetting], HIGH); for each case?
3. For handling the encoder, starting on line 28, are the nested if statements the best way to go about that? It feels clunky to me, but I can't think of a way to reduce the code.
4. In each switch case is there a way to run allSettingsLedsOff() once per case or a better way to achieve turning off the last LED that was on. Since only one should be on at any given time I figured keeping track of a history state wasn't necessary and while this works it bothers me that the active LED is blinking with each loop(albeit imperceptible at full speed).
Any and all advice is welcomed. Thank you.
What it does:
- A rotary encoder sequentially turns on one led at a time out of an array of ten. very exciting.
Wiring details:
- here's a photo. I didn't make a diagram because it's pretty simple and my questions are all software related.
PJRC & other products used:
- teensy 3.2
- pjrc audio shield(not being used in this sketch)
- PEC11 rotary encoder https://cdn-shop.adafruit.com/datasheets/pec11.pdf
- 3mm blue LEDs https://www.adafruit.com/product/780
- resistors 2.2k ohm(all I had on hand)
Software setup:
- building via Arduino 1.8.5 on OSX 10.12.6
Code:
#include <Encoder.h>
// things on pins
Encoder rotaryEnc(4, 5);
int ledPins[] = {14, 12, 16, 17, 11, 7, 20, 21, 22, 23};
// global vars
int pinCount = 10;
long oldPosition = -999;
int ledSetting = 1;
// functions
void allSettingsLedsOff() {
for (int currentPin = 0; currentPin < pinCount; currentPin++) {
digitalWrite(ledPins[currentPin], LOW);
}
}
void setup() {
for (int currentPin = 0; currentPin < pinCount; currentPin++) {
pinMode(ledPins[currentPin], OUTPUT);
}
}
void loop() {
// encoder knob sets active setting via the ledSetting var
long newPosition = rotaryEnc.read();
if (newPosition > oldPosition + 2) { // QUESTION 1
if (ledSetting > 0) {
ledSetting--;
} else {
ledSetting = pinCount - 1;
}
oldPosition = newPosition;
} else if (newPosition < oldPosition - 2) {
if (ledSetting < pinCount - 1) {
ledSetting++;
} else {
ledSetting = 0;
}
oldPosition = newPosition;
}
// do stuff per setting
switch (ledSetting) {
case 0:
allSettingsLedsOff();
digitalWrite(ledPins[ledSetting], HIGH);
break;
case 1:
allSettingsLedsOff();
digitalWrite(ledPins[ledSetting], HIGH);
break;
case 2:
allSettingsLedsOff();
digitalWrite(ledPins[ledSetting], HIGH);
break;
case 3:
allSettingsLedsOff();
digitalWrite(ledPins[ledSetting], HIGH);
break;
case 4:
allSettingsLedsOff();
digitalWrite(ledPins[ledSetting], HIGH);
break;
case 5:
allSettingsLedsOff();
digitalWrite(ledPins[ledSetting], HIGH);
break;
case 6:
allSettingsLedsOff();
digitalWrite(ledPins[ledSetting], HIGH);
break;
case 7:
allSettingsLedsOff();
digitalWrite(ledPins[ledSetting], HIGH);
break;
case 8:
allSettingsLedsOff();
digitalWrite(ledPins[ledSetting], HIGH);
break;
case 9:
allSettingsLedsOff();
digitalWrite(ledPins[ledSetting], HIGH);
break;
}
}
Code questions:
1. is this an okay way to debounce a rotary encoder(line 29 & 36)?
2. in the switch case is there a way to avoid repeating: digitalWrite(ledPins[ledSetting], HIGH); for each case?
3. For handling the encoder, starting on line 28, are the nested if statements the best way to go about that? It feels clunky to me, but I can't think of a way to reduce the code.
4. In each switch case is there a way to run allSettingsLedsOff() once per case or a better way to achieve turning off the last LED that was on. Since only one should be on at any given time I figured keeping track of a history state wasn't necessary and while this works it bothers me that the active LED is blinking with each loop(albeit imperceptible at full speed).
Any and all advice is welcomed. Thank you.