Sketch 5.3 from Arduino Cookbook

Simple circuit, 5VDC pushbutton w/ external pulldown resistor on D2 input, LED on D13 output. “debounce” is the function and boolean tells it to return 0 or 1. Where did pin come from? Is that a typo? What pin? I assume it is the inputPin? Corrected the typos in loop(), put scope on pins. I also breadboarded an external LED to pin 13 in addition to the button and it’s external pulldown resistor on pin 2. Digital inputPin 2 goes HI and LOW when button pushed. Digital output ledPin 13 stays HI. I can trace pin states but no way to see what processor is doing with the code without adding a lot of code to monitor the code and serial.print outputs to the serial monitor that I know of. Code compiles and loads without errors, but LED does not change state.

 * Reliability Detecting Closing of Switch
 * debounce sketch

const int inputPin=2;
const int ledPin=13;
const int debounceDelay=10;

boolean debounce(int pin)  // please explain the int and pin here?
  boolean state;
  boolean previousState;

  for(int counter=0; counter<debounceDelay;counter++)
    if(state !=previousState)
      return true;
      return false;
  return state;

void setup() 

void loop() 
//  if(debounce(inPin))  this is a typo in the text, will not compile like this
  if(debounce(inputPin))//corrected pin assignment
//    digitalWrite(outPin,HIGH);  also a typo

pin is the argument passed to this function:

boolean debounce(int pin)

Whenever you call:


Then it uses inPin for pin

and if you called


then it would use the number 5 for pin in that function.

Go find a good C++ tutorial and study up about functions.

Code compiles and loads without errors, but LED does not change state.

There's no code to tell it to change state. There is a line that tell it to write that pin HIGH if the button is pressed, but there is no code anywhere that ever tells it to write it back to LOW. So it shouldn't ever change.

That was it, I think?


else digitalWrite(ledPin,LOW);

after the if statement and it works now, or at least the LED goes on and off. I can only assume the debounce(int pin) is working since all else seems to work. What puzzles me is that the LED is ON when idle and OFF when button pushed. I think it should be the other way, OFF when idle and ON when pushed.

Thanks for the input, Sam

How is it wired?

Normally a button would be wired between the pin and ground so you can use INPUT_PULLUP instead of an external resistor. Then a pushed button reads LOW and a released button reads HIGH.

For LEDs it just matters whether you put them between the 5V and the pin or between the pin and ground. If they’re between 5V and the pin then writing LOW turns them on and HIGH turns them off.

You should disabuse yourself now of any notion that HIGH somehow means ON and LOW somehow means OFF. Most of the time it’s just the opposite. Think of HIGH as 5V and LOW as 0V.



As per the exercise drawing the NO switch is between pin2 and gnd. From the pin2 side of the switch is a 12K resistor to 5V. Yes I would normally use an internal pullup, but the exercise uses an external one. Not sure why the exercise is using the bool debounce, I usually use a simple delay(X) instead. 10-50 millis works fine. But it is a good mental workout and thought provoking.

OK I double checked the wiring and found the 5V and gnd connections were swapped. Corrected and works like it should now. Mea Culpa

Thanks Guys

FYI I have found some thought provoking and useful ideas in this book, but many typos in the code (such as the lack of code line to turn LED off). Hopefully fixed in the new version coming out. Wonderful PDF file named "Arduino Book" was on a UNIROI cd that came with some hardware I bought from them. LOL they ripped off the "Arduino Cookbook" which was a $40 US book until the new version was announced which is more expensive.

For your new hobby:

Arduino links.

For many Arduino PDF document links:
Google >>>- - - - > arduino filetype: pdf

Google >>>- - - - > C++ filetype: pdf

Watch these:

Some things to read:


@larryd, TNX for the reading list Larry, K++

Great, yes thx Larry