|
| 1 | +/* |
| 2 | + Encoder change with pushbutton |
| 3 | +
|
| 4 | + Reads an encoder and its pushbutton and uses the changes |
| 5 | + to control an LED. The knob turns the intensity up and down |
| 6 | + and the button turns it on and off, saving the intensity |
| 7 | + when turned off for when it turns back on. |
| 8 | +
|
| 9 | + created 17 June 2019 |
| 10 | + by Tom Igoe |
| 11 | +*/ |
| 12 | + |
| 13 | +#include <Encoder.h> |
| 14 | + |
| 15 | +const int buttonPin = 5; // pushbutton is on pin 5 |
| 16 | +const int ledPin = 3; // LED is on pin 3 |
| 17 | +Encoder knob(0, 1); // initialize encoder on pins 0 and 1 |
| 18 | +int lastKnobState = 0; // set last knob state |
| 19 | +int lastButtonState = 0; // set last button state |
| 20 | +int intensity = 0; // intensity of the LED |
| 21 | +int lastIntensity = 0; // intensity before LED was last turned off |
| 22 | + |
| 23 | +void setup() { |
| 24 | + // open serial communications |
| 25 | + Serial.begin(9600); |
| 26 | + // set I/O pin states: |
| 27 | + pinMode (buttonPin, INPUT_PULLUP); |
| 28 | + pinMode (ledPin, OUTPUT); |
| 29 | +} |
| 30 | + |
| 31 | +void loop() { |
| 32 | + // add any encoder knob change to the intensity: |
| 33 | + intensity = intensity + readEncoderChange(); |
| 34 | + // constrain it to 0-255: |
| 35 | + intensity = constrain(intensity, 0, 255); |
| 36 | + |
| 37 | + // if the button is released: |
| 38 | + if (readButtonChange() == 1) { |
| 39 | + // if the light is on, turn it off: |
| 40 | + if (intensity > 0) { |
| 41 | + // save intensity before it's turned off: |
| 42 | + lastIntensity = intensity; |
| 43 | + intensity = 0; |
| 44 | + } else { |
| 45 | + // restore it to the last intensity before turning off: |
| 46 | + intensity = lastIntensity; |
| 47 | + } |
| 48 | + } |
| 49 | + // update the intensity of the LED: |
| 50 | + analogWrite(ledPin, intensity); |
| 51 | +} |
| 52 | + |
| 53 | +int readEncoderChange() { |
| 54 | + // returns 0 for no change, -1 for one direction, 1 for the other: |
| 55 | + int result = 0; |
| 56 | + // read the encoder: |
| 57 | + int knobState = knob.read(); |
| 58 | + //get the change in the encoder: |
| 59 | + int knobChange = knobState - lastKnobState; |
| 60 | + // if it's changed by 4 or more pulses: |
| 61 | + if (abs(knobChange) >= 4) { |
| 62 | + // You just want direction of change, not value, so |
| 63 | + // divide value by abs. value to get 1 or -1: |
| 64 | + result = knobChange / abs(knobChange); |
| 65 | + // save the current state for comparison next time: |
| 66 | + lastKnobState = knobState; |
| 67 | + } |
| 68 | + return result; |
| 69 | +} |
| 70 | + |
| 71 | +int readButtonChange() { |
| 72 | + // returns 0 for no change, 1 for HIGH, -1 for LOW |
| 73 | + int result = 0; |
| 74 | + // read the pushbutton: |
| 75 | + int buttonState = digitalRead(buttonPin); |
| 76 | + // if the pushbutton has changed: |
| 77 | + if (buttonState != lastButtonState) { |
| 78 | + // wait a few milliseconds to debounce the button |
| 79 | + delay(3); |
| 80 | + // print the type of change, depending on the |
| 81 | + // state of the button: |
| 82 | + if (buttonState == LOW) { |
| 83 | + result = -1; |
| 84 | + } else { |
| 85 | + result = 1; |
| 86 | + } |
| 87 | + // save the current button state for comparison next time: |
| 88 | + lastButtonState = buttonState; |
| 89 | + } |
| 90 | + return result; |
| 91 | +} |
0 commit comments