Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hold button down to increase a varible #10

Open
j209177 opened this issue Feb 24, 2022 · 1 comment
Open

Hold button down to increase a varible #10

j209177 opened this issue Feb 24, 2022 · 1 comment

Comments

@j209177
Copy link

j209177 commented Feb 24, 2022

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.

@DCRalph
Copy link

DCRalph commented Sep 15, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants