-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
124 lines (94 loc) · 2.45 KB
/
code.py
File metadata and controls
124 lines (94 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from digitalio import DigitalInOut, Direction, Pull
import board
import time
print("Starting Bag Controller")
# Number of seconds to open the relay
RELAY_OPEN_SECONDS = 0.04
# Number of seconds between relays
RELAY_DELAY_SECONDS = 7
# Number of relays to iterate
NUMBER_OF_RELAYS = 5
ONBOARD_LED_PIN = board.GP25
# Pin mappings for Pico-relay B board
# Docs: https://www.waveshare.com/wiki/Pico-Relay-B
PICO_RELAY_B_PIN_CONFIG = [
board.GP21, # Channel 1
board.GP20, # Channel 2
board.GP19, # Channel 3
board.GP18, # Channel 4
board.GP17, # Channel 5
board.GP16, # Channel 6
board.GP15, # Channel 7
board.GP14, # Channel 8
]
# Normal pin mappings
VANILLA_PIN_CONFIG = [
board.GP0,
board.GP1,
board.GP2,
board.GP3,
board.GP4,
board.GP5,
board.GP6,
board.GP7,
board.GP8,
board.GP9,
board.GP10,
board.GP11,
board.GP12,
board.GP13,
board.GP14,
board.GP15,
board.GP16,
board.GP17,
board.GP18,
board.GP19,
board.GP20,
board.GP21,
board.GP22,
board.GP26,
board.GP27,
board.GP28
]
# Set pin mapping
PIN_CONFIG = PICO_RELAY_B_PIN_CONFIG
def setup_output_pin(pin):
output = DigitalInOut(pin)
output.direction = Direction.OUTPUT
return output
def setup_pins(pins):
output_pins = []
for x in range(NUMBER_OF_RELAYS):
pin = pins[x]
output = setup_output_pin(pin)
output_pins.append(output)
return output_pins
onboard_led = setup_output_pin(ONBOARD_LED_PIN)
relays = setup_pins(PIN_CONFIG)
def print_status(relay_num, status, seconds):
statusStr = "on" if status else "off"
format_str = "Relay {relay_num} {status} ({seconds} seconds)"
msg = format_str.format(relay_num=relay_num,
status=statusStr, seconds=seconds)
print(msg)
def toggle_relay(relay):
print_status(relay_number, True, RELAY_OPEN_SECONDS)
relay.value = False
time.sleep(RELAY_OPEN_SECONDS)
print_status(relay_number, False, RELAY_DELAY_SECONDS)
relay.value = True
# Turn off all relays
for relay in relays:
relay.value = True
# Delay first run
time.sleep(RELAY_DELAY_SECONDS)
while True:
print("Starting Relay Sequence")
relay_number = 1
for relay in relays:
onboard_led.value = False
toggle_relay(relay)
onboard_led.value = False
time.sleep(RELAY_DELAY_SECONDS)
relay_number += 1
print("Finished Relay Sequence")