code.py
 1  # SPDX-FileCopyrightText: 2017 John Edgar Park for Adafruit Industries
 2  #
 3  # SPDX-License-Identifier: MIT
 4  
 5  # Screaming Cauldron
 6  # for Adafruit Industries Learning Guide
 7  
 8  import time
 9  
10  import board
11  import neopixel
12  from analogio import AnalogIn
13  from digitalio import DigitalInOut, Direction
14  
15  led = DigitalInOut(board.D13)  # on board red LED
16  led.direction = Direction.OUTPUT
17  
18  aFXPin = DigitalInOut(board.D2)  # pin used to drive the AudioFX board
19  aFXPin.direction = Direction.OUTPUT
20  
21  aFXPin.value = False  # runs once at startup
22  time.sleep(.1)  # pause a moment
23  aFXPin.value = True  # then turn it off
24  
25  analog0in = AnalogIn(board.D1)
26  neoPin = board.D0  # pin int0 which the NeoPixels are plugged
27  numPix = 30  # number of NeoPixels
28  pixels = neopixel.NeoPixel(neoPin, numPix, auto_write=0, brightness=.8)
29  pixels.fill((0, 0, 0,))
30  pixels.show()
31  
32  
33  def get_voltage(pin):
34      return (pin.value * 3.3) / 65536
35  
36  
37  def remap_range(value, left_min, left_max, right_min, right_max):
38      # this remaps a value from original (left) range to new (right) range
39      # Figure out how 'wide' each range is
40      leftSpan = left_max - left_min
41      rightSpan = right_max - right_min
42  
43      # Convert the left range into a 0-1 range (int)
44      valueScaled = int(value - left_min) / int(leftSpan)
45  
46      # Convert the 0-1 range into a value in the right range.
47      return int(right_min + (valueScaled * rightSpan))
48  
49  
50  while True:
51      distRaw = analog0in.value  # read the raw sensor value
52      print("A0: %f" % distRaw)  # write raw value to REPL
53      distRedColor = remap_range(distRaw, 0, 64000, 0, 255)
54      distGreenColor = remap_range(distRaw, 0, 64000, 200, 0)
55      if distRaw > 40000:  # at about 4 inches, this goes off!
56          led.value = True
57          aFXPin.value = False
58          time.sleep(.35)
59  
60      else:
61          led.value = False
62          aFXPin.value = True
63  
64      # change pixel colors based on proximity
65  
66      print(distRedColor, distGreenColor, (distRedColor + distGreenColor))
67      pixels.fill((distRedColor, distGreenColor, 0))
68  
69      pixels.write()
70  
71      time.sleep(0.1)