// buttons
#define buttonL 7
#define buttonR 5
#define buttonU 4
#define buttonD 6
#define buttonReset 15
#define buttonScroll 19
// leds
#define ledL 18
#define ledR 13
#define ledU 12
#define ledD 9
// constants
const int blinkInterval = 1000; // still haven't figured this part out - want long press to have LED light for 1000ms
const int shortPress = 500;
const int longPress = 500;
const int maxCount = 500;
const int scrollPollRate = 50;
const int slowToFast = 10;
const int deepSleepTime = 6000;
// ints Last State
int buttonLLS = LOW;
int buttonRLS = LOW;
int buttonULS = LOW;
int buttonDLS = LOW;
int buttonResetLS = LOW;
int buttonScrollLS = LOW;
int ledLLS = LOW;
int ledRLS = LOW;
int ledULS = LOW;
int ledDLS = LOW;
// ints Current State
int buttonLCS;
int buttonRCS;
int buttonUCS;
int buttonDCS;
int buttonResetCS;
int buttonScrollCS;
int ledLCS;
int ledRCS;
int ledUCS;
int ledDCS;
// true / false bools - Button Press
bool buttonLPress = false;
bool buttonRPress = false;
bool buttonUPress = false;
bool buttonDPress = false;
bool buttonScrollPress = false;
bool buttonAny = false;
bool ledLOn = false;
bool ledROn = false;
bool ledUOn = false;
bool ledDOn = false;
// true false bools - Button Long
bool buttonLLong = false;
bool buttonRLong = false;
bool buttonULong = false;
bool buttonDLong = false;
bool buttonScrollLong = false;
// counting and recording millis - press and release time
unsigned long buttonLPressTime = 0;
unsigned long buttonLReleaseTime = 0;
unsigned long buttonRPressTime = 0;
unsigned long buttonRReleaseTime = 0;
unsigned long buttonUPressTime = 0;
unsigned long buttonUReleaseTime = 0;
unsigned long buttonDPressTime = 0;
unsigned long buttonDReleaseTime = 0;
unsigned long buttonScrollPressTime = 0;
unsigned long buttonScrollReleaseTime = 0;
// led on time millis
unsigned long ledLTurnedOn = 0;
unsigned long ledRTurnedOn = 0;
unsigned long ledUTurnedOn = 0;
unsigned long ledDTurnedOn = 0;
// unsigned long ledLCurrentTime = 0;
unsigned long ledRCurrentTime = 0;
unsigned long ledUCurrentTime = 0;
unsigned long ledDCurrentTime = 0;
// unsigned long for scroll timer
unsigned long oldCount = 0;
unsigned long newCount = 0;
unsigned long startTime = 0;
unsigned long currentTime = 0;
unsigned long scrollPollTimer = 0;
// items for Deep Sleep
unsigned long bootTime = 0;
unsigned long currentBootTime = 0;
unsigned long deepSleepCheck = 0;
bool deepSleepActive = false;
bool buttonPressedSomewhere = false;
void setup()
{
Serial.begin(115200);
Serial.println("Set up void completed");
//pinModes buttons
pinMode(buttonL, INPUT);
pinMode(buttonR, INPUT);
pinMode(buttonU, INPUT);
pinMode(buttonD, INPUT);
pinMode(buttonReset, INPUT);
pinMode(buttonScroll, INPUT);
//pinModes leds
pinMode(ledL, OUTPUT);
pinMode(ledR, OUTPUT);
pinMode(ledU, OUTPUT);
pinMode(ledD, OUTPUT);
startTime = millis();
bootTime = millis();
}
void loop()
{
currentBootTime = millis();
deepSleepCheck = currentBootTime - bootTime;
if(deepSleepActive == false && buttonLPress == true || buttonRPress == true || buttonUPress == true || buttonDPress == true || buttonScrollPress == true)
{
bootTime = millis();
deepSleepActive = false;
delay(10);
}
else if(deepSleepActive == false && deepSleepCheck >= deepSleepTime)
{
deepSleepActive = true;
Serial.println("I have entered Deep Sleep");
}
// left button and led
{
buttonLCS = digitalRead(buttonL);
if(buttonLCS == HIGH && buttonLLS == LOW) //button is pressed
{
buttonLPressTime = millis();
buttonLPress = true;
buttonLLong = false;
}
else if(buttonLCS == LOW && buttonLLS == HIGH) //button released
{
buttonLReleaseTime = millis();
buttonLPress = false;
if(buttonLPress == false && buttonLLong == false)
{
long lPressedDuration = buttonLReleaseTime - buttonLPressTime;
if(lPressedDuration < shortPress)
Serial.println("Left button 'Short Press'");
digitalWrite(ledL, HIGH);
digitalWrite(ledL, LOW);
}
}
if(buttonLPress == true && buttonLLong == false)
{
long lPressedDuration = millis() - buttonLPressTime;
if(lPressedDuration > longPress)
{
Serial.println("Left button 'Long Press'");
digitalWrite(ledL, HIGH);
buttonLLong = true;
}
}
buttonLLS = buttonLCS;
delay(50);
}
//right button & led
{
buttonRCS = digitalRead(buttonR);
if(buttonRCS == HIGH && buttonRLS == LOW) // right button pressed
{
buttonRPress = true;
buttonRLong = false;
buttonRPressTime = millis();
}
else if(buttonRCS == LOW && buttonRLS == HIGH) // right button released
{
buttonRPress = false;
buttonRReleaseTime = millis();
if(buttonRPress == false && buttonRLong == false)
{
long rPressedDuration = buttonRReleaseTime - buttonRPressTime;
if(rPressedDuration < shortPress)
Serial.println("Right button 'Short Press'");
digitalWrite(ledR, HIGH);
digitalWrite(ledR, LOW);
}
}
if(buttonRPress == true && buttonRLong == false)
{
long rPressedDuration = millis() - buttonRPressTime;
if(rPressedDuration > longPress)
{
Serial.println("Right button 'Long Press");
digitalWrite(ledR, HIGH);
buttonRLong = true;
}
}
buttonRLS = buttonRCS;
}
// Up button & led
{
buttonUCS = digitalRead(buttonU);
if(buttonUCS == HIGH && buttonULS == LOW) // Up button pressed
{
buttonUPress = true;
buttonULong = false;
buttonUPressTime = millis();
}
else if(buttonUCS == LOW && buttonULS == HIGH) // Up button released
{
buttonUPress = false;
buttonUReleaseTime = millis();
if(buttonUPress == false && buttonULong == false)
{
long uPressedDuration = buttonUReleaseTime - buttonUPressTime;
if(uPressedDuration < shortPress)
Serial.println("Up Button, 'Short Press'");
digitalWrite(ledU, HIGH);
digitalWrite(ledU, LOW);
}
}
if(buttonUPress == true && buttonULong == false)
{
long uPressedDuration = millis() - buttonUPressTime;
if(uPressedDuration > longPress)
{
Serial.println("Up button, 'Long Press'");
digitalWrite(ledU, HIGH);
buttonULong = true;
}
}
buttonULS = buttonUCS;
}
// Down button & led
{
buttonDCS = digitalRead(buttonD);
if(buttonDCS == HIGH && buttonDLS == LOW) // down button pressed
{
buttonDPress = true;
buttonDLong = false;
buttonDPressTime = millis();
}
else if(buttonDCS == LOW && buttonDLS == HIGH) // down button released
{
buttonDPress = false;
buttonDReleaseTime = millis();
if(buttonDPress == false && buttonDLong == false)
{
long dPressedDuration = buttonDReleaseTime - buttonDPressTime;
if(dPressedDuration < shortPress)
Serial.println("Down Button, 'Short Press'");
digitalWrite(ledD, HIGH);
digitalWrite(ledD, LOW);
}
}
if(buttonDPress == true && buttonDLong == false)
{
long dPressedDuration = millis() - buttonDPressTime;
if(dPressedDuration > longPress)
{
Serial.println("Down Button, 'Long Press'");
digitalWrite(ledD, HIGH);
buttonDLong = true;
}
}
buttonDLS = buttonDCS;
}
// This is the reset button for the LEDs
{
buttonResetCS = digitalRead(buttonReset);
if(buttonResetCS == HIGH && buttonResetLS == LOW) //reset pressed
{
digitalWrite(ledL, LOW);
digitalWrite(ledR, LOW);
digitalWrite(ledU, LOW);
digitalWrite(ledD, LOW);
Serial.println("All LEDs RESET");
}
buttonResetLS = buttonResetCS;
}
// This is for the scroll button
{
buttonScrollCS = digitalRead(buttonScroll);
currentTime = millis();
scrollPollTimer = currentTime - startTime;
if(scrollPollTimer >= scrollPollRate)
{
if(buttonScrollCS == HIGH) //button pressed
{
buttonScrollPress = true;
buttonScrollLong = false;
}
else if(buttonScrollCS == LOW) //button released
{
buttonScrollPress = false;
oldCount = 0;
}
if(buttonScrollPress == true && newCount < slowToFast)
{
Serial.print("Scroll button SLOW, value is: ");
newCount = oldCount + 1;
long differenceCount = newCount - oldCount;
Serial.print(differenceCount);
Serial.print(" , new count total value is: ");
Serial.println(newCount);
}
if(newCount >= slowToFast && newCount < maxCount)
{
Serial.print("Scroll button FAST, value is: ");
newCount = oldCount * 1.25;
long differenceCount = newCount - oldCount
Serial.println(differenceCount);
}
else if(newCount >= maxCount)
{
long differenceCount = newCount - oldCount;
Serial.print("Scroll button MAX, value is: ");
Serial.print(differenceCount);
Serial.print(" , new count total value is: ");
Serial.println(newCount);
}
oldCount = newCount;
startTime = currentTime;
}
}
}
Loading
esp32-c6-devkitc-1
esp32-c6-devkitc-1