#!/usr/bin/env python
import time
import signal
import sys
from PyMata.pymata import PyMata
# Digital pins
GREEN_LED = 6
PUSH_BUTTON1 = 11
PUSH_BUTTON2 = 12
# LED value
led_value = 0
# Callback function
# Set the LED to pwm value of the pushbutton switch
def cb_push_button(data):
global led_value
print(data)
print(led_value)
if data[1] == 11:
led_value=led_value+5
if led_value
> 255:
led_value=255
board.analog_write(GREEN_LED,
led_value)
else:
led_value=led_value-5
if led_value
< 0:
led_value=0
board.analog_write(GREEN_LED,
led_value)
# Re-arm the latch to detect when the
button is pressed
board.set_digital_latch(PUSH_BUTTON1,
board.DIGITAL_LATCH_HIGH, cb_push_button)
board.set_digital_latch(PUSH_BUTTON2,
board.DIGITAL_LATCH_HIGH, cb_push_button)
def signal_handler(sig, frame):
print('You pressed Ctrl+C')
if board is not None:
board.close()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
# Create a PyMata instance
board = PyMata("/dev/ttyUSB0", verbose=True)
# Set pin modes
# Set the pin to digital output to light the green LED
board.set_pin_mode(GREEN_LED, board.PWM,
board.DIGITAL)
# Set the pin to digital input to receive button presses
board.set_pin_mode(PUSH_BUTTON1, board.INPUT,
board.DIGITAL)
board.set_pin_mode(PUSH_BUTTON2, board.INPUT,
board.DIGITAL)
# Arm the digital latch to detect when the button is
pressed
board.set_digital_latch(PUSH_BUTTON1,
board.DIGITAL_LATCH_HIGH, cb_push_button)
board.set_digital_latch(PUSH_BUTTON2,
board.DIGITAL_LATCH_HIGH, cb_push_button)
# A forever loop until user presses Ctrl+C
while 1:
pass
|