I would like to set up the arduino to do the following.
Push and release button ---> delay ----> LED Light up ----> Fade to off ---> wait for button to be pushed again if pushed again repeat output function.
I know how to do the fading and have a button light up an LED.
But am struggling to link the 2 together.
Consider the wonderfulness of "subroutines" (or "functions"):
void waitForButtonPress()
{
// Your working button code.
}
void myDelay()
{
delay(mydelaytime);
}
void lightAndFade()
{
// Your working LED fading code
}
void loop()
{
waitForButton();
myDelay();
lightAndFade();
// loops and does it again...
}
It only needs to get complicated if you want to do things concurrently (like, oh, allow the button to be pushed during the fading for an early re-start.)
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 11;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
void waitForButtonPress()
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
void myDelay()
{
delay(100);
}
void lightAndFade()
{
// fade in from min to max in increments of 5 points:
for(int fadeValue = 255 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(0);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(100); // Your working LED fading code
}
void loop()
{
waitForButton();
myDelay();
lightAndFade();
}
waitForButton() was never defined. The arduino is only going to continuously run code if it is in the void loop(). However, you dont have any of the code you wrote out in the void loop. Therefore it wont work.
If you want the delay to be the amount of time the LED stays light before turning off it needs to be in the button loop. Also if you want the light to fade off it needs to be in there as well.
Try have the button loop as the main loop.
Like this:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 11;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
myDelay();
lightAndFade();
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
void myDelay()
{
delay(100);
}
void lightAndFade()
{
// fade in from min to max in increments of 5 points:
for(int fadeValue = 255 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(0);
}
// fade out from max to min in increments of 5 points:
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(100); // Your working LED fading code
}
}
You forgot the semicolon after the calls to the subroutine.
Just updated the code i post to add the missing {}. I would have tested it myself but i'm at work right now and don't have the IDE installed on this PC.