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;
  previousState=digitalRead(pin);

  for(int counter=0; counter<debounceDelay;counter++)
  {
    delay(1);
    state=digitalRead(pin);
    
    if(state !=previousState)
    {
      counter=0;
      previousState=state;
    }
    if(state==LOW)
      return true;
    else
      return false;
  }
  return state;
}

void setup() 
{
  pinMode(inputPin,INPUT);
  pinMode(ledPin,OUTPUT);
}

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
    digitalWrite(ledPin,HIGH);
  }
}

pin is the argument passed to this function:

boolean debounce(int pin)

Whenever you call:

if(debounce(inputPin))

Then it uses inPin for pin

and if you called

if(debounce(5))

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?

added

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.

FYI

2017-08-05_20-33-24.jpg

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
Or
https://www.google.ca/search?q=arduino+filetype%3A+pdf&rlz=1C9BKJA_enCA739CA739&oq=arduino+filetype%3A+pdf&aqs=chrome..69i57j69i65.1385j0j7&hl=en-US&sourceid=chrome-mobile&ie=UTF-8

Google >>>- - - - > C++ filetype: pdf
Or
https://www.google.ca/search?q=c%2B%2B+filetype%3A+pdf&rlz=1C9BKJA_enCA739CA739&oq=c%2B%2B+filetype%3A+pdf&aqs=chrome..69i57.22790j0j7&hl=en-US&sourceid=chrome-mobile&ie=UTF-8

Watch these:

Some things to read:

https://learn.adafruit.com/category/learn-arduino

https://learn.sparkfun.com/tutorials/how-to-read-a-schematic

https://www.arduino.cc/en/Reference/HomePage

http://playground.arduino.cc/Code/AvoidDelay

http://www.gammon.com.au/forum/?id=12153

https://www.arduino.cc/en/Main/Products

http://arduino-info.wikispaces.com/

http://www.gammon.com.au/motors

http://www.gammon.com.au/forum/?id=11955

https://uca26c5a2635cfebdfe4db41f3e0.dl.dropboxusercontent.com/cd/0/get/ALt0BrI2xu9WMGWZRQ_9a5BR44TPGYBER106X3UCw8AQKQCa0NJu2aZlcheHqM7uHjvKDUJXxvCK6yL-Lx7WjPTdhXz5V2KWEaSePesSBlgR5fLJkr1slS4qLGpZ-1ypS92PxCLbEjVOZKbZvdR9pxcHoWzQSZIj6lelHfQ7UMNopt3SMXrQkKiekb3Vy6nEzGE/file?dl=1

.

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

Great, yes thx Larry