The following code should (I hope) turn on an LED if the switch (set as HIGH) in Setup) is "turned-on" to connect pin 2 to ground. The sketch serialprints "HIGH" or "LOW" supposedly on the basis of the state of the button. The actual result is the consistent reporting of "HIGH". I assume that I have coded this sketch badly or I am using the "digitalRead" incorrectly.
int state = 0;
const int ledPin = 8;
const int buttonPin = 2;
int buttonState = 1;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW)
{ Serial.println ("buttonstate is low.");
}
else
{ Serial.println ("buttonstate is --- HIGH .");
}
state = buttonState;
if (state == '1')
{ digitalWrite(ledPin, HIGH);
state = 0;
}
else if (state == '0')
{ digitalWrite(ledPin, LOW); // LED ON
state = 0;

}
delay(5000);
}
Hi,
If you are switching pin 2 to gnd when you press the button,
Change;
pinMode(buttonPin, INPUT);
to
pinMode(buttonPin, INPUT_PULLUP);
When you open the button contact you need to pull Pin 2 HIGH, it doesn't happen on its own, so that PULLUP turns on an internal pullup resistor to pull pin2 high when the button is open.
will give you the exact same result. One is simply more efficient but both will work perfectly well to read from the button. This is not a floating pin problem.
This sketch --- incorprating some of your (everyone) suggestions and advice (while ignoring comments clearly meant as no more than insults) --- works as planned. That is, the sketch differentiates between "HIGH" and "LOW" as I needed. The "delay" lines give me moments to observes the results (LED light and hoped-for Serial.print displays on the Serial Monitor.
int state = 0;
const int ledPin = 8;
const int buttonPin = 2;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
pinMode(buttonPin, INPUT_PULLUP);
digitalWrite(buttonPin, HIGH);
int buttonState = 0;
}
void loop()
{
int buttonState = digitalRead(buttonPin);
Serial.print("buttonstate is ");
Serial.println(buttonState);
if (buttonState == 0)
{
Serial.println ("buttonState is low.");
state = buttonState;
}
else
{
Serial.println ("buttonState is --- HIGH .");
state = buttonState;
}
if (state == 1)
{
digitalWrite(ledPin, HIGH); // LED ON
state = 0;
delay(5000);
}
else if (state == 0);
{
digitalWrite(ledPin, LOW); // LED ON
state = 0;
}
delay(3000);
}
Ahh, giod piont. The odd thing is that the sketch more or less works. Obviously, I have to re-test this. Thank, ToddL (I do wonder if you tag 1962 to names for the same reason I tag 1943 to my lables.)
RE-opening, perhaps. In following sketch (see below) I am trying to respond to the grounding of pin 2. Pressing a button connects that pin to ground on the Pro-Mini. This was tried without a "PULLUP" setup, and the digit readout from pin 2 was a 1 (integer).
On advice in this forum then I coded for INPUT_PULLUP for the pin, with a 10K resistor between the switch and pin2. The result did not change. The return from the digit read wa a 1 (int), again.
The test code (before and after the digital read) is in the SETUP.
Here is the sketch
#include <SoftwareSerial.h>
const int led = 8;
const int button = 2;
int loop_count;
int ButtonState;
// #include <SoftwareSerial.h>
void setup()
{
Serial.begin(9600);
Serial.println("\nstarting setup");
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
pinMode(button, INPUT_PULLUP);
digitalWrite(button, HIGH);
int C_status = 0;
int ButtonState = 0;
Serial.print("\nShow 'ButtonState' before first digital read. It is -->");
Serial.print(ButtonState);
Serial.println(".");
Serial.println ("\nnext, trial digital read.\npress and hold the button");
delay(750);
ButtonState = digitalRead(button);
Serial.print("\n The button state is "); Serial.println(ButtonState);
Serial.println("Slave Sketch - end of setup");
delay(3000);
}
void loop()
{
if (loop_count == 0) {
ButtonState = digitalRead(button);
Serial.print("after setup digital read produces the buttonState - ");
Serial.println(ButtonState);
loop_count ++;
}
// Reading the button
ButtonState = digitalRead(button);
Serial.print("\t\t after 2nd Digital read in loop the ButtonState is ");
Serial.println(ButtonState);
if (ButtonState == 0)
{
Serial.print("\n ButtonState tests [in an if just above] as ");
Serial.print(ButtonState);
Serial.println(" . That should match the previous serialprint.");
}
else
{
Serial.print("\n ButtonState tests [in an if just above] as ");
Serial.print(ButtonState);
Serial.println(" . That === should match === the previous serialprint.");
}
delay(3000);
}
The grounds are solid, even back to the GND pin on the UNO.
The UNO is in "Arduino/ISP" mode so that I can upload code changes to the Pro-Mini.
Here is the output of the Serial Monitor:
Below please see the wiring/connections of my test setup. Rememeber that the Pro_Min is connected to the UNO for uploading via the connnections to the ISP pins on the UNO
Here is the Serial Monitor output.
15:38:12.270 -> starting setup
15:38:12.270 ->
15:38:12.270 -> Show 'ButtonState' before first digital read. It is -->0.
15:38:12.335 ->
15:38:12.336 -> next, trial digital read.
15:38:12.369 -> press and hold the button
15:38:13.098 ->
15:38:13.098 -> The button state is 1
15:38:13.098 -> Slave Sketch - end of setup
15:38:16.066 -> after setup digital read produces the buttonState - 1
15:38:16.132 -> after 2nd Digital read in loop the ButtonState is 1
15:38:16.198 ->
15:38:16.198 -> ButtonState tests [in an if just above] as 1 . That === should match === the previous serialprint.
15:38:19.224 -> after 2nd Digital read in loop the ButtonState is 1
15:38:19.290 ->
15:38:19.290 -> ButtonState tests [in an if just above] as 1 . That === should match === the previous serialprint.
15:38:22.328 -> after 2nd Digital read in loop the ButtonState is 1
Here is the physical setup.Remember that the Pro Mini is conncected to the UNO for uploading via Arduino/ISP.
Hi, paulpaulson. I need a bit of help
The note on S1 and R4 is clear. That pretty much tells me that the whole "External Pull Down" use is "BAD."
All of the lines into the UNO Rev3 are red or green, so I infer that any of these connections are okay.
It appears that the S2 and the S3 legs of the circuit are okay; I infer that the red connectors to these switch are neutral (neutral in terms of best or good),
I have a bit of difficulty differing between the black ciruit components and possibly blue ones. I think that the whole LED connections line means neutral (not bad. good, or best --- okay). I acknowledge that the "blue" "black" problem is mine.
The SKETCH is a minor concern in our conversation.
By the way, I printed the diagram for ongoing refence.
Thank you.