Hello all, after days of trying I need some help with this one.
Goal: 1 pushbutton dimming if it's HIGH for more than 2 seconds (cycle trough 0%--100%--0%, and/or if it's HIGH for less than 2 seconds set dimming to 100% or 0%.
Trouble: The loop for dimming works only when I release the button..
If anyone can point me in the right direction.....
if (digitalRead(dimButtonPin) == HIGH) {
delay(100);
int startTime = millis();
while (digitalRead(dimButtonPin) == HIGH) delay(50);
int endTime = millis();
if ((endTime - startTime) > 2000) {
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >=255) {
fadeAmount = -fadeAmount;
}
Serial.println(brightness);
BLUEdutyCycle = brightness;
REDdutyCycle = brightness;
}
}
if (digitalRead(dimButtonPin) == HIGH) {
delay(100);
int startTime = millis();
while (digitalRead(dimButtonPin) == HIGH) delay(50);
int endTime = millis();
if ((endTime - startTime) < 1500) {
if (OnOff_Status == 0) {
OnOff_Status = 100;
Serial.println("100%");
BLUEdutyCycle = 255;
REDdutyCycle = 255;
}
else if ((OnOff_Status == 100)) {
OnOff_Status = 0;
Serial.println("0%");
BLUEdutyCycle = 0;
REDdutyCycle = 0;
}
}
}
you want to exit this while loop if the 2s have elapsed, even if the button is still pressed ➜ so add a condition to break the while based on millis() - startTime
if you don't want to build complexity into the while condition, you could consider something as simple as
const unsigned long waitTime = 2000ul;
unsigned long startTime = millis();
unsigned long duration=0;
while (true) {
duration = millis() - startTime;
if (digitalRead(dimButtonPin) == LOW) break; // exit the while loop upon release
if (duration >= waitTime) break; // exit the while loop if there is a long press
}
if (duration >= waitTime) { // if we exited the loop upon long press
...
}
make sure you use the right type for millis (unsigned long not int)
of course this is a blocking approach, so your code won't do anything else during that time. if you need to do other stuff, consider a state machine approach and using millis
Hello all, as promised I'll keep you informed about the progress..... and after many hours it's working like I want.
Here is also the code, maybe you have a advice or tip.
Thank you both for helping me.
Grtz; Cees
static uint8_t touchPin = 27;
static uint8_t ledPin = 33;
// Fade parameters
int brightness = 0;
int fadeAmount = 5;
bool buttonState = LOW;
bool lastButtonState = LOW;
unsigned long buttonPressTime = 0;
void fadeLed()
{
while (buttonState == HIGH) {
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >=255) {
fadeAmount = -fadeAmount;
}
analogWrite(ledPin, brightness);
delay(50);
Serial.print("Dimming: ");
Serial.println(brightness);
buttonState = digitalRead(touchPin);
}
}
void setup()
{
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
pinMode(touchPin, INPUT_PULLDOWN);
}
void loop()
{
// Read the state of the pushbutton
buttonState = digitalRead(touchPin);
// Toggle LED if button is pressed and released within 1500ms
if (buttonState == HIGH && lastButtonState == LOW) {
buttonPressTime = millis();
} else if (buttonState == LOW && lastButtonState == HIGH) {
if (millis() - buttonPressTime < 1500) {
if (brightness == 0) {
brightness = 255;
} else {
brightness = 0;
}
analogWrite(ledPin, brightness);
Serial.print("Switch: ");
Serial.println(brightness);
}
}
// Start fading LED in and out when button is pressed
if (millis() - buttonPressTime > 1500) {
if (buttonState == HIGH) {
fadeLed();
}
}
lastButtonState = buttonState;
}