/*
Button
Turns on and off a light emitting diode(LED) connected to digital pin 13,
when pressing a pushbutton attached to pin 2.
The circuit:
- LED attached from pin 13 to ground through 220 ohm resistor
- pushbutton attached to pin 2 from +5V
- 10K resistor attached to pin 2 from ground
- Note: on most Arduinos there is already an LED on the board
attached to pin 13.
created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button
*/
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
Per theseposts and these guidelines, if you've got something to say about the example, its schematics, its explanation, or you have a better way of doing it, please share.
You have been asked by yourself to draw a connection diagram among DPIn-2 of UNO, Switch, Pull-down resistor, 5V, and GND. I think that you are missing two more items which are: a LED with a series resistor, which will turn On when the Switch is pressed and Serial Monitor to display this message: **Switch has been pressed". After that you have to create a sketch to check that the Switch is closed.
2. My Sketch to check that Switch has been pressed and then turn On LED1 and show this message on SM (Srial Monitor: Swicth has been pressed.
(1) Activate Serial Monitor at Bd = 9600. Serial.begin(9600);
(2) Set direction of DPin-2 as INPUT with external pull-down resistor and DPin-4 as OUTPUT.
pinMode(2, INPUT);
pinMode(4, OUTPUT);
(3) Check again and again that Switch has been pressed/closed by looking for HIGH at DPin-2. If Switch is closed, turn On LED1 and show message on SM.
int y = digitalRead(2);
if( y != HIGH)
{
Check again for closed condition of Switch //convert this Text Code into C++/Arduino Code
}
delay(200); //wait for the debouncing of Switch
digitalWrite(4, HIGH); //LED1 is ON
Serial.println("Switch has been pressed.")
3. Click on Arduino icon and check check that a window (called IDE = Integrated Development environment) has apperaed on the Desktop of the PC with the following two empty functions:
void setup()
{
}
void loop()
{
}
4. Place the codes of Step-2 within the appropriate function(s) of Step-3. 5. Verify/Compile to eliminate erros of the sketch. 6. Upload the sketch. 7. Open the SM at Bd (Baud rate) = 9600 (Fig-2). 8. Gently, just press/release the switch. 9. Observe that LED1 has turned On. 10. Check that Swicth has been pressed message has appeared on SM.
A small mismatch between the code and the diagrams is that the code calls out an external LED, while the circuit diagram uses the Uno's built-in LED (attached to 13, LED_BUILTIN, or PB5) instead of an external LED.
I don't like that the leading example is in terms of switches that are pulled low.
I could say honestly I don't like that LEDs are almost always wired to ground and illuminate with HIGH.
I don't like HIGH and LOW, but we are stuck with that, please don't spend time, anyone, 'splaining why it's a Good Thing.
So my point is that the earlier ppl can be turned on to the fact that sometime grounding a pin can make an LED light up, and sometimes pushing a button means the input reads HIGH and that…
… fixing this kind of upside down problem is always always going to be handy.
the better. Sorry, my grammar is out to lunch.
At an early point with the kinds of things noobs get up to with Arduino boards, some kind of minimal understanding of the real works to which we like to connect is essential.
Just a small bit of Boy Scout merit badge electronics.
Here's another button-testing sketch with some rate-limited scanning of all the (floating) inputs :
// report.ino -- A demo of millis-based periodic status reporting
// https://wokwi.com/projects/408753591671265281
//
// Every 1000ms, this program reads and reports the analog and digital staus of each pin.
//
// configure pullup resistors? zero means floating, non-zero means pullup
const int pullup[] = {0, 0, // RX,TX on Uno
0, 0, 0, // 2,3,4
0, 0, 0, 0, 0, // 5-9
0, 0, 0, 0, // 10,11,12,13
0, 0, 0, 0, 0, 0 // A0, A1 A2 A3 A4 A5
};
void setup() {
Serial.begin(115200);
// do INPUT_PULLUP on everything?
for (int ii = 2; ii < NUM_DIGITAL_PINS; ++ii) {
if (pullup[ii] != 0 ) pinMode(ii, INPUT_PULLUP);
}
}
void loop() {
report();
}
void report() {
auto now = millis(); // https://www.arduino.cc/reference/en/language/functions/time/millis/
const typeof(now) interval = 1000;
static typeof(now) last = -interval; // schedule to trigger immediately
if (now - last >= interval) { // Expired?
last += interval; // Advance phase-locked with the processor clock
//while(now - last >= interval){last += interval;} // Advance multiple times. Needed only if loop() is stupidly slow
Serial.print(now);
Serial.print("ms");
for ( int ii = 0; ii < NUM_ANALOG_INPUTS; ++ii) {
Serial.print(" ");
Serial.print(analogRead(ii));
}
for ( int ii = 0; ii < NUM_DIGITAL_PINS; ++ii) {
Serial.print(" ");
Serial.print(digitalRead(ii));
}
Serial.println();
}
}
Simulation with a couple inputs connected to buttons: