def deal_with_command(s): if s[0] != ID: return 0 if s[1] != ord(b'S') and s[1] != ord(b'R') and s[1] != ord(b'Q'): return 0 if s[1] == ord(b'S'): for i in range(4, 8, 1): speed[i - 4] = s[i] set_car_speed() st = struct.pack('4s4B6i', s[0:4], speed[0], speed[1], speed[2], speed[3], compass.get_x(), compass.get_y(), compass.get_z(), accelerometer.get_x(), accelerometer.get_y(), accelerometer.get_z()) uart.write(st) sleep(20) if s[1] == ord(b'R'): display.show(Image.SAD) uart.write('+++') for i in range(4, 8, 1): speed[i - 4] = 0 set_car_speed() sleep(1000) reset() return 1
def getAcceleration(self): x = accelerometer.get_x() y = accelerometer.get_y() z = accelerometer.get_z() acceleration = math.sqrt(x ** 2 + y ** 2 + z ** 2) # ** means make this a power of. return acceleration
def get_sensor_data(): x = accelerometer.get_x() y = accelerometer.get_y() z = accelerometer.get_z() a = button_a.was_pressed() b = button_b.was_pressed() print(x, y, z, a, b) return x, y
def getAcceleration(self): x = accelerometer.get_x() y = accelerometer.get_y() z = accelerometer.get_z() acceleration = math.sqrt(x**2 + y**2 + z**2) return acceleration
def get_delay(self): """ Return the delay depending on the Z accelerometer. """ # Completely untested, I had to write it blindly :( z = accelerometer.get_z() delay = 1-z delay = delay*(800) + 200 # always generate a delay between 0.2 and 1 seconds return int(delay)
use_cutoff = 2 ### SETTINGS END ### from microbit import accelerometer, button_b, pin1, pin2, display, Image, sleep import math import utime #import music # variables current_sec = float(utime.ticks_ms())/1000. # some reference time in ms # read in currect accel data accel = math.sqrt(float(accelerometer.get_x())**2 + float(accelerometer.get_y())**2 + float(accelerometer.get_z())**2) window_values = [0] * RAVG_AVG_WIN amplitude_window_values = [0] * RAVG_AMP_WIN stepspermin_window_values = [0] * RAVG_AMP_WIN count = 0 count_amplitude = 0 accel_avg = 0. #peak_resolution_accel = 50 peaks_sec = [current_sec, current_sec, current_sec] peaks = [-1, -1, -1] current_peak = 0 nr_peaks = 3 valleys_sec = [current_sec, current_sec, current_sec] valleys = [2000, 2000, 2000] current_valley = 0
def accel(): while not button_a.is_pressed(): p = abs(accelerometer.get_z()) pitch(p, wait=False) sleep(10)
def rotationRoll(): return atan2( accelerometer.get_x(), sqrt(accelerometer.get_y() ** 2 + accelerometer.get_z() ** 2) ) * (180 / pi)
def read_y(): i2c.write(MMA8653_ADDR, bytes([MMA8653_OUT_Y_MSB]), repeat=True) result = i2c.read(MMA8653_ADDR, 1) # Unpack it as a signed char result = ustruct.unpack('b', result)[0] # Scale it to 0 to +/- 2000 and set orientation return result * 16 * -1 def read_z(): i2c.write(MMA8653_ADDR, bytes([MMA8653_OUT_Z_MSB]), repeat=True) result = i2c.read(MMA8653_ADDR, 1) # Unpack it as a signed char result = ustruct.unpack('b', result)[0] # Scale it to 0 to +/- 2000 return result * 16 check_device() configure_device() while True: x = read_x() y = read_y() z = read_z() print("[X:{}] [Y:{}] [Z:{}]".format(x, y, z)) print("[X:{}] [Y:{}] [Z:{}]\n".format( accelerometer.get_x(), accelerometer.get_y(), accelerometer.get_z())) sleep(200)
display.clear() audio.play(Sound.SPRING) sleep(500) numbers = ('One', 'Two', 'Three') for i in range(3, 0, -1): display.show(i) speech.say(numbers[i - 1], speed=500, pitch=50, throat=100, mouth=50) sleep(750) set_volume(volume) while score < max_score: shake = max( 0, abs(accelerometer.get_x()) + abs(accelerometer.get_y()) + abs(accelerometer.get_z()) - 2048) level = min( max(0, level - drop_level + translate(shake, 0, 2048, 0, add_level)), max_level) pitch = round(translate(level, 0, max_level, 440, 880)) plotBarGraph(level, max_level) if level == max_level: level = 0 score += 1 music.stop() sleep(50) display.show(Image.YES) music.play(music.BA_DING) sleep(450) continue if buzzer:
def read_y(): i2c.write(MMA8653_ADDR, bytes([MMA8653_OUT_Y_MSB]), repeat=True) result = i2c.read(MMA8653_ADDR, 1) # Unpack it as a signed char result = ustruct.unpack('b', result)[0] # Scale it to 0 to +/- 2000 and set orientation return result * 16 * -1 def read_z(): i2c.write(MMA8653_ADDR, bytes([MMA8653_OUT_Z_MSB]), repeat=True) result = i2c.read(MMA8653_ADDR, 1) # Unpack it as a signed char result = ustruct.unpack('b', result)[0] # Scale it to 0 to +/- 2000 return result * 16 check_device() while True: x = read_x() y = read_y() z = read_z() print("[X:{}] [Y:{}] [Z:{}]".format(x, y, z)) print("[X:{}] [Y:{}] [Z:{}]\n".format(accelerometer.get_x(), accelerometer.get_y(), accelerometer.get_z())) sleep(200)
def getangle(): """ Accelerometer y output for first quadrant range 0-1024 """ """ ~11.378 steps/degree (1024 steps / 90 degrees ) """ return math.radians(accelerometer.get_y() / 11.378), accelerometer.get_z( ) #translate y angle to radians, z for backward shooting check (z should always be negative or zero if shooting straight up)