Yes I understand the code i write it, the only problem is how to combine dimming led and turning led on/off.
I tried more options for combining but it isnt working i dont know how to do it, I think I need time condition, I just don't know how to do it.
The code from post#9 works very well on an Uno.
Assuming Arduino Uno, because you didn't tell us which Arduino you're using.
A short press turns the LED on/off, and a long press dims the led.
A second long press dims the LED the other way.
Fully tested, so check your build if it doesn't work.
Leo..
I also wrote once a millis() timed version that dims 32 PCA9685 channels with a lookup table for equal brightness steps, which you probably can find on this site if you do a search.
But millis() timing seems too complicated for a beginner with these simple requirements.
Leo..
In the sim, did you press/hold the button long enough.
Brightness in the sim is a bit hard to see, but it works.
Where did that board come from (link).
The silkscreen says that the three LEDs are connected to D4, D5, D6.
If so, then the yellow LED should dim.
In the code from post#9 you can change ledPin from 5 to 6, so the green LED dims.
I see that the two buttons are labeled as D2 and D3, so D2 should work.
Leo..
It's great to see your interest in modifying your program to control a single LED with just one button. I'll be happy to help you with that. Here's an updated code that combines the functionality of both buttons into one:
const int button = 2;
const int led = 6;
int val1;
int inc = 1; // Variable to track whether the LED should be increasing or decreasing in brightness
void setup() {
pinMode(button, INPUT_PULLUP);
pinMode(led, OUTPUT);
}
void loop() {
val1 = digitalRead(button);
if (val1 == LOW) { // Button is pressed
if (inc == 1) {
analogWrite(led, 255); // Turn the LED on at full brightness
inc = -1; // Set the direction to decrease brightness
} else {
analogWrite(led, 0); // Turn the LED off
inc = 1; // Set the direction to increase brightness
}
delay(50); // Small delay to debounce the button
}
if (inc == -1) {
int brightness = analogRead(led); // Read the current LED brightness
brightness -= 5; // Decrease the brightness gradually
if (brightness < 0) {
brightness = 0; // Ensure the brightness does not go below 0
inc = 1; // Set the direction back to increase brightness
}
analogWrite(led, brightness); // Update the LED brightness
delay(20); // Delay between brightness changes for smoother dimming effect
}
}
In this updated code, we use the analogWrite function to control the LED brightness instead of turning it on/off directly. When the button is pressed, it checks the current direction of brightness (inc variable) and either turns the LED on at full brightness or turns it off, then changes the direction. By holding the button, the LED gradually decreases in brightness until it reaches 0, at which point it changes the direction back to increasing.
Feel free to adjust the delay values and brightness changes to suit your desired effect.
I hope this will help you.
Thanks for your code, but it doesnt works right, when i press the button led turns on and if I press it again it turns off but not everytime.
If i hold down the button, the led is blinking, if I release the button led goes off.
Did you find the solution already to trigger two ( or more ) actions with one button?
If not, search the forum with the following text : "button pressed more than 2 seconds do somethingelse". Two replies down you will find code from Maercello.Romani. Very very well done!!
I made domotica systems on Mega with this code as the basis, I use longpress to start dimming. If the LED is at max, the increase/decrease direction gets reversed. The shortkeypress function is of course for switching ON and OFF. If the level of the LED at switch OFF is kept in a variable, the LED can be switched on again with the same value.
As I understood your request, this should help.