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);
}
}
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.
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.
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.