From 053512dc55de6dfe20a6339ee22a6fa6f11ac2f5 Mon Sep 17 00:00:00 2001 From: baidarka Date: Sat, 9 Dec 2023 19:06:06 +0100 Subject: [PATCH] day night bypass (#16) * functional grounding test * added day and night cycle * daily cycle --- README.md | 23 ++++++++------- slag-in-de-rondte.py | 70 +++++++++++++++++++++++--------------------- 2 files changed, 48 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 22a1e20..d7e3fb3 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,8 @@ E.g. Thonny or VSCode. For lookenspeepers, build this project and watschen der Blinkenlichten! -- Wire up your RaspberryPi Pico according to the Fritzing diagram. +- Wire up your RaspberryPi Pico according to the Fritzing diagram +- Optionally remove the resistor on GPIO2 to use an 8 minutes day/night cycle - Save the script 'slag-in-de-rondte.py' to your Pico as 'main.py' - Restart your Pico @@ -78,16 +79,16 @@ A hollow pipe tool (Dutch: holpijp) is only needed if you want to punch holes in ![Hollow pipe tool](/img/holpijp.png) -| pin | pin name | in/out | description | -| --- | --- | --- | --- | -| 3 | GND | | ground | -| 4 | GPIO 2 | in | (future use) | -| 21 | GPIO 16 | out 3.3V | LED Texel | -| 22 | GPIO 17 | out 3.3V | LED Vlieland | -| 24 | GPIO 18 | out 3.3V | LED Terschelling | -| 25 | GPIO 19 | out 3.3V | LED Ameland | -| 26 | GPIO 20 | out 3.3V | LED Schiermonnikoog | -| 39 | VSYS | 5V | LED Red + Green Harlingen | +| pin | pin name | in/out | description | +| --- | --- | --- | --- | +| 3 | GND | | ground | +| 4 | GPIO 2 | in | disables night and day cycle | +| 21 | GPIO 16 | out 3.3V | LED Texel | +| 22 | GPIO 17 | out 3.3V | LED Vlieland | +| 24 | GPIO 18 | out 3.3V | LED Terschelling | +| 25 | GPIO 19 | out 3.3V | LED Ameland | +| 26 | GPIO 20 | out 3.3V | LED Schiermonnikoog | +| 39 | VSYS | 5V | LED Red + Green Harlingen | CAUTION: Drawing too much power from the board may damage the board. diff --git a/slag-in-de-rondte.py b/slag-in-de-rondte.py index 63c1de4..e3be4cd 100644 --- a/slag-in-de-rondte.py +++ b/slag-in-de-rondte.py @@ -27,11 +27,10 @@ ameland = PWM(Pin(19), f) schiermonnikoog = PWM(Pin(20), f) -# WIP: Optionally, run without day&night cycle -always_on = Pin(2, Pin.IN) - -# Optionally, grab the temperature of your board -adc = machine.ADC(4) +# Optionally, run without night_n_day cycle +# - pin not grounded = night and day cycle of 8 minutes +# - pin grounded = continuously use LEDs +night_n_day = Pin(2, Pin.IN, Pin.PULL_UP) # Coroutine: Flash a LED, once async def flash(pwm): @@ -92,7 +91,7 @@ async def isophase(pwm, d): pwm.duty_u16(0) await uasyncio.sleep_ms(round(d/2) * 1000) -# Corouting: Fade out a LED +# Corouting: Fade out a LED, to stop a cycle for the day async def fade_out(pwm): # Get the current duty cycle and fade out from there. @@ -189,46 +188,49 @@ async def main(): # Half a day contains roughly a tidal cycle, high water + low water # In the board game this cycle takes 8 minutes (480 seconds) tidal_cycle = 480 + + # Switch on all lighthouses (night) + lighthouses = [ + uasyncio.create_task(characteristics_texel(texel)), + uasyncio.create_task(characteristics_vlieland(vlieland)), + uasyncio.create_task(characteristics_terschelling(terschelling)), + uasyncio.create_task(characteristics_ameland(ameland)), + uasyncio.create_task(characteristics_schiermonnikoog(schiermonnikoog)) + ] + while True: - - # A night of sailing starts. Switch on all five lighthouses - print('Night') - task_tx = uasyncio.create_task(characteristics_texel(texel)) - task_vl = uasyncio.create_task(characteristics_vlieland(vlieland)) - task_ts = uasyncio.create_task(characteristics_terschelling(terschelling)) - task_am = uasyncio.create_task(characteristics_ameland(ameland)) - task_sch = uasyncio.create_task(characteristics_schiermonnikoog(schiermonnikoog)) - + # Leave lighthouses on for the night await uasyncio.sleep(tidal_cycle) - # Is Pin4 grounded? - # WORK IN PROGRESS; this seems like a bad approach - if always_on == 0: - # pin is grounded - print('always on!') + # Enable night_n_day cycle? (is Pin grounded?) + if night_n_day.value() == 0: + print('pin grounded: no night_n_day') + continue else: - print('night & day') - - # Print the temperature. (keep an eye on the board) - ADC_voltage = adc.read_u16() * (3.3/65536) - temp_celcius = 27 - (ADC_voltage - 0.706)/0.001721 - print('Temperature: {}'.format(round(temp_celcius))) + print('pin not grounded: night_n_day') # After a night of sailing comes... a day of sailing :) # Switch off all five lighthouses - print('Day') - task_tx.cancel() - task_vl.cancel() - task_ts.cancel() - task_am.cancel() - task_sch.cancel() - + print('day') + for task in lighthouses: + task.cancel() + # Run the GC gc.collect() - ## Leave lighthouses off for the day + # Leave lighthouses off for the day await uasyncio.sleep(tidal_cycle) + # Switch the lighthouses on for the night + print('night') + lighthouses = [ + uasyncio.create_task(characteristics_texel(texel)), + uasyncio.create_task(characteristics_vlieland(vlieland)), + uasyncio.create_task(characteristics_terschelling(terschelling)), + uasyncio.create_task(characteristics_ameland(ameland)), + uasyncio.create_task(characteristics_schiermonnikoog(schiermonnikoog)) + ] + # Start event loop uasyncio.run(main()) \ No newline at end of file