Created
July 4, 2021 20:23
-
-
Save thoraxe/f00f374eaab7b25974005c712a8a14a4 to your computer and use it in GitHub Desktop.
Terrible trimpot reader for Teens/Arduino
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 the Bounce2 library found here : | |
// https://github.com/thomasfredericks/Bounce2 | |
#include <Bounce2.h> | |
// INSTANTIATE A Button OBJECT | |
Bounce2::Button button = Bounce2::Button(); | |
// WE WILL attach() THE BUTTON TO THE FOLLOWING PIN IN setup() | |
#define BUTTON_PIN 11 | |
#define JOYSTICK_BUTTON 1 | |
// DEFINE THE PIN FOR THE LED : | |
// 1) SOME BOARDS HAVE A DEFAULT LED (LED_BUILTIN) | |
//#define LED_PIN LED_BUILTIN | |
// 2) OTHERWISE SET YOUR OWN PIN | |
#define LED_PIN 13 | |
// logging interval in milliseconds | |
#define AD_READ_INTERVAL 100 | |
long previous_millis = 0; | |
// the input rotary device | |
#define TRIM_POT A0 | |
#define TRIM_POT_SCALE 1023 | |
#define TRIM_POT_POSITIONS 12 | |
#define TRIM_POT_MINUS_BUTTON 2 | |
#define TRIM_POT_PLUS_BUTTON 3 | |
int previous_position = -1; | |
int current_position = -1; | |
// -1 is CCW, 1 is CW | |
int move_direction = 0; | |
// number of milliseconds to hold the joystick value | |
#define TRIM_POT_MILLIS 250 | |
// when the last event was | |
int previous_pot_millis = 0; | |
void setup() { | |
// BUTTON SETUP | |
// SELECT ONE OF THE FOLLOWING : | |
// 1) IF YOUR BUTTON HAS AN INTERNAL PULL-UP | |
// button.attach( BUTTON_PIN , INPUT_PULLUP ); // USE INTERNAL PULL-UP | |
// 2) IF YOUR BUTTON USES AN EXTERNAL PULL-UP | |
button.attach( BUTTON_PIN, INPUT_PULLUP ); // USE EXTERNAL PULL-UP | |
// DEBOUNCE INTERVAL IN MILLISECONDS | |
button.interval(50); | |
// INDICATE THAT THE LOW STATE CORRESPONDS TO PHYSICALLY PRESSING THE BUTTON | |
button.setPressedState(LOW); | |
// LED SETUP | |
pinMode(LED_PIN,OUTPUT); | |
// analog input | |
//pinMode(TRIM_POT,INPUT); | |
// initialize the LED to be off | |
digitalWrite(LED_PIN,LOW); | |
// calculate the initial position of the rotary to set the "previous position" | |
// in this way we'll only detect change after moving it on boot | |
int pot_v = analogRead(TRIM_POT); | |
previous_position = calculatePotPosition(pot_v, TRIM_POT_SCALE, TRIM_POT_POSITIONS); | |
current_position = previous_position; | |
} | |
void loop() { | |
// checking trim pots | |
unsigned long current_millis = millis(); | |
if (current_millis - previous_millis > AD_READ_INTERVAL) { | |
// get the analog voltage on the trim pot pin | |
// this is not a pure voltage but an A/D value | |
int pot_v = analogRead(TRIM_POT); | |
//Serial.print(TRIM_POT); | |
//Serial.print(": "); | |
//Serial.println(pot_v); | |
current_position = calculatePotPosition(pot_v, TRIM_POT_SCALE, TRIM_POT_POSITIONS); | |
previous_millis = current_millis; | |
//Serial.print("Current: "); | |
//Serial.println(current_position); | |
} | |
// UPDATE THE BUTTON | |
// YOU MUST CALL THIS EVERY LOOP | |
button.update(); | |
if ( button.pressed() ) { | |
digitalWrite(LED_PIN,HIGH); | |
//Serial.println("Button pressed!"); | |
Joystick.button(JOYSTICK_BUTTON,HIGH); | |
} | |
if ( button.released() ) { | |
digitalWrite(LED_PIN,LOW); | |
//Serial.println("Button released!"); | |
Joystick.button(JOYSTICK_BUTTON,LOW); | |
} | |
//Serial.print("Previous: "); | |
//Serial.println(previous_position); | |
// check if the trim pot position has changed | |
if (current_position != previous_position) { | |
Serial.println("Pot position changed!"); | |
// check which direction it moved | |
if (current_position < previous_position) { | |
// pot turned clockwise / right | |
move_direction = 1; | |
Serial.print("Pot turned right: "); | |
Serial.println(move_direction); | |
} else if (current_position > previous_position) { | |
// pot turned counter / left | |
move_direction = -1; | |
Serial.print("Pot turned left: "); | |
Serial.println(move_direction); | |
} | |
previous_position = current_position; | |
previous_pot_millis = current_millis; | |
if (move_direction == -1) { | |
// this is a counter-clockwise rotation, so press the minus joystick button | |
Joystick.button(TRIM_POT_MINUS_BUTTON, HIGH); | |
Serial.println("MINUS ON"); | |
} else if (move_direction == 1) { | |
// this is a clockwise rotation, so press the plus button | |
Joystick.button(TRIM_POT_PLUS_BUTTON, HIGH); | |
Serial.println("PLUS ON"); | |
} | |
} | |
// check how long we have been in this move direction | |
if (current_millis - previous_pot_millis > TRIM_POT_MILLIS) { | |
// check the last recorded move direction | |
if (move_direction == -1) { | |
// turn off the minus button | |
Joystick.button(TRIM_POT_MINUS_BUTTON, LOW); | |
Serial.println("MINUS OFF"); | |
} else if (move_direction == 1) { | |
// turn off the plus button | |
Joystick.button(TRIM_POT_PLUS_BUTTON, LOW); | |
Serial.println("PLUS OFF"); | |
} | |
move_direction = 0; | |
previous_pot_millis = current_millis; | |
} | |
//delay(500); | |
} | |
int calculatePotPosition(int val, int scale, int positions) { | |
int result = val / (scale / positions); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment