hello all, i just made single push button dimmer project, its working perfect but problem is there is no stop and start point between led full brightened and starting dim point, i want extra push function to start dimming. please can you modify this code ? thanks in advance. here is the code;
const int GREEN = 4;
const byte button3 = 3;
int button3St;
int count = 0;
int dimmer = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(GREEN, OUTPUT);
pinMode(button3,INPUT_PULLUP);
}
void loop() {
button3St = digitalRead(button3);
if (button3St == LOW){
if (count == 0){
dimmer = dimmer +5;
delay(50);
if (dimmer>255){
count = 1;
}
}
else if (count == 1){
dimmer = dimmer -5;
delay(50);
if (dimmer<0){
count = 0;
}
}
}
Serial.println(dimmer);
Serial.println(count);
analogWrite(GREEN, dimmer);
}
First, when dimmer goes over 255, you shoukd restrain it to 255.
Then, instead of making count equal to 1, meaning that the other section is executed, make it 2, to mean wait here at maximum brightness until you see the other button.
On the other side, dimming to zero, again restrain dimmer to be exactly zero, and set count to 3, to mean wait here at minimum brightness until you see the other button.
When you first press the button the light becomes brighter until you release the button.
When you next press the button the light becomes dimmer until you release the button. Then, on the next press, it goes back to getting brighter.
To do that you need to note that the button has been released. Each time through the loop record the 'previousButton3St'. Switch direction when the 'button3St' is LOW and previousButton3St is HIGH (new press).
this is single push button dimmer. it keeps dimming and brightening led while pushing button or hold . i want when it reached @ count == 1, then stop and start again when i hit another push.
keep pushing or hold button till it reaches 255 and count 1. and when it count 1 it start dimming to 0 with same method. and in other words I want pause point where count ==1 is.
@humamb how are your switches wired? Does pressing a switch give the reading of HIGH or LOW?
Do you mean to have a second pushbutton to make it go? If the same button, you need to handle every time it goes from not pressed to pressed.
See the IDE for the state change detection examlpe.
This
while (digitalRead(goButton)) {}
will hang right there until a button goes LOW, which woukd be pressed if you have wired them with a pull-up resistor or used INPUT_PULLUP in you pinMode() call, and have the pushbutton wired between the input pin and ground.
Add the ! logical negation operator if your switch reads HIGH when you press it.
much thanks mate. i wired pull_up resistor and i,m getting low when i press button. i,m operating same button for increasing and decreasing. you got me right and i really appreciate your help and thanks again for your guide lines.
OK, while getting brighter, the button down makes it increase in brightness to max.
Then getting dimmer.
While getting dimmer, the button down makes it decrease in brightness to off.
Now you have said you want it to not immediately go dimming after you have reached maximum.
And not immediately go brighter after you have reached minimum.
Sorry, just want to be sure you are only using one button, I got off track before. So one button that has to be released and then it will do the other thing when next pressed?
That is how I would want a wall-switch dimming button to work. To make it ON or OFF unambiguously and easily.
Here is your prettified code in the wokwi simulator, where I added a servo that waves because the simulator doesn't do dim LEDs very well:
I have a bus ride before I can get to the big screen, don't hold your breath.
Do you have expansion plans?
The solutions from here can be easy and quick, but a waste if you have larger plans. Or a but more involved, but laying a foundation for all kindsa tricks you may think of and want to accomplish.
OK, I placed a variation of @alto777's idea in #12 above into the sketch viz:
// https://forum.arduino.cc/t/about-adding-stop-and-start-point/1037964
# include <Servo.h>
const int GREEN = 4;
const byte button3 = 3;
const int servoPin = 5;
Servo myServo;
int button3St;
int count = 0;
int dimmer = 0;
void setup() {
Serial.begin(9600);
pinMode(GREEN, OUTPUT);
pinMode(button3, INPUT_PULLUP);
myServo.attach(servoPin);
}
void loop() {
button3St = digitalRead(button3);
if (button3St == LOW) {
if (count == 0) {
dimmer = dimmer + 5;
delay(50);
if (dimmer > 255) {
count = 1;
// right here we are at the maximum, so hang until he is def off the button
dimmer = 255; // clamp at the maximum
analogWrite(GREEN, 255);
do delay(50); while (!digitalRead(button3));
}
}
else if (count == 1) {
dimmer = dimmer - 5;
delay(50);
if (dimmer < 0) {
count = 0;
// right here we are at the minimum, so hang until he is def off the button
// code it like the max case above!
}
}
}
Serial.println(dimmer);
Serial.println(count);
analogWrite(GREEN, dimmer);
int servoPosition = map(dimmer, 0, 255, 0, 180);
myServo.write(servoPosition);
}
I only "fixed" the getting brighter part, read it and see if it makes sense enough that you can figure what must be added to the getting dimmer part.
I call it a hack because… that's what it is. It messes up a few of the formerly nice things about how your sketch flowed, but it will get the desired functionality. At the cost of future flexibility.
A finite state machine, which your origianl code was quite close enough to in the absract, would make this a nice little sketch ready for the future.