Need help with breadboarding "Example 05" from Banzi's Arduino book

i am brand new to Arduino. I'm going through the book "Getting Started with Arduino" and I can't get example 05 to work. I am a little annoyed with the book because it says to "simply build both circuits on different parts of the breadboard. I've tried that and when I press/hold the button on the breadboard, the LED flickers, and when I release the button, it keeps flickering for about a half second. A diagram for this example would have been nice :frowning:

Has anyone out there gotten this example to work?

A diagram for this example would have been nice

It uses two circuits previously used, so, why is another picture needed? There is nothing common between the two circuits, except the Arduino, hence the note to build in two separate areas.

Posting YOUR schematic, and your code, would be good ideas, eh?

I also have an issue with this Example 5-2 in Getting Started with Arduino.
Rather than creating a new topic (as there are already at least 2, including this one, open on the subject), I will continue with this one.
I have checked my whole sketch twice, and it seems to be correct. I have attached it to this post if anyone finds the time to review it.
I have also attached a photo of the wiring - each separate part of the system works correctly (the push button on one side, and the LED on the other), yet once I upload the sketch nothing happens when I press the push button.

May anyone guide me here, and point me to the error I made?

Thanks in advance!
Damien

PushToDim.ino (1.63 KB)

You may have got the switch rotated by 90 degrees, so it is always shorted.
Have you tried adding prints to your sketch (sorry, I'm posting from a phone, so I can't easily see your code)

if ((val = HIGH) && (old_val == HIGH)) {

= and == are not interchangeable.

= and == are not interchangeable.

Haha great catch, thanks, I'll try that tomorrow.

You may have got the switch rotated by 90 degrees

I will also try that - even though this part of the circuit worked correctly when I tested it to switch on/off a LED.
What is the "print" you write about?

Thanks Arrch and AWOL, the latest upload to the board worked. And I did not even have to use the print function to debug ;o)

I'm new and having trouble with this as well.
When I press and hold the button the LED stays on but doesn't dim. If I press again, it turns off.
I also note that the Arduino LED off of pin 13 is constantly on whether the button is pressed or not - is that supposed to happen?
I've reviewed my code and circuit but must be missing something.
Oh, and the LED setup called for a 270ohm resistor which I didn't have - so I used a 330. Maybe that has something to do with it?

Here's my code and the photo of my circuit. (WOW, how the heck did the photo flip? Sorry about that. Not sure what to do about it.)

If you can help enlighten me of my errors I'd greatly appreciate it.

// 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, the brightness changes.

const int LED = 9; // 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

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

// 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 (state == 1 && (millis() - startTime) > 500) { // if the button is held for more than 500 ms.
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) {
digitalWrite(LED, HIGH); //turn LED ON at the current brightness level
} else {
digitalWrite(LED, LOW); // turn LED off
}
}

When I press and hold the button the LED stays on but doesn't dim.

Judging by the code, it's supposed to get brighter actually, due to this, which increments the variable brightness:

brightness++;

..... but, that incremented value isn't being used. I think you're missing an analogWrite, something like this:

if (state == 1 && (millis() - startTime) > 500) {     // if the button is held for more than 500 ms.
    brightness++;                                         // increment brightness by 1
   analogWrite(LED, brightness); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< NEW
    delay(10);                                             // delay to avoid brightness going up too fast

LED off of pin 13 is constantly on

It's pretty normal

called for a 270ohm resistor which I didn't have - so I used a 330. Maybe that has something to do with it?

Nope, just makes the current a little less so it's a little dimmer.

EDIT: PLEASE use the # button above the :sweat_smile: to put your code in code tags. Select the part you want as code, then hit that #.

Code will then look like this

Fantastic! It's working! Thanks!
Strange that that line of code was not in the example in the book.
Also, thanks for the code formatting tip.

collerch:
Fantastic! It's working! Thanks!
Also, thanks for the code formatting tip.

You're welcome: glad to help.

Strange that that line of code was not in the example in the book.

Indeed, but there you go.