ClickButton icon indicating copy to clipboard operation
ClickButton copied to clipboard

Hold button down to increase a varible

Open j209177 opened this issue 3 years ago • 1 comments

Hi, I am using this library for a flashlight button and was wondering if you would be so gracious as to guide me in figuring out how to hold the button down to ramp up the PWM signal to the LED. Without letting go of the button.

Below is my attempt in coding my desired outcome.

for reference in the code, antiBlueSpotsRamp is to see if the flashlight is unlocked.

''' if(button1.clicks == -1 and antiBlueSpotsRamp == 1){ brightness = brightness + brightnessIncrease; '''

It would mean more than you know if you were able to help me in figuring this out. Days have been wasted searching on how to achieve this ramping outcome.

j209177 avatar Feb 24 '22 15:02 j209177

something like this

const int maxBrightness = 255;          // Maximum brightness value
const int brightnessIncrease = 5;       // Amount to increase brightness each step
const unsigned long rampInterval = 100; // Interval between brightness increases in milliseconds
unsigned long lastRampTime = 0;         // Timestamp of the last brightness increase
bool isChanging = false;                // Flag indicating whether the brightness is currently changing

void loop() {
    // Check if the button is held down and the flashlight is unlocked

    if(button1.clicks == -1  && antiBlueSpotsRamp == 1) {
      isChanging = true;
    }


    if (isChanging && digitalRead(/*ButtonPin*/)) {
        // Check if it's time to increase the brightness
        if (millis() - lastRampTime >= rampInterval) {
            lastRampTime = millis(); // Update the timestamp

            // Increase the brightness
            brightness += brightnessIncrease;
            if (brightness > maxBrightness) {
                brightness = maxBrightness; // Cap the brightness at the maximum value
            }

        }
    } else {
        isChanging = false;
    }
}

DCRalph avatar Sep 15 '24 14:09 DCRalph