Skip to content

Commit

Permalink
encoder updates
Browse files Browse the repository at this point in the history
  • Loading branch information
tigoe committed Nov 6, 2019
1 parent 5ae6754 commit a60c3c1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
65 changes: 65 additions & 0 deletions Encoder_Button_Steps/Encoder_Button_Steps.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Encoder and button reader.
Most encoders have a detent every 4 steps. This example reads
for the detent steps.
Circuit:
- rotary encoder attached to pins 2 and 3
- pushbutton attached to pin 4, connected to ground
created 23 Feb 2019
by Tom Igoe
*/

#include <Encoder.h>

const int buttonPin = 4; // pushbutton pin
Encoder knob(2, 3); // encoder on pins 2 and 3
int lastButtonState = LOW; // last button state
int lastKnobState = -1; // last knob state
int timestamp = 0; // timestamp

int steps = 0;
const int maxSteps = 24;

void setup() {
// initialize button pin and Serial and keyboard:
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
}

void loop() {
// read the sensors:
int buttonState = digitalRead(buttonPin);
int knobState = knob.read();

// if the button has changed:
if (buttonState != lastButtonState) {
// debounce the button:
delay(10);
// if button is pressed:
if (buttonState == LOW) {
Serial.println("button press");
} else {
Serial.println("button release");
}
// save current state for next time through the loop:
lastButtonState = buttonState;
}

// compare current and last knob state:
int knobChange = knobState - lastKnobState;
// if it's changed by 4 or more (one step):
if (abs(knobChange) >= 4) {
// get the direction (-1 or 1):
int knobDirection = (knobChange / abs(knobChange));
steps += knobDirection;
// if you want to make the steps rollover, use this:
//steps = steps % maxSteps;

Serial.println(steps);

// save knobState for next time through loop:
lastKnobState = knobState;
}
}
4 changes: 3 additions & 1 deletion Encoder_reset/Encoder_reset.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
http://www.pjrc.com/teensy/td_libs_Encoder.html
created 19 Feb 2018
modified 6 Nov 2019
by Tom Igoe
*/

#include <Encoder.h>
Encoder thisEncoder(6, 7);
Encoder thisEncoder(2,3);
const int buttonPin = 4;
long lastPosition = -1;
int lastButtonState = 0;
Expand Down Expand Up @@ -40,6 +41,7 @@ void loop() {
if (buttonState == LOW) {
// reset the encoder count:
thisEncoder.write(0);
Serial.println("reset");
}
}
// save current state for comparison next time:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,3 @@ void loop() {
Serial.print(",");
Serial.println(analogStDev);
}





0 comments on commit a60c3c1

Please sign in to comment.