I need to make for school a project. That when i hold a button it fades. But when you let go the button it needs to stop immediately. But when i you hold the button again it needs to start again with fading but the brightness needs to start from 0. I have a program but when i press again it just starts from the previous brightness. Pls help. This is my code:
int led = 9;
int brightness = 5;
int fadeAmount = 5;
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop() {
int val = digitalRead(2);
Serial.println(val);
if (val==HIGH){
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount; }
delay(100);
}
else{
digitalWrite(9, LOW);
}
}
1 Answer 1
The main problem is you have to reset brightness to 0 when the switch is LOW. Btw, I did not test the sketch, only compiled it.
int led = 9;
int brightness = 5;
int fadeAmount = 5;
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop() {
int val = digitalRead(2);
Serial.println(val);
if (val==HIGH){
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount; }
delay(100);
}
else{
digitalWrite(9, LOW);
brightness = 0; // Restart with brightness 0
}
}
Some other improvements result in:
#define LED_PIN 9
#define DELAY_TIME 100
int brightness = 5;
int fadeAmount = 5;
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
int switchButton = digitalRead(2);
Serial.println(switchButton);
if (switchButton == HIGH) {
analogWrite(LED_PIN, brightness);
brightness += fadeAmount;
if ((brightness <= 0) || (brightness >= 255)) {
fadeAmount = -fadeAmount;
}
delay(DELAY_TIME );
}
else {
digitalWrite(LED_PIN, LOW);
brightness = 0; // Restart with brightness 0
}
}
Explanation
- Always indent your code (so that { and } are clear, notice each new { increases the indentation by two spaces, each } decreases the indentation by two spaces.
- For values that do not change use a #define. This prevents (in this case) a few bytes not be stored in valuable memory space. Especially if you would use arrays this is important.
- You used 9 in
digitalWrite(9, LOW);
which was probably a mistake, always use defines wherever possible. I also would use#define DELAY_TIME 100
instead of the hardcoded 100 value. - I addes some spaces before/after values (like
switchButton == HIGH
). It doesn't matter if you do or not, but be consequent. - Use good variable names (
switchButton
is more clear thanval
. - Use
brightness += fadeAmount;
instead ofbrightness = brightness + fadeAmount;
; this is the normal way of increasing (or decreasing/multiplying/whatever) the value of a variable. - Use ( and ) in non trivial if statements like
if ((brightness <= 0) || (brightness >= 255))
, this could save you hours of searching in case you ever make a mistake (in this case there is no functional change).
Good luck with your project.
-
Wow, a bounty of good coding advice! (Voted)Duncan C– Duncan C2019年05月03日 00:03:37 +00:00Commented May 3, 2019 at 0:03