Hello!
I wrote a program in which I turn the led on and off on press with the first button, and with the second button Im dimming led on/off when im holding the button.
I'm interested in how to change the code so that instead of two buttons and two leds, I could control only one led which will be turning on/off on press and dimming on/off on hold with only one button.
Br!
const int button = 2;
const int led1 = 6;
int val1;
int val2;
int inc = "INCREASING";
const int led = 4;
const int button1 = 3;
int read;
int val3;
int oldval;
void setup(){
pinMode(button,INPUT_PULLUP);
pinMode(led1,OUTPUT);
pinMode(led,OUTPUT);
pinMode(button1,INPUT);
}
void loop(){
val1 = digitalRead(button);
if(val1==1){
if(inc == "INCREASING"){
val2 ++ ;
delay(20);
if(val2 > 250){
inc = "DECREASING";
}
}
else if (inc == "DECREASING"){
val2 -- ;
delay(20);
if(val2 < 1){
inc = "INCREASING";
}
}
}
analogWrite(led1, val2);
read = digitalRead(button1);
if (read == 1){
delay (20);
read = digitalRead(button1);
if (read == 0){
val3 = oldval + 1;
}
}
else{}
switch(val3){
case 1:
digitalWrite(led,1);
oldval = val3;
break;
case 2:
digitalWrite(led,0);
oldval = 0;
break;
default:
digitalWrite(led, LOW);
oldval = 0;
break;
}
}
I apologize if I wrote the instructions poorly, I am sending an additional description below
i want to use a single pushbutton to Turn off and on LED but also dimming or brightening the LED with the same Pushbutton. If the Pushbutton is pressed only once it acts as on/off switch and turns LED on if it is off in current state and vice versa. While the LED is turned on , if the button is pressed long it dims the LED light and next time the button is pressed long it brightens the led and vice-versa.
All help is greatly appreciated. Thanks in advance!
delete this sketch and write your own from scratch. you need to learn how to recognize long key press. if it is done then insert LED handling depends on key holding time.
everything works individually, I just have a problem with combining the two programs, I would like help for this, or a completely different written program.
const byte buttonPin = 2; // button between pin and ground, no resistor
const byte ledPin = 5;
bool ledOn, dimDown;
byte pwmValue = 100; // default mid brightness
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode (ledPin, OUTPUT);
}
void loop() {
if (!digitalRead(buttonPin)) delay(20); // debounce time
if (!digitalRead(buttonPin)) { // if still pressed
delay(500); // long press?
if (!digitalRead(buttonPin)) { // long press
while (!digitalRead(buttonPin)) {
if (dimDown) {
if (pwmValue > 10) pwmValue--;
}
else {
if (pwmValue < 255) pwmValue++;
}
analogWrite(ledPin, pwmValue);
delay(25); // time between dim steps (dim speed)
}
dimDown = !dimDown; // flip direction
} else { // short press
if (ledOn) { // if on, turn off
digitalWrite (ledPin, LOW);
ledOn = false; // remember
dimDown = false; // force dim up at next turn on
} else { // if off, set to previous brightness value
analogWrite (ledPin, pwmValue);
ledOn = true; // remember
}
}
}
}
considering that you want to alternately brighten or dim the LED, the code needs to recognize a button press, not simply that it is being pressed, and then for how long it is held down
recognizing a button press requires recognizing a change in state (which means maintaining the state in a variable) as well as debouncing to recognize a single change in state (a simple 20 msec delay should suffice)
when the button is pressed, a timestamp should be captured and a timer started.
if the button is released before the timer expires, the timer is disabled and a short button press is recognized, turning on or off the LED and the state of the LED tracked. for this press: ON, OFF
if the timer expires, a long press is recognized and the led brightness increased or decreased. the time period can presumably be made shorter and subsequent timeouts would continue to inc/decrease the brightness.
some state is needed that changes with each button press (not timeout). when OFF, to ON or INCREASING. when ON to OFF or DECREASING. when INCREASING or DECREASING, a short button press presumably goes to OFF and a long pressed alternates between IN/DECREASING
not so trivial. start with recognizing a button press then a long button press and then repeated timeouts while the button is held down
actions are probably simpler to recognize a short button press, just pressed (long) and then continued press (longer)
I tried various changes to the program, tried to understand it, how exactly it works, I also added some things, but it does not work, if anyone has any advice or ideas, I would be grateful.
If you a stuck on a problem start a new arduino project specifically to figure out the problem your having. In your case you need to figure out when a key press is above and below a certain threshold.
Above that threshold you do your dimming and below that threshold toggle the led.
Here is some pseudo code to get you started.
if (key is pressed) {
start = millis(); //start counter
while(key is pressed); //wait until key is released
if (millis()-start < threshold) //to get to this line the key must of been released
//toggle led code here //if elapsed time is below the min dim led time then toggle
else
//dim led code here //if elapsed time is above or equal to dim led time then dim the led
}