/ Crickit_Exhibit / code.py
code.py
1 # SPDX-FileCopyrightText: 2019 Dano Wall for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 """ 6 Crickit Exhibit 7 Project by Dano Wall and Isaac Wellish 8 Code by Isaac Wellish 9 The Crickit Exhibit demonstrates almost all of the capabilities 10 which CRICKIT can offer in one project 11 """ 12 13 # Functions: 14 #1. Hit a button to trigger a solenoid 15 #2. Hit a button to turn on an electromagnet 16 #3. Touch conductive tape to trigger a neopixel animation 17 #4. Turn a potentiometer to control a servo 18 #5. Shine light on the CPX to trigger and change the speed of a DC motor 19 #6. Hit both buttons to trigger a sound from the speaker! 20 21 import time 22 from adafruit_crickit import crickit 23 import board 24 import neopixel 25 from analogio import AnalogIn 26 from simpleio import map_range, tone 27 28 # RGB values 29 RED = (255, 0, 0) 30 YELLOW = (255, 150, 0) 31 GREEN = (0, 255, 0) 32 CYAN = (0, 255, 255) 33 BLUE = (0, 0, 255) 34 PURPLE = (180, 0, 255) 35 36 # For signal control, we'll chat directly with seesaw, use 'ss' to shorted typing! 37 # create seesaw object 38 ss = crickit.seesaw 39 40 # Two buttons are pullups, connect to ground to activate 41 BUTTON_1 = crickit.SIGNAL1 42 BUTTON_2 = crickit.SIGNAL2 43 44 ss.pin_mode(BUTTON_1, ss.INPUT_PULLUP) 45 ss.pin_mode(BUTTON_2, ss.INPUT_PULLUP) 46 47 #solenoid at drive spot 1 48 crickit.drive_1.frequency = 1000 49 50 #electromagnet at drive spot 2 51 crickit.drive_2.frequency = 1000 52 53 # initialize NeoPixels to num_pixels 54 num_pixels = 30 55 56 # The following line sets up a NeoPixel strip on Crickit CPX pin A1 57 pixels = neopixel.NeoPixel(board.A1, num_pixels, brightness=0.3, auto_write=False) 58 59 #sleep var for pushing both buttons 60 SLEEP_DELAY = 0.1 61 62 # NeoPixel function 63 def color_chase(color, wait): 64 for i in range(num_pixels): 65 pixels[i] = color 66 time.sleep(wait) 67 pixels.show() 68 time.sleep(0.5) 69 70 # potentiometer connected to signal #3 71 pot = crickit.SIGNAL8 72 73 # initialize the light sensor on the CPX and the DC motor 74 light_in = AnalogIn(board.LIGHT) 75 76 while True: 77 78 # button + solenoid & electromagnet code 79 # button 1 - solenoid on 80 if not ss.digital_read(BUTTON_1): 81 print("Button 1 pressed") 82 crickit.drive_1.fraction = 1.0 # all the way on 83 time.sleep(0.01) 84 crickit.drive_1.fraction = 0.0 # all the way off 85 time.sleep(0.5) 86 else: 87 crickit.drive_1.fraction = 0.0 88 89 # button 2 electromagnet on 90 if not ss.digital_read(BUTTON_2): 91 print("Button 2 pressed") 92 crickit.drive_2.fraction = 1.0 # all the way on 93 time.sleep(0.5) 94 else: 95 crickit.drive_2.fraction = 0.0 # all the way off 96 97 # Capacitive touch + neopixel code 98 touch_raw_value = crickit.touch_1.raw_value 99 100 if touch_raw_value>800: 101 print("chase") 102 color_chase(PURPLE, 0.1) 103 else: 104 pixels.fill((0,0,0)) 105 pixels.show() 106 107 # potentiomter + servo 108 109 # uncomment this line to see the values of the pot 110 # print((ss.analog_read(pot),)) 111 # time.sleep(0.25) 112 113 # maps the range of the pot to the range of the servo 114 angle = map_range(ss.analog_read(pot), 0, 1023, 180, 0) 115 116 # sets the servo equal to the relative position on the pot 117 crickit.servo_1.angle = angle 118 119 # Light sensor + DC motor 120 121 # uncomment to see values of light 122 # print(light_in.value) 123 # time.sleep(0.5) 124 125 # reads the on-board light sensor and graphs the brighness with NeoPixels 126 # light value remaped to motor speed 127 peak = map_range(light_in.value, 3000, 62000, 0, 1) 128 129 # DC motor 130 crickit.dc_motor_1.throttle = peak # full speed forward 131 132 # hit both buttons to trigger noise 133 if not ss.digital_read(BUTTON_1) and not ss.digital_read(BUTTON_2): 134 print("Buttons 1 and 2 pressed") 135 for f in (262, 294, 330, 349, 392, 440, 494, 523): 136 tone(board.A0, f, 0.25) 137 time.sleep(SLEEP_DELAY)