For an LED circuit, I would like code that will allow an LED to stay on at a continuous/persistent brightness, but when a pushbutton is pressed, to switch from persistent brightness mode, to "fade in and out like an apple computer" mode. For my first attempt at this, I took the on/off code from "Getting Started with Arduino" (pg 49) quoted below as "CODE A", and in lieu of "digitalWrite(LED, LOW);" following the "} else {" command, I added the "fade in like an apple computer code" ("Getting Started with Arduino" pg 57). The original "fade in and out like an apple computer" code is written below as "CODE B", and the resultant code after having combined "CODE A" with "CODE B" is written below as "CODE C".
My issue: "CODE C" running via Arduino Uno starts by fading the LED in and out like an apple computer. When I press the pushbutton, nothing happens when the LED is in the midst of its fade and does not switch to persistent brightness mode. However, at the VERY moment the fade of the LED reaches its lowest point, and if at that exact moment I press the pushbutton, then BAM, the LED switches to persistence mode. The problem: it is VERY difficult to hit the pushbutton, right at the very moment the LED is at the dead-center bottom of its fade-out. I've tried changing some "==" to "<" or ">", but without success. Any help would be greatly appreciated, and I apologize if there's a forehead-slap solution as this is my first attempt writing an unique code. Thank you in advance.
CODE A
// Example 03C: Turn on LED when the button is pressed and keep it on after is is released.
// including simple debouncing
//Now with another new and improved formula!!!
const int LED = 13; // the pin for the LED
const int BUTTON = 7; // the input pin where the pushbutton is connected
int val = 0; // val will be used to store the state of the input pin
int old_val = 0; // this variable stores the previous value of "val"
int state = 0; // 0 = LED off while 1 = LED on
void setup() {
pinMode (LED, OUTPUT); // tell Arduino LED is an output
pinMode (BUTTON, INPUT); //and BUTTON is an input
}
void loop() {
val = digitalRead(BUTTON); //read input value and store it
//check if there was a transition
if ((val == HIGH) && (old_val == LOW)) {
state = 1 - state;
delay(10);
}
old_val = val; // val is now old, let's store it
if (state == 1) {
digitalWrite(LED, HIGH); // turn LED ON
} else {
digitalWrite(LED, LOW);
}
}
CODE B
//Example 04: Fade an LED in and out like on a sleeping Apple computer
const int LED = 9; // the pin for the LED
int i = 0; // we'll use this to count up and down
void setup() {
pinMode(LED, OUTPUT); //tell Ardiuno LED is an output
}
void loop() {
for (i=0; i < 255; i++) { // loop from 0 to 254 (fade in)
analogWrite(LED, i); //set the LED brightness
delay(10); //Wait 10ms because analogWrite is instantaneous and we would not see a change
}
for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)
analogWrite(LED, i); // set the LED brightness
delay(10); // wait 10 ms
}
}
CODE C: Resultant code from combining A and B
// When button is pressed toggle between continuous brightness
// and fading in and out like an apple computer.
const int LED = 9; // the pin for the LED
const int BUTTON = 7; // the input pin where the pushbutton is connected
int i = 0; // we'll use this to count up and down
int val = 0; // val will be used to store the state of the input pin
int old_val = 0; // this variable stores the previous value of "val"
int state = 0; // 0 = LED off while 1 = LED on
void setup() {
pinMode (LED, OUTPUT); // tell Arduino LED is an output
pinMode (BUTTON, INPUT); //and BUTTON is an input
}
void loop() {
val = digitalRead(BUTTON); //read input value and store it
//check if there was a transition
if ((val == HIGH) && (old_val == LOW)) {
state = 1 - state;
delay(10);
}
old_val = val; // val is now old, let's store it
if (state == 1) {
digitalWrite(LED, HIGH); // turn LED ON
} else {
for (i=0; i < 255; i++) { // loop from 0 to 254 (fade in)
analogWrite(LED, i); //set the LED brightness
delay(10); //Wait 10ms because analogWrite is instantaneous and we would not see a change
}
for (i = 255; i > 0; i--) { // loop from 255 to 1 (fade out)
analogWrite(LED, i); // set the LED brightness
delay(10); // wait 10 ms
}
}
}