Forum Comments

LEDs not working
In Support
LEDs not working
In Support
blaravie
Apr 27, 2021
Interesting, so they all work as advertised so no issues there. When I load the code.py they do not respond or light up. Is it possible there is an issue with the potentiometer? Or any issues you see with the code? import time import board import neopixel import math import audioio import audiocore from audiocore import WaveFile from audioio import AudioOut import digitalio from digitalio import DigitalInOut, Direction, Pull from analogio import AnalogIn, AnalogOut bar_pin = board.D5 dial_pin = board.D6 bar_pixels = 15 dial_pixels = 6 RED = (255, 0, 0) YELLOW = (255, 255, 0) GREEN = (0, 255, 0) phase = 0 barrel = neopixel.NeoPixel(bar_pin, bar_pixels, brightness=1.0, auto_write=False) dial = neopixel.NeoPixel(dial_pin, dial_pixels, brightness=1.0, auto_write=False) # Give power to D10 which powers the speaker amplifier enable = digitalio.DigitalInOut(board.D10) enable.direction = digitalio.Direction.OUTPUT enable.value = True # Set up audio channel/Open audio files and prep for playback audio = audioio.AudioOut(board.A0) startup = audiocore.WaveFile(open("Startup.wav", "rb")) shoot = audiocore.WaveFile(open("RayGunPew.wav", "rb")) empty = audiocore.WaveFile(open("Empty.wav", "rb")) quote1 = audiocore.WaveFile(open("Dempsey_1.wav", "rb")) quote2 = audiocore.WaveFile(open("Dempsey_2.wav", "rb")) quote3 = audiocore.WaveFile(open("Dempsey_3.wav", "rb")) reloadOpen = audiocore.WaveFile(open("Reload_Open.wav", "rb")) reloadClose = audiocore.WaveFile(open("Reload_Close.wav", "rb")) trigger = DigitalInOut(board.D11) trigger.direction = Direction.INPUT trigger.pull = Pull.UP sensor = DigitalInOut(board.D12) sensor.direction = Direction.INPUT sensor.pull = Pull.UP pot_read = AnalogIn(board.A2) barrel_open = False hallstate = barrel_open trigger_count = 0 ct = 0 check = time.monotonic() last_check = check last_val = 0 pause = 0.05 def get_color(pos): if (pos < 0): barrel.fill(0, 0, 0) if (pos > 255): barrel.fill(0, 0, 0) if (pos < 85): barrel.fill((int(255 - pos*3), 0, int(pos*3))) elif (pos < 170): pos -= 85 barrel.fill((0, int(pos*3), int(255 - pos*3))) else: pos -= 170 barrel.fill((int(pos * 3), int(255 - (pos*3)), 0)) barrel.show() def get_mode(mode): if mode <= 100: barrel.fill((15, 100, 175)) barrel.show() elif mode > 100 and mode <= 200: barrel.fill((0, 255, 0)) barrel.show() elif mode > 200 and mode <= 300: barrel.fill((255, 0, 0)) barrel.show() elif mode > 300 and mode <= 400: blue_wave() else: white_wave() def blue_wave(): for p in range(bar_pixels): BLUE = int(abs(math.sin((p - phase)*3.14 / 18) * 235))+15 barrel[p] = (0, 0, BLUE) barrel.show() def white_wave(): for p in range(bar_pixels): WHITE = int(abs(math.sin((p - phase)*3.14 / 18) * 175))+15 barrel[p] = (WHITE, WHITE, WHITE) barrel.show() def dial_disp(): dial[0] = RED dial[1] = YELLOW dial[2] = GREEN dial.show() def bad_fire(i): if i == 0: audio.play(empty) elif i == 1: audio.play(quote1) elif i == 2: audio.play(quote2) elif i == 3: audio.play(quote3) for i in range(155): barrel.fill((i, i, i)) dial[0] = (i, 0, 0) dial[1] = (i, i, 0) dial[2] = (0, i, 0) dial.show() barrel.show() dial_disp() audio.play(startup) # Main loop while True: check = time.monotonic() delta = abs(check - last_check) if delta >= pause: reading = pot_read.value val = int((reading * 255) / 65535) get_color(val) phase += 1 last_check = check last_hallstate = hallstate hallstate = sensor.value # If trigger is pulled while the barrel is closed if not trigger.value and not sensor.value and not audio.playing: if trigger_count < 20: audio.play(shoot) elif ct < 4: bad_fire(ct) ct += 1 else: ct = 0 trigger_count += 1 elif hallstate and hallstate != last_hallstate: audio.play(reloadOpen) elif not hallstate and hallstate != last_hallstate: trigger_count = 0 audio.play(reloadClose)
1
1
Code issues
In Support
blaravie
More actions