Created
December 22, 2022 23:47
-
-
Save MikahB/67d3e00dac024b8a83895423a229e1be to your computer and use it in GitHub Desktop.
Bounce2 / analogWrite Problems on Teensy 4.1
This file contains hidden or 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 <Arduino.h> | |
#include <Bounce2.h> | |
#include <EEPROM.h> | |
// input states | |
enum inputState{off, low, medium, high, reset}; | |
// input actions | |
enum inputAction {shortPress, longPress}; | |
// Pin 13 has an onboard LED on the Teensy 4.1 | |
int outputPins[] = {13}; | |
int inputPins[] = {2}; | |
constexpr int bounceTime = 20; | |
constexpr u_int16_t INPUT0_LASTVAL = 1000; | |
Button buttons[1]; | |
inputState CurrentInputState; | |
// this indicates what mode we're starting in | |
// todo: non-blocking | |
void Blink(int numBlinks, int timeVal = 100) { | |
if (numBlinks <= 0) {return;} | |
for (int i = 0; i < numBlinks; i++) { | |
digitalWrite(13, LOW); | |
delay(10); | |
digitalWrite(13, HIGH); | |
delay(timeVal); | |
digitalWrite(13, LOW); | |
delay(timeVal); | |
} | |
} | |
// this actually sets the output | |
// note: need to fix terminology between input and output states | |
void SetOutputState(inputState newState, int outputNumber = 0) { | |
CurrentInputState = newState; | |
switch (newState) | |
{ | |
case off: | |
digitalWrite(outputPins[outputNumber], LOW); | |
return; // explicitly, without changing last state | |
break; | |
case low: | |
Blink(1); | |
//analogWrite(outputPins[outputNumber], 128); | |
break; | |
case medium: | |
Blink(2); | |
//analogWrite(outputPins[outputNumber], 192); | |
break; | |
case high: | |
Blink(3); | |
//analogWrite(outputPins[outputNumber], 256); | |
break; | |
} | |
// if we didn't turn it off, store this value for later | |
EEPROM.write(INPUT0_LASTVAL, newState); | |
} | |
// this runs the state machine | |
void UpdateInputState(inputAction action) { | |
// if it's off, just turn on to previous setting | |
if (CurrentInputState == off) { | |
inputState lastSetting = static_cast<inputState>(EEPROM.read(INPUT0_LASTVAL)); | |
SetOutputState(lastSetting); | |
} | |
// if it's NOT off, figure out what to do based on action | |
else { | |
if (action == shortPress) | |
SetOutputState(off); | |
else { | |
// yuck - cast both ways to increment | |
inputState nextState = static_cast<inputState>(static_cast<int>(CurrentInputState) + 1); | |
if (nextState == reset) nextState = low; // if we're cycling off off high, go back to low | |
SetOutputState(nextState); | |
} | |
} | |
} | |
void setup() { | |
// initialize the output pins | |
for (int oPin: outputPins) | |
pinMode(oPin, OUTPUT); | |
// little different iterate since we need pin# and location | |
for (uint16_t i = 0; i < (sizeof(inputPins) / sizeof(inputPins[0])); i++) { | |
int pinNumber = inputPins[i]; | |
pinMode(pinNumber, INPUT_PULLDOWN); // pull low to not float | |
buttons[i].attach(pinNumber); | |
buttons[i].interval(bounceTime); | |
buttons[i].setPressedState(HIGH); // when pin goes high, btn is pressed | |
} | |
SetOutputState(off); | |
} | |
void loop() { | |
buttons[0].update(); | |
if (buttons[0].fell()) | |
{ | |
//Blink(4); | |
if (buttons[0].previousDuration() > 1000) | |
UpdateInputState(longPress); | |
else | |
UpdateInputState(shortPress); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment