Hi everyone. Is my first time that I ask something in this forum. I would like to know if someone have accomplished the project that are in this link [Using One Pushbutton as Toggle Switch and dimmer].
The code of the post 2 in lhe link works fine but what I want is when the led are dimming we can stop in the point of dimming. Someone can help me with this?
I did something similar a few years back for all of my kitchen lighting. I used a UNO and have 6 channels with 6 buttons. I did my debounce and checking as an array and checked all 6 switches at one time.
I could not follow the link given very well but I used used a PWM pin. This will make it a bit easier. You will need a few flags, on/off, up/down and dim value.
When the button is pressed you look at the on/off flag, complement it then use that as the on off state of the LED (output). ie on becomes off and off becomes on.
If it was off you copy the dim value to the PWM register this will cause the output to turn on.
If it was on you copy the PWM register to the dim value and write zero to the PWM register, this turns the output off.
The dim value has the pwm value for the output. When the button is held for a period of time you complement the direction flag and either increase or decrease the PWM value and copy that to the PWM register. After a period of time if it is still pressed you repeat the up down until the limit is reached.
This is a rough outline, you will have to work with it and your code must be non blocking so do not use the delah(). Do it is steps, start and get the on/off working first, use something mid point such as 125 as the pwm value. When the On/Off works then work on the dimming value. In the first part it will be off or partly on. I did my increments in 5 but you can determine that when you get it working. Good luck.