/ AutoSunglasses / code.py
code.py
 1  # SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  """
 6  Circuit Playground Express auto-sunglasses/flashlight
 7  
 8  Adafruit invests time and resources providing this open source code.
 9  Please support Adafruit and open source hardware by purchasing
10  products from Adafruit!
11  
12  Written by Dave Astels for Adafruit Industries
13  Copyright (c) 2018 Adafruit Industries
14  Licensed under the MIT license.
15  
16  All text above must be included in any redistribution.
17  """
18  
19  import time
20  import board
21  import pwmio
22  from adafruit_circuitplayground.express import cpx
23  from adafruit_motor import servo
24  
25  pwm = pwmio.PWMOut(board.A1, duty_cycle=2 ** 15, frequency=50)
26  my_servo = servo.Servo(pwm)
27  
28  cpx.pixels.fill((0, 0, 0))
29  my_servo.angle = 90
30  
31  while True:
32      light_level = cpx.light
33  
34      if light_level < 10:
35          cpx.pixels.fill((200, 200, 200))
36      else:
37          cpx.pixels.fill((0, 0, 0))
38          if light_level < 200:
39              my_servo.angle = 90
40          else:
41              my_servo.angle = 0
42  
43      time.sleep(0.25)