You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.1 KiB

5 years ago
  1. /*
  2. * Computer Switchboard
  3. *
  4. * Because interfacing with computers should be fun
  5. * and a keyboard is not enough.
  6. *
  7. * Let's turn a computer into an airplane (interface wise).
  8. *
  9. */
  10. /*
  11. DigitalReadSerial
  12. Reads a digital input on pin 2, prints the result to the serial monitor
  13. This example code is in the public domain.
  14. */
  15. // digital pin 2 has a pushbutton attached to it. Give it a name:
  16. int pushButton = 9;
  17. // the setup routine runs once when you press reset:
  18. void setup() {
  19. // initialize serial communication at 9600 bits per second:
  20. Serial.begin(9600);
  21. // make the pushbutton's pin an input:
  22. // I was somewhat confused about this. I thought maybe it would be an output
  23. // to ground, through the resistor, and it would only conduct when grounded, but
  24. // using the input pullup works. I'm not sure if you can detect when an output grounds or is conducting...
  25. pinMode(pushButton, INPUT_PULLUP);
  26. //https://stackoverflow.com/questions/6160963/how-can-i-digitalread-a-pin-that-is-in-pinmode-output
  27. //https://forum.arduino.cc/index.php?topic=183977.0
  28. //This approach doesn't work as you can't read a floating pin reliably
  29. //so don't do it. It always reads zero, or will read noise.
  30. //pinMode(pushButton, OUTPUT);
  31. //digitalWrite(3,HIGH);
  32. //interestingly, you could use a shunt resistor... but more complex
  33. //https://arduino.stackexchange.com/questions/14647/how-can-i-detect-a-disconnected-pin
  34. delay(100);
  35. }
  36. // the loop routine runs over and over again forever:
  37. void loop() {
  38. // easy way
  39. // read the input pin:
  40. // int buttonState = digitalRead(pushButton);
  41. // print out the state of the button:
  42. // Serial.println(buttonState);
  43. delay(10); // delay in between reads for stability
  44. int resultb = 0;
  45. resultb = PINB;
  46. Serial.println(resultb,BIN); //noisy, but reads 0 when low.
  47. // Serial.println(bitRead(PORTB,1));// PORTB is for high or low
  48. // PIN is for reading input. PORTB doesn't work here.
  49. // https://arduino.stackexchange.com/questions/14647/how-can-i-detect-a-disconnected-pin
  50. // not sure what that link is on about, regarding port usage.
  51. delay(100);
  52. }