import machine from pyb import Accel accel = Accel() print(accel.x(), accel.y(), accel.z(), accel.tilt())
from pyb import LED as l from time import sleep as t from pyb import Accel accel = Accel().x led_i = [l(1), l(2), l(3), l(4)] def x(): x = int((int(accel()) + 21) / 14) print(x) return x while 1: for h in led_i: h.off() led_i[x()].on() t(0.2)
# Imports import pyb import machine from pyb import Accel from pyb import Pin # In/Out -puts defenition accel = Accel() # built in accelorometer led1 = pyb.LED(1) led2 = pyb.LED(2) led3 = pyb.LED(3) switch = pyb.Switch() # built in switch # defenition of pins # input pins used with interupts lightCurtain_x5 = Pin('X5', Pin.IN, Pin.PULL_UP) dutFinished_x6 = Pin('X6', Pin.IN, Pin.PULL_UP) closeLid_x7 = Pin('X7', Pin.IN, Pin.PULL_UP) allLightsOff_x8 = Pin('X8', Pin.IN, Pin.PULL_UP) switch = Pin('X17', Pin.IN, Pin.PULL_UP) # output pins connected to the cylindervalves # passRelay: # True: valve can move to right or left # False: valve is in non powerd state # switchRelay: # True: cylinder retracts (gets pushed in) # False: cylinder extends (gets pushed out) passRelay50_x1 = Pin('X1', Pin.OUT) cylinderRetract50_x2 = Pin('X2', Pin.OUT)
from pyb import Accel, Servo servo = Servo(1) accel = Accel() while 1: servo.angle(accel.x(), 100) pyb.delay(200)
from pyb import Accel from pyb import Gyro from pyb import Mag a = Accel(range=Accel.RANGE_8G) g = Gyro() m = Mag() while True: pyb.delay(1000) ax = a.x() ay = a.y() az = a.z() mx = m.x() my = m.y() mz = m.z() gx = g.x() gy = g.y() gz = g.z() print('-----') print(ax, ay, az) print(gx, gy, gz) print(mx, my, mz)
#hardware platform: pyboard V1.1 from pyb import Accel import time accel = Accel() #Accel is an object that controls the accelerometer. while True: time.sleep(0.1) print("x:%d, y:%d, z:%d" % (accel.x(), accel.y(), accel.z()))
# MicroPython for the IOT - Chapter 6 # Example for working with the accelerometer on a Pyboard # Note: only works for the Pyboard! import pyb from pyb import Accel acc = Accel() print("Press CTRL-C to stop.") while True: pyb.delay(500) # Short delay before sampling print("X = {0:03} Y = {1:03} Z = {2:03}".format(acc.x(), acc.y(), acc.z()))