Last active
June 7, 2020 14:58
-
-
Save adrianjost/c50b24405393e344d6bdac964b184588 to your computer and use it in GitHub Desktop.
Arduino WWCW One Button Control
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
#define LED_WW 5 | |
#define LED_CW 6 | |
#define BTN 7 | |
#define BRIGHTNESS_MAX 255 | |
#define TIMEOUT 500 | |
#define TIMEOUT_INFINITY 60000 // 1min | |
#define BRIGHNESS_STEP 1 | |
#define BRIGHNESS_STEP_DURATION 15 | |
#define TEMP_STEP 0.01 | |
#define TEMP_STEP_DURATION 50 | |
int brightness = 0; | |
float temp = 0.5; | |
void setup() | |
{ | |
pinMode(LED_WW, OUTPUT); | |
pinMode(LED_CW, OUTPUT); | |
pinMode(BTN, INPUT); | |
} | |
void setLED(byte ww, byte cw) { | |
analogWrite(LED_WW, ww); | |
analogWrite(LED_CW, cw); | |
} | |
bool waitForBtn(int ms, bool state = true) { | |
long start = millis(); | |
while (millis() - start < ms) { | |
delay(50); | |
if (digitalRead(BTN) == state) { | |
return true; | |
} | |
} | |
return false; | |
} | |
void updateLED() { | |
float val = (temp < 0.5 ? temp : (temp - 0.5)) * 2; | |
if (temp < 0.5) { | |
setLED(brightness, constrain(brightness - (brightness * val), 0, BRIGHTNESS_MAX)); | |
} else { | |
setLED(constrain(brightness - (brightness * (1 - val)), 0, BRIGHTNESS_MAX), brightness); | |
} | |
} | |
// tap | |
// toggle power | |
//brightness = (brightness == 0) ? 100 : 0; | |
//updateLED(); | |
void loop() { | |
if (waitForBtn(TIMEOUT_INFINITY, true)) { | |
if (waitForBtn(TIMEOUT, false)) { | |
if (waitForBtn(TIMEOUT, true)) { | |
if (waitForBtn(TIMEOUT, false)) { | |
if (waitForBtn(TIMEOUT, true)) { | |
if (waitForBtn(TIMEOUT, false)) { | |
if (waitForBtn(TIMEOUT, true) && !waitForBtn(TIMEOUT, false)) { | |
// tap tap tap hold | |
// cycle temp - | |
while (digitalRead(BTN)) { | |
temp -= TEMP_STEP; | |
if (temp <= 0) { | |
temp += 1; | |
} | |
updateLED(); | |
delay(TEMP_STEP_DURATION); | |
} | |
} | |
} else { | |
// tap tap hold | |
// cycle temp + | |
while (digitalRead(BTN)) { | |
temp += TEMP_STEP; | |
if (temp >= 1) { | |
temp -= 1; | |
} | |
updateLED(); | |
delay(TEMP_STEP_DURATION); | |
} | |
} | |
} else { | |
// tap tap | |
} | |
} else { | |
// tap, hold | |
// increase brightness | |
while (digitalRead(BTN) && brightness + BRIGHNESS_STEP < BRIGHTNESS_MAX) { | |
brightness += BRIGHNESS_STEP; | |
updateLED(); | |
delay(BRIGHNESS_STEP_DURATION); | |
} | |
} | |
} else { | |
// tap | |
// toggle power | |
brightness = (brightness == 0) ? 100 : 0; | |
updateLED(); | |
} | |
} else { | |
// hold | |
// reduce brightness | |
while (digitalRead(BTN) && brightness - BRIGHNESS_STEP >= 0) { | |
brightness -= BRIGHNESS_STEP; | |
updateLED(); | |
delay(BRIGHNESS_STEP_DURATION); | |
} | |
} | |
} | |
waitForBtn(TIMEOUT_INFINITY, false); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
tap
hold
tap
hold
tap
tap
hold
tap
tap
tap
hold