Advertisement
gabbyshimoni

testPBconnection

Feb 12th, 2020
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. int pbVal = 0;
  2. int pbPin;
  3. #define ledPin 13
  4.  
  5. void setup() {
  6. Serial.begin(9600);
  7. Serial.println("Test Push Button");
  8. Serial.println("================");
  9. Serial.println();
  10. Serial.println("Connecting guidelines:");
  11. Serial.println("Place the PB above the mid rail of the breadboard");
  12. Serial.println("in way that two legs are on one side of the rail");
  13. Serial.println("and the other two legs are on the other side");
  14. Serial.println();
  15. Serial.println("One leg is connected to 5V");
  16. Serial.println("the opposite leg is connected to digital pin, and");
  17. Serial.println("to a 10K Ohm resistor that goes to GND");
  18. Serial.println("This is PULL DOWN connection");
  19. Serial.println();
  20. Serial.println();
  21. Serial.println("The program reads the state of the button (pressed or released)");
  22. Serial.println("and prints the state to screen and controlls the on board led");
  23.  
  24. pinMode(ledPin, OUTPUT);
  25.  
  26. Serial.println();
  27. Serial.println();
  28. Serial.println("Enter the pin number that the PB is connected to:");
  29. while (Serial.available() == 0);
  30. pbPin = Serial.parseInt();
  31. while (pbPin < 2 || pbPin > 13) {
  32. Serial.println("Pin number " + String(pbPin) + " is invalid.");
  33. Serial.println("Check your connection and/or eneterd data");
  34. Serial.println();
  35. Serial.println("Enter the pin number that the PB is connected to:");
  36. Serial.println();
  37. while (Serial.available() == 0);
  38. pbPin = Serial.parseInt();
  39. }
  40. pinMode(pbPin, INPUT);
  41. }
  42.  
  43. void loop() {
  44.  
  45. pbVal = digitalRead(pbPin);
  46. Serial.println("PB state is:" + String((pbVal ? "HIGH" : "LOW")) );
  47. digitalWrite(ledPin, pbVal);
  48. delay(1000);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement