int pbVal = 0;
int pbPin;
#define ledPin 13
void setup() {
Serial.begin(9600);
Serial.println("Test Push Button");
Serial.println("================");
Serial.println();
Serial.println("Connecting guidelines:");
Serial.println("Place the PB above the mid rail of the breadboard");
Serial.println("in way that two legs are on one side of the rail");
Serial.println("and the other two legs are on the other side");
Serial.println();
Serial.println("One leg is connected to 5V");
Serial.println("the opposite leg is connected to digital pin, and");
Serial.println("to a 10K Ohm resistor that goes to GND");
Serial.println("This is PULL DOWN connection");
Serial.println();
Serial.println();
Serial.println("The program reads the state of the button (pressed or released)");
Serial.println("and prints the state to screen and controlls the on board led");
pinMode(ledPin, OUTPUT);
Serial.println();
Serial.println();
Serial.println("Enter the pin number that the PB is connected to:");
while (Serial.available() == 0);
pbPin = Serial.parseInt();
while (pbPin < 2 || pbPin > 13) {
Serial.println("Pin number " + String(pbPin) + " is invalid.");
Serial.println("Check your connection and/or eneterd data");
Serial.println();
Serial.println("Enter the pin number that the PB is connected to:");
Serial.println();
while (Serial.available() == 0);
pbPin = Serial.parseInt();
}
pinMode(pbPin, INPUT);
}
void loop() {
pbVal = digitalRead(pbPin);
Serial.println("PB state is:" + String((pbVal ? "HIGH" : "LOW")) );
digitalWrite(ledPin, pbVal);
delay(1000);
}
Comments