Help get this topic noticed by sharing it on Twitter, Facebook, or email.
I’m confused.

Getting Started With Arduino Second Edition- Problem with Example Code

I started reading through Getting Started With Arduino Second Edition just earlier yesterday and thus far everything has made sense and although I have run into a few problems I can usually figure them out on my own- but I am having a really hard time with Example 5 on pg. 59-60.

The sketch is, to my understanding, supposed to:
1.) Turn an LED on or off using a push button during a press and release action.
2.) Change the brightness of the LED using the push button during a press and hold action.

I directly copied and pasted the sketch from a digital copy of the book into the arduino environment and it only kind of worked. This is what I experienced.
1.) The LED will get brighter and dim in a repeated loop while the LED is on.
2.) I can turn the LED on/off with the push button using a press and release action.
3.) The LED does not change brightness when I use the press and hold action.
4.) If I press and hold the LED while the light is on the LED will stop changing brightness and remain on and at that brightness until I release the button.
5.) If I press and hold the LED while the light is off the LED will remain unlit until I release the button.

I am not sure what I could be doing wrong since I copied the sketch exactly. Any ideas?

This is exactly what I have entered if that helps at all:

const int LED = 9; // the pin for the LED
const int 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)
delay(10);
}
// 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
}
}

The Arduino and Breadboard setup:








1 person has
this question
+1
Reply