code.py
  1  # SPDX-FileCopyrightText: 2018 Limor Fried for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  import time
  6  from digitalio import DigitalInOut, Direction, Pull
  7  import adafruit_lis3dh
  8  from busio import I2C
  9  from adafruit_seesaw.seesaw import Seesaw
 10  from adafruit_seesaw.pwmout import PWMOut
 11  from adafruit_motor import servo
 12  import neopixel
 13  import board
 14  
 15  # create accelerometer
 16  i2c1 = I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
 17  lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c1, address=0x19)
 18  lis3dh.range = adafruit_lis3dh.RANGE_8_G
 19  
 20  # Create seesaw object
 21  i2c = I2C(board.SCL, board.SDA)
 22  seesaw = Seesaw(i2c)
 23  
 24  # Create servo object
 25  pwm = PWMOut(seesaw, 17)    # Servo 1 is on s.s. pin 17
 26  pwm.frequency = 50          # Servos like 50 Hz signals
 27  my_servo = servo.Servo(pwm) # Create my_servo with pwm signal
 28  
 29  # LED for debugging
 30  led = DigitalInOut(board.D13)
 31  led.direction = Direction.OUTPUT
 32  
 33  # two buttons!
 34  button_a = DigitalInOut(board.BUTTON_A)
 35  button_a.direction = Direction.INPUT
 36  button_a.pull = Pull.DOWN
 37  button_b = DigitalInOut(board.BUTTON_B)
 38  button_b.direction = Direction.INPUT
 39  button_b.pull = Pull.DOWN
 40  
 41  # NeoPixels!
 42  pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=1)
 43  pixels.fill((0,0,0))
 44  
 45  #################### log file for logging mode!
 46  logfile = "/log.csv"
 47  # check that we could append if wanted to
 48  try:
 49      fp = None
 50      fp = open(logfile, "a")
 51      print("File system writable!")
 52  # pylint: disable=bare-except
 53  except:
 54      print("Not logging, trapeeze mode!")
 55  
 56  # If we log, have some helper variables
 57  logging = False
 58  logpoints = 0
 59  outstr = ""
 60  
 61  # When its time to release the trapeze
 62  release = False
 63  
 64  while True:
 65      if button_a.value: # A pressed
 66          while button_a.value:	# wait for release
 67              pass
 68          if fp:  # start or stop logging
 69              logging = not logging
 70              print("Logging: ", logging)
 71              time.sleep(0.25)
 72          else:
 73              my_servo.angle = 180      # open
 74  
 75      if button_b.value: # B pressed
 76          while button_b.value:	# wait for release
 77              pass
 78          my_servo.angle = 0      # close
 79  
 80      x, y, z = lis3dh.acceleration
 81  
 82      # To keep from corrupting the filesys, take 25 readings at once
 83      if logging and fp:
 84          outstr += "%0.2F, %0.2F, %0.2F\n" % (x, y, z)
 85          logpoints += 1
 86  
 87          if logpoints > 25:
 88              led.value = True
 89              #print("Writing: "+outstr)
 90              fp.write(outstr+"\n")
 91              fp.flush()
 92              led.value = False
 93              logpoints = 0
 94      else:
 95          # display some neopixel output!
 96          if z > 20:
 97              # MAXIMUM EFFORT!
 98              pixels.fill((0, 255, 0))
 99              if release:
100                  my_servo.angle = 180
101  
102          elif z < 3 and y > 0: # means at the outer edge
103              release = True
104              # flash red when we peak
105              pixels.fill((255, 0, 0))
106  
107          else:
108              pixels.fill((0,0,int(abs(z)*2)))
109  
110      time.sleep(0.05)