There are a few aspects of having fun with switches that we did not cover during our lecture. For those with out experience working with switches, may not be fun at all if your switches act unpredictably or do not work at all.
Concepts:
1.Pull up or pull down resistor: click here to get an explanation
2. De-bouncing
Arduino, like other Microcontrolers, need to sense a definite high or low in their digital inputs. Both pulling up or down (in the circuit)) and de-bouncing (in the code) are ways to eliminate a false or fluctuating reading from a sensor (buttons been the simplest ones) connected to a digital input.
1. Pull up or pull down: (in the circuit) you place a 10k resistor in between your button and 5v.
With Pull down you place a 10k resistor in between your button and Gnd instead. You choose, just make sure that is either one or the other.
Here is what your code and circuit would look like in each case:
a)Pull up
Notice in the circuit:
1.One of the pins of the button is connected to the yellow wire and a resistor.
2.The yellow wire goes to a digital in on the Arduino board (not shown here)
3.The resistor is connected to red wire that plugs in the 5v (high) pin on the Arduino
4. The other pin of the button is connected to the black wire which plugs in the Gnd pin on the Arduino
:::circuit:::
:::Code:::
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { //
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
}
}
a)Pull down
Notice in the circuit:
1.One of the pins of the button is connected to the yellow wire and a resistor.
2.The yellow wire goes to a digital in on the Arduino board (not shown here)
3.The resistor is connected to black wire that plugs in the Gnd pin (low) on the Arduino
4. The red wire plugs in the 5v pin on the Arduino
:::Code:::
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == LOW) { //
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
}
}
2. De-bouncing: ( in your code) In order to make sure that your button has actually been pressed, you declare variables in your code that are going to store and old (previous) and a new (current) value of the button reading. These two variables are then compared. There is a given time that you write into the code so the Arduino waits in between readings. In this example of the de-bouncing code, if the Arduino senses a fluctuation in the digital in pin (connected to your button) that time gets increased.
Here is the code form the Arduino site. This code also makes your push button behave like a switch by holding its state even when it is not been pressed.
:::Code:::
int inPin = 7; // the number of the input pin
int outPin = 13; // the number of the output pin
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
void setup()
{
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
}
void loop()
{
reading = digitalRead(inPin);
// if we just pressed the button (i.e. the input went from LOW to HIGH),
// and we've waited long enough since the last press to ignore any noise...
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
// ... invert the output
if (state == HIGH)
state = LOW;
else
state = HIGH;
// ... and remember when the last button press was
time = millis();
}
digitalWrite(outPin, state);
previous = reading;
}
Comments