Coding Advice!

Hello,

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.

Cheers!

Victor

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.)

what am i doing wrong here?

This code doesnt work :frowning:

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();
}
void loop()
{
  waitForButton();
}

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.

The error im getting is

" In function 'void setup()':
error: expected initializer before 'buttonState "

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.

I still get the same error message! :frowning:

you are missing the {curly brackets} that close off void loops

void ()
{
//your code
}

OMG this is so frustrating.

Still not letting me do it :confused:

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.

Just updated the code i post to add the missing {}.

The button State function is the problem preventing me to do it!

buttonState = digitalRead(buttonPin);

In function 'void loop()':
error: 'buttonState' was not declared in this scope

You need to define the button state in setup. add this:

int buttonState = 0

AHAAA! Found it !! Working!!! woop! Thanks for all you help!

Ahh wait.. There is no delay between button pressing and the LED lighting up ... :confused: oh well i will test this all in the morning