>>> exit()
Enter the following command to create a new files called switch.py
# nano switch.py
import Adafruit_BBIO.GPIO as GPIO import time GPIO.setup("P8_12", GPIO.IN) old_switch_state = 0 while True: new_switch_state = GPIO.input("P8_12") if new_switch_state == 1 and old_switch_state == 0 : print('Do not press this button again!') time.sleep(0.1) old_switch_state = new_switch_state
To start the program, enter the command:
# python switch.py
# python switch.py Do not press this button again! Do not press this button again!
We only want the message to appear when the button is pressed. To prevent the message appearing continuously as long as the button is held down the variable old_switch_state is used and the message only displayed when the switch goes from not being pressed to being pressed.
The time.sleep command is a simple way to avoid switch bounce, which would cause the message to appear twice if the switch contacts did not close cleanly when the button is pressed.