Forum Comments

Ray Gun Low Battery indication?
In Support
Ray Gun Low Battery indication?
In Support
happy_paladin
Dec 21, 2021
I finally got around to building my ray gun. It is a Christmas present so I am finishing just in time! The M0 didn't have enough RAM for making code changes so I replaced it with an M4 so I could make these changes. I added the battery checking, comments to the code and also some more color modes. I also created two more Dempsy quotes to ask for more juice, but I am not sure where to put them. The battery code has two modes and is accessed by turning the potentiometer all the way counter-clockwise: If the battery_debug variable is set to 1 (debug mode) then it will light up one barrel LED for each 0.1V above 3.0V. I used this to see what ranges the ADC was seeing as I charged and discharged the battery. If the battery_debug variable is set to 0 (Normal mode) I made a green to yellow to orange to Red bar graph with the barrel LEDs to show the state of the battery. Only on power up does Dempsy warn the player of low batteries! I welcome more changes and improvements. I am not a software engineer and am hacking this code in the same sprit that it was written, simplicity and functionality above everything else. # Ray Gun Final Code 1/18/20 # Designed for "The Original" using Feather M0 Express # The Ray Gun Project # 10/18/21 - Added additional LED pulse modes - K. Neal # 12/18/21 - Battery Level changes. Needed to upgrade to M4 - K. Neal 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 # Neo Pixel Definitions bar_pin = board.D5 dial_pin = board.D6 bar_pixels = 15 # Number of LEDs on barrel dial_pixels = 3 # Number of LEDs on dial (Note both Dials driven together) RED = (255, 0, 0) YELLOW = (255, 255, 0) GREEN = (0, 255, 0) phase = 0 # Initialize phase for Wave lighting effects # Instatiate neopixel chains 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 = DigitalInOut(board.D10) enable.direction = Direction.OUTPUT enable.value = True # Set up audio channel/Open audio files and prep for playback audio = AudioOut(board.A0) startup = WaveFile(open("Startup.wav", "rb")) shoot = WaveFile(open("RayGunPew.wav", "rb")) empty = WaveFile(open("Empty.wav", "rb")) quote1 = WaveFile(open("Dempsey_1.wav", "rb")) quote2 = WaveFile(open("Dempsey_2.wav", "rb")) quote3 = WaveFile(open("Dempsey_3.wav", "rb")) reloadOpen = WaveFile(open("Reload_Open.wav", "rb")) reloadClose = WaveFile(open("Reload_Close.wav", "rb")) juice_soon = WaveFile(open("Juice_soon.wav", "rb")) out_of_juice = WaveFile(open("out_of_juice.wav", "rb")) # Trigger input instantiation trigger = DigitalInOut(board.D11) trigger.direction = Direction.INPUT trigger.pull = Pull.UP # Hall sensor input instantiation sensor = DigitalInOut(board.D12) sensor.direction = Direction.INPUT sensor.pull = Pull.UP # Potentiometer input instantiation pot_read = AnalogIn(board.A2) # Battery Level input instantiation vbat_voltage = AnalogIn(board.VOLTAGE_MONITOR) # Instantiate and initialize variables barrel_open = False hallstate = barrel_open trigger_count = 0 # number of times trigger pulled, cleared by reloading ct = 0 # Determines what audio file to play when firing while empty num_shots = 10 # Number of shots before reload check = time.monotonic() last_check = check check2 = time.monotonic() last_check2 = check2 last_val = 0 # Unused battery_voltage = 3.3 pause = 0.05 # How often to check potentiometer and increment phase for wave animation battery_check = 0.5 # How often to check battery level battery_debug = 0 # Check ADC for Battery # Get battery voltage def get_voltage(pin): return (pin.value * 3.3) / 65536 * 2 # Barrel neopixel selection def get_mode(mode): if mode <= 50: bat_disp() # Battery barrel.show() elif mode > 50 and mode <= 100: # Yellow barrel.fill((255, 255, 0)) barrel.show() elif mode > 100 and mode <= 150: # Light Blue barrel.fill((15, 100, 175)) barrel.show() elif mode > 150 and mode <= 200: # Green barrel.fill((0, 255, 0)) barrel.show() elif mode > 200 and mode <= 250: # Red barrel.fill((255, 0, 0)) barrel.show() elif mode > 250 and mode <= 300: green_wave() elif mode > 300 and mode <= 350: lightblue_wave() elif mode > 350 and mode <= 400: red_wave() elif mode > 400 and mode <= 450: blue_wave() else: white_wave() # def bat_disp(): if battery_debug: barrel.fill((0, 0, 0)) # Turn off all leds for i in range(0, 14, 1): if i < battery_voltage * 10 - 30: # From 3.0V to 4.5V 0.1V increment barrel[i] = (0, 255, 0) # Green else: if battery_voltage > 3.5: # All Green barrel.fill((0, 255, 0)) elif battery_voltage > 3.45 and battery_voltage <= 3.5: barrel.fill((0, 255, 0)) # All Green barrel[4] = (0, 0, 0) # Turn off top layer barrel[5] = (0, 0, 0) barrel[14] = (0, 0, 0) elif battery_voltage > 3.4 and battery_voltage <= 3.45: barrel.fill((255, 255, 0)) # Yellow barrel[4] = (0, 0, 0) # Turn off top two layers layer barrel[5] = (0, 0, 0) barrel[14] = (0, 0, 0) barrel[3] = (0, 0, 0) barrel[6] = (0, 0, 0) barrel[13] = (0, 0, 0) elif battery_voltage > 3.3 and battery_voltage <= 3.4: barrel.fill((0, 0, 0)) # Turn off all LEDS barrel[0] = (255, 128, 0) # Turn bottom two rows Orange barrel[9] = (255, 128, 0) barrel[10] = (255, 128, 0) barrel[1] = (255, 128, 0) barrel[8] = (255, 128, 0) barrel[11] = (255, 128, 0) else: # battery voltage < 3.3 barrel.fill((0, 0, 0)) # Turn off all LEDS barrel[0] = (255, 0, 0) # Turn bottom row red barrel[9] = (255, 0, 0) barrel[10] = (255, 0, 0) # Blue Wave barrel effect 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() # time.sleep(0.002) # White Wave barrel effect 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() # time.sleep(0.002) # Green Wave barrel effect def green_wave(): for p in range(bar_pixels): GREENW = int(abs(math.sin((p - phase) * 3.14 / 18) * 235)) + 15 barrel[p] = (0, GREENW, 0) barrel.show() # time.sleep(0.002) # Green Wave barrel effect def red_wave(): for p in range(bar_pixels): REDW = int(abs(math.sin((p - phase) * 3.14 / 18) * 235)) + 15 barrel[p] = (REDW, 0, 0) barrel.show() # time.sleep(0.002) # Light Blue Wave barrel effect def lightblue_wave(): for p in range(bar_pixels): LBLUE = int(abs(math.sin((p - phase) * 3.14 / 18) * 235)) + 15 LIGHT = int(abs(math.sin((p - phase) * 3.14 / 18) * 50)) barrel[p] = (LIGHT, LIGHT, LBLUE) barrel.show() # time.sleep(0.002) # Set dial LEDs def dial_disp(): dial[0] = RED dial[1] = YELLOW dial[2] = GREEN dial.show() # Play sounds for empty ray gun 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) # Startup Light animation 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() # Set dial LED's dial_disp() # Startup audio.play(startup) battery_voltage = get_voltage(vbat_voltage) if battery_voltage < 3.4: audio.play(juice_soon) if battery_voltage < 3.2: audio.play(out_of_juice) # Main loop while True: if audio.playing: while audio.playing: pass check = time.monotonic() check2 = time.monotonic() delta = abs(check - last_check) delta2 = abs(check2 - last_check2) if delta >= pause: reading = pot_read.value val = int((reading * 500) / 65535) get_mode(val) phase += 1 last_check = check if delta2 >= battery_check: battery_voltage = get_voltage(vbat_voltage) last_check2 = check2 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 < num_shots: 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
0
Ray Gun Low Battery indication?
In Support
Ray Gun Low Battery indication?
In Support
happy_paladin
Apr 22, 2021
1
0
Ray Gun Low Battery indication?
In Support
h
happy_paladin
More actions