Need help with loop (pushbutton dimming)

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;
      }
    } 
  }

that's what you are asking for

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

Thank you for helping, I'm going back to VSC and try some more.. :+1:

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

For extra information and examples look at

Again, thank U so much.
Never to old to keep on learning (58) :wink:

looks this over

const byte PinLed = LED_BUILTIN;
const byte PinBut = A1;
byte butState;

unsigned long msecPeriod;
unsigned long msecBut;

void
loop (void)
{
    unsigned long msec = millis ();
    if (msecPeriod && msec - msecBut >= 2000)  {
        msecBut = msec;
        digitalWrite (PinLed, ! digitalRead (PinLed));
    }

    byte but = digitalRead (PinBut);
    if (butState != but)  {
        butState = but;
        delay (20);         // debounce

        if (LOW == but)  {
            msecBut    = msec;
            msecPeriod = 2000;
            digitalWrite (PinLed, ! digitalRead (PinLed));
        }
        else
            msecPeriod = 0;
    }
}

void
setup (void)
{
    Serial.begin (9600);

    pinMode (PinLed, OUTPUT);
    pinMode (PinBut, INPUT_PULLUP);
    butState = digitalRead (PinBut);
}

Thanks for the help, I'm gonna try a a little bit now.
Here where I live is't 20:00 now, so tomorrow new day, new chance.

I keep you all informed about the process.

many hours of hacking around left in the day :slight_smile:
...

Hello all, as promised I'll keep you informed about the progress..... and after many hours it's working like I want. :slight_smile:
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;  
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.