2012-03-15 02:03:15 +01:00
|
|
|
/*
|
2017-07-15 00:30:34 +02:00
|
|
|
Input Pull-up Serial
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-14 21:34:00 +02:00
|
|
|
This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a digital
|
|
|
|
input on pin 2 and prints the results to the Serial Monitor.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
The circuit:
|
2017-07-12 22:18:23 +02:00
|
|
|
- momentary switch attached from pin 2 to ground
|
|
|
|
- built-in LED on pin 13
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-15 00:30:34 +02:00
|
|
|
Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal
|
2017-07-14 21:34:00 +02:00
|
|
|
20K-ohm resistor is pulled to 5V. This configuration causes the input to read
|
|
|
|
HIGH when the switch is open, and LOW when it is closed.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-12 22:18:23 +02:00
|
|
|
created 14 Mar 2012
|
2017-07-15 00:30:34 +02:00
|
|
|
by Scott Fitzgerald
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-12 22:18:23 +02:00
|
|
|
This example code is in the public domain.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-12 22:18:23 +02:00
|
|
|
http://www.arduino.cc/en/Tutorial/InputPullupSerial
|
2017-07-15 00:35:58 +02:00
|
|
|
*/
|
2012-03-15 02:03:15 +01:00
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
void setup() {
|
2012-03-15 02:03:15 +01:00
|
|
|
//start serial connection
|
|
|
|
Serial.begin(9600);
|
2017-07-12 19:58:31 +02:00
|
|
|
//configure pin 2 as an input and enable the internal pull-up resistor
|
2012-03-15 04:11:35 +01:00
|
|
|
pinMode(2, INPUT_PULLUP);
|
2013-10-21 09:58:40 +02:00
|
|
|
pinMode(13, OUTPUT);
|
2012-03-15 02:03:15 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-10-21 09:58:40 +02:00
|
|
|
void loop() {
|
2012-03-15 02:03:15 +01:00
|
|
|
//read the pushbutton value into a variable
|
|
|
|
int sensorVal = digitalRead(2);
|
|
|
|
//print out the value of the pushbutton
|
|
|
|
Serial.println(sensorVal);
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2017-07-14 21:34:00 +02:00
|
|
|
// Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
|
|
|
|
// HIGH when it's open, and LOW when it's pressed. Turn on pin 13 when the
|
2012-03-15 04:11:35 +01:00
|
|
|
// button's pressed, and off when it's not:
|
|
|
|
if (sensorVal == HIGH) {
|
|
|
|
digitalWrite(13, LOW);
|
2015-07-06 15:18:33 +02:00
|
|
|
} else {
|
2012-03-15 04:11:35 +01:00
|
|
|
digitalWrite(13, HIGH);
|
|
|
|
}
|
2012-03-15 02:03:15 +01:00
|
|
|
}
|