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