Newbie question- Getting Started with Arduino Ex05


I managed to fade an LED with a push button easy enough by adding a reference to the button to the Ex 04 fade. Wired the same way, Ex 05 did not work. What do you think I did wrong?

Fade an LED in and out with button (first attempt)
(Ex 04 with pushbutton added)


#define LED 9  
#define BUTTON 7 
int i = 0;      

void setup()  {
  pinMode(LED, OUTPUT); 
  pinMode(BUTTON, INPUT);  

void loop() {
  for (i = 0; i < 255; i++) {  
  analogWrite(LED, i);         
  delay(50);    }
  for (i = 255; i > 0; i--) {  
  analogWrite(LED, i);           

And the Fade LED with a pushbutton and stay on (Ex 05):

//  Example 05: Turn on LED when the button is pressed 
//  and keep it on after it is released
//  including simple de-bouncing.
//  If the button is held, brightness changes. 
// Copy and paste this example into an empty Arduino sketch

#define LED 9     // the pin for the LED 
#define BUTTON 7  // input pin of the pushbutton

int val = 0;      // stores the state of the input pin

int old_val = 0;  // stores the previous value of "val" 
int state = 0;    // 0 = LED off while 1 = LED on

int brightness = 128;        // Stores the brightness value
unsigned long startTime = 0; // when did we begin pressing?

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 
                             // yum, fresh 

  // check if there was a transition 
  if ((val == HIGH) && (old_val == LOW)) {
    state = 1 - state; // change the state from off to on
                       // or vice-versa

    startTime = millis(); // millis() is the Arduino clock
                          // it returns how many milliseconds
                          // have passed since the board has
                          // been reset.

    // (this line remembers when the button
    // was last pressed)

// check whether the button is being held down 
  if ((val == HIGH) && (old_val == HIGH)) {

    // If the button is held for more than 500ms.
    if (state == 1 && (millis() - startTime) > 500) {

      brightness++; // increment brightness by 1
      delay(10);    // delay to avoid brightness going
                    // up too fast

      if (brightness > 255) { // 255 is the max brightness

        brightness = 0; // if we go over 255
                        // let's go back to 0

  old_val = val; // val is now old, let's store it 

  if (state == 1) {      
    analogWrite(LED, brightness); // turn LED ON at the                                               // current brightness level
  } else { 
    analogWrite(LED, 0); // turn LED OFF 

Ex04? Ex05? Are these exercises? From what source?

What do you think I did wrong?

What you didn't do was explain what happened that you didn't expect, or what failed to happen that you did expect.

Sorry for not being clear. The exercises are out of "Getting Started with Arduino" (Massimo Banzi). I expected the LED to go on, stay on and the brightness level to be determined by the length of of time the button was pushed. I believe that it will have to be wired differently to work.

The button should be wired the same in both exercises. As near as I can tell, the code you posted is the same as that in the book. And, your expectation is correct, too.

Since the code is not doing what you want, now is a great time to learn about debugging.

Add this to setup:


Then, in loop, add Serial.print and/or Serial.println statements to print out when the button is pressed, and whenit is being held down. Print out the value in brightness. Look at this link, to learn about the Serial.print and Serial.println functions:

Upload the modified sketch, and use the last button on the right to open the Serial Monitor. Verify that the baud rate (in the bottom, right corner) is set to 9600, too.

You should see your print and println output. That should give you some clues as to what is happening.

If you can't determine what is wrong (or if you do), post the modified code, and sample output, for more help.

Well, I got it to work. It probably was the wiring that was wrong to begin with, though that was a week ago. I had treated ground as common for the button and for the LED.

Thank you for the suggestions Paul S., I now add serial prints to everything. R

might want to flag those with a comment, i use


after serial prints

as you add Serial.print(ln) scattered thought out your code its nice to be able to look for // DEBUG flags when your going nuts trying to find out why every print is prefixed with 17 or whatever