code.py
1 # SPDX-FileCopyrightText: 2018 Limor Fried for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import time 6 from busio import I2C 7 import analogio 8 from adafruit_seesaw.seesaw import Seesaw 9 from adafruit_seesaw.pwmout import PWMOut 10 from adafruit_motor import motor 11 import board 12 13 light = analogio.AnalogIn(board.LIGHT) 14 15 16 print("Theramin-like turning") 17 18 # Create seesaw object 19 i2c = I2C(board.SCL, board.SDA) 20 seesaw = Seesaw(i2c) 21 22 # Create one motor on seesaw PWM pins 22 & 23 23 motor_a = motor.DCMotor(PWMOut(seesaw, 22), PWMOut(seesaw, 23)) 24 motor_a.throttle = 0 # motor is stopped 25 26 def map_range(x, in_min, in_max, out_min, out_max): 27 # Maps a number from one range to another. 28 mapped = (x-in_min) * (out_max - out_min) / (in_max-in_min) + out_min 29 if out_min <= out_max: 30 return max(min(mapped, out_max), out_min) 31 return min(max(mapped, out_max), out_min) 32 33 while True: 34 print((light.value,)) 35 motor_a.throttle = map_range(light.value, 500, 8000, 1.0, 0) 36 time.sleep(0.1)