def main(): import pyb serial = pyb.USB_VCP() midi = MidiOut(serial, channel=1) switch = pyb.Switch() if hasattr(pyb, 'Accel'): accel = pyb.Accel() SCALE = 1.27 else: from staccel import STAccel accel = STAccel() SCALE = 127 while True: while not switch(): pyb.delay(10) note = abs(int(accel.x() * SCALE)) velocity = abs(int(accel.y() * SCALE)) midi.note_on(note, velocity) while switch(): pyb.delay(50) midi.note_off(note)
def main(): sw_state = False led = pyb.LED(1) switch = pyb.Switch() switch.callback(set_sw_state) accel = STAccel() hid = pyb.USB_HID() while True: if sw_state: x, y, z = accel.xyz() hid.send((0, int(x * MAG), int(-y * MAG), 0)) pyb.delay(int(1000 / FREQ))
def led_angle(): # make LED objects l1 = pyb.LED(1) l2 = pyb.LED(2) accel = STAccel() while True: # get x-axis x = accel.x() * 50 # turn on LEDs depending on angle if x < -10: l2.on() l1.off() elif x > 10: l1.on() l2.off() else: l1.off() l2.off() # delay so that loop runs at at 1/50ms = 20Hz pyb.delay(50)
import pyb from pyb import Pin from staccel import STAccel import math accel = STAccel() click_threshold = 1.5 right_threshold = 0.4 left_threshold = -right_threshold up_threshold = 0.4 down_threshold = -up_threshold reverse_threshold = -0.4 def isReversed(z): if z <= reverse_threshold: return True else: return False def isClicked(z): if z > click_threshold: return True else: return False def getDiff(x, y): x_diff = 0
import pyb from pyb import Pin from staccel import STAccel from sc1602 import SC1602 lcd_pin = { 'RS': Pin.board.PB8, 'E': Pin.board.PB9, 'DB0': Pin.board.PE6, 'DB1': Pin.board.PC13, 'DB2': Pin.board.PE4, 'DB3': Pin.board.PE5, 'DB4': Pin.board.PB4, 'DB7': Pin.board.PB7, 'DB6': Pin.board.PD7, 'DB5': Pin.board.PB5, } accel = STAccel() lcd = SC1602(lcd_pin) lcd.initialize() while True: x, y, z = accel.xyz() lcd.set_cursor(0, 0) lcd.write("X:{0:+5.02f} Y:{1:+5.02f} ".format(x, y)) lcd.set_cursor(1, 0) lcd.write("Z:{0:+5.02f} ".format(z)) pyb.delay(100)
global acc_it_count acc_it = 1 acc_it_count += 1 accint = pyb.ExtInt(pyb.Pin.cpu.E0, ExtInt.IRQ_RISING, Pin.PULL_DOWN, acccallback) def rangecallback(line): global range_it global range_it_count range_it = 1 range_it_count += 1 rangeint = pyb.ExtInt(pyb.Pin.cpu.D1, ExtInt.IRQ_RISING, Pin.PULL_DOWN, rangecallback) rangepin.high() acc = STAccel() bot = Robot() dist = VL6180() bot.SetPower(100, 100) bot.SetMaxSpeed(256, 256) bot.MotorStatus(1) if do_log == 1: f = open("/sd/log.csv", "w") pyb.delay(5) dist.start_cont_range()