Skip to content

Commit

Permalink
Randomized stop and start (#18)
Browse files Browse the repository at this point in the history
* added random delay for start and stop

* docs night-n-day
  • Loading branch information
baidarka authored Dec 10, 2023
1 parent 053512d commit de1f6d2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ E.g. Thonny or VSCode.
For lookenspeepers, build this project and watschen der Blinkenlichten!

- Wire up your RaspberryPi Pico according to the Fritzing diagram
- Optionally remove the resistor on GPIO2 to use an 8 minutes day/night cycle
- (optional: ground GPIO2 with a resistor to bypass the 8 minutes day/night cycle and be 'always on')
- Save the script 'slag-in-de-rondte.py' to your Pico as 'main.py'
- Restart your Pico

Expand Down Expand Up @@ -61,16 +61,16 @@ Tip: check for MicroPython linting
## Parts, wiring, pinout

| item | quantity |
| --- | --- |
| RaspberryPi Pico H | 1 |
| LED white (5mm) | 5 |
| LED red (5mm) | 1 |
| LED green (5mm) | 1 |
| resistor 1kΩ | 7 |
| breadboard | 1 |
| hollow pipe tool 4mm | 1 |

An [RPI Pico](https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html)
| --- | --- |
| RaspberryPi Pico H | 1 |
| LED white (5mm) | 5 |
| LED red (5mm) | 1 |
| LED green (5mm) | 1 |
| resistor 1kΩ | 7 |
| breadboard | 1 |
| hollow pipe tool 4mm | 1 |

A [RPI Pico](https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html)
costs about €6.- at [Kiwi](https://www.kiwi-electronics.com/nl/raspberry-pi-pico-h-10939),
a breadboard (400 points) costs about €4.-.
A hollow pipe tool (Dutch: holpijp) is only needed if you want to punch holes in your game board, to fit the LEDs.
Expand All @@ -82,7 +82,7 @@ A hollow pipe tool (Dutch: holpijp) is only needed if you want to punch holes in
| pin | pin name | in/out | description |
| --- | --- | --- | --- |
| 3 | GND | | ground |
| 4 | GPIO 2 | in | disables night and day cycle |
| 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 |
Expand Down
Binary file modified img/lighthouse-leds-1.fzz
Binary file not shown.
Binary file removed img/lighthouse-leds-rpi-pico_bb-old.png
Binary file not shown.
Binary file modified img/lighthouse-leds-rpi-pico_bb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 34 additions & 4 deletions slag-in-de-rondte.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from time import sleep
import gc
import math
import random
import time
import uasyncio

Expand All @@ -32,7 +33,7 @@
# - pin grounded = continuously use LEDs
night_n_day = Pin(2, Pin.IN, Pin.PULL_UP)

# Coroutine: Flash a LED, once
# Coroutine
async def flash(pwm):
"""Fade a LED on and off, using Pulse Width Modulation
Expand Down Expand Up @@ -60,7 +61,7 @@ async def flash(pwm):
pwm.duty_u16(0)
await uasyncio.sleep_ms(1600)

# Corouting: Isophase a LED
# Coroutine
async def isophase(pwm, d):
"""Isophase a LED on and off, using Pulse Width Modulation
Expand Down Expand Up @@ -91,9 +92,10 @@ async def isophase(pwm, d):
pwm.duty_u16(0)
await uasyncio.sleep_ms(round(d/2) * 1000)

# Corouting: Fade out a LED, to stop a cycle for the day
# Coroutine
async def fade_out(pwm):

"""Fade out a LED, to stop the current task
"""
# Get the current duty cycle and fade out from there.
current_duty_cycle = pwm.duty_u16()
current_duty_cycle_fraction = (current_duty_cycle // (duty_max/100))
Expand All @@ -104,8 +106,18 @@ async def fade_out(pwm):
await uasyncio.sleep_ms(12)
pwm.duty_u16(0)

# Coroutine
async def random_delay():
"""Random delay of at most 5 seconds
"""
max_msec = 5000
delay_ms = math.floor(random.random() * max_msec)
await uasyncio.sleep_ms(delay_ms)

# Coroutine: characteristics of light: Texel FL(2) W 10s
async def characteristics_texel(texel):
await random_delay()

while True:
try:
time_begin = time.ticks_ms()
Expand All @@ -115,25 +127,33 @@ async def characteristics_texel(texel):
await uasyncio.sleep_ms(10000 - time_elapsed)

except (uasyncio.CancelledError):
await random_delay()

# Switch off
await fade_out(texel)
print('Task Texel cancelled')
raise

# Coroutine: characteristics of light: Vlieland ISO W 4s
async def characteristics_vlieland(vlieland):
await random_delay()

while True:
try:
await isophase(vlieland, 4)

except (uasyncio.CancelledError):
await random_delay()

# Switch off
await fade_out(vlieland)
print('Task Vlieland cancelled')
raise

# Coroutine: characteristics of light: Terschelling FL(1) W 5s
async def characteristics_terschelling(terschelling):
await random_delay()

while True:
try:
time_begin = time.ticks_ms()
Expand All @@ -142,13 +162,17 @@ async def characteristics_terschelling(terschelling):
await uasyncio.sleep_ms(5000 - time_elapsed)

except (uasyncio.CancelledError):
await random_delay()

# Switch off
await fade_out(terschelling)
print('Task Terschelling cancelled')
raise

#Coroutine: characteristics of light: Ameland FL(3) W 15s
async def characteristics_ameland(ameland):
await random_delay()

while True:
try:
time_begin = time.ticks_ms()
Expand All @@ -159,13 +183,17 @@ async def characteristics_ameland(ameland):
await uasyncio.sleep_ms(15000 - time_elapsed)

except (uasyncio.CancelledError):
await random_delay()

# Switch off
await fade_out(ameland)
print('Task Ameland cancelled')
raise

# Coroutine: characteristics of light: Schiermonnikoog FL(4) W 20s
async def characteristics_schiermonnikoog(schiermonnikoog):
await random_delay()

while True:
try:
time_begin = time.ticks_ms()
Expand All @@ -177,6 +205,8 @@ async def characteristics_schiermonnikoog(schiermonnikoog):
await uasyncio.sleep_ms(20000 - time_elapsed)

except (uasyncio.CancelledError):
await random_delay()

# Switch off
await fade_out(schiermonnikoog)
print('Task Schiermonnikoog cancelled')
Expand Down

0 comments on commit de1f6d2

Please sign in to comment.