Background: On a RP2040 chip on the Pico, using Micropython on the Python editor Mu.
Looking to program a chip that counts current cycles as a series of buttons are being pushed but one type (T1) is a normally closed switch and the other type (T2) is normally open. I have both in a big loop that tries to count them all at the same time but I am not sure if I need a timer on it or if this works continuously running, as well as how to write the micropython code to perform this action and am currently looking for guidance as I am new to python.
Example T1:
A1 = Pin(2, Pin.IN, Pin.PULL_UP)
countA1 = 0
while True:
if Pin.input(2) is False, then True:
countA1 = countA1 + 1
Example T2
B1 = Pin(15, Pin.IN, Pin.PULL_UP)
countB1 = 0
while True:
if Pin.input(15) is True:
countB1 = countB1 + 1
--but I need this to only count if the current is temporary and is not stuck on.--
Then I need all of this to print onto a screen to display the overall counts, until a reset button is pressed that will reset all the counters to 0.
I appreciate any and all guidance on how to approach this problem and improve this micropython code.
I have it set up as the posted code below but then realized after setting it up what was the main problem with the quality of buttons I am trying to test for cycled current and not simply if current goes through sometimes. Even if somebody knows a good guide to follow that is new-to-python-friendly as I only have past experience in R, MATLAB, and C. Thank you again!
import machine
import rp2
from machine import Pin
import time
A1 = Pin(2, Pin.IN, Pin.PULL_UP)
countA1 = 0
A2 = Pin(4, Pin.IN, Pin.PULL_UP)
countA2 = 0
A3 = Pin(5, Pin.IN, Pin.PULL_UP)
countA3 = 0
A4 = Pin(6, Pin.IN, Pin.PULL_UP)
countA4 = 0
A5 = Pin(7, Pin.IN, Pin.PULL_UP)
countA5 = 0
A6 = Pin(9, Pin.IN, Pin.PULL_UP)
countA6 = 0
A7 = Pin(10, Pin.IN, Pin.PULL_UP)
countA7 = 0
A8 = Pin(11, Pin.IN, Pin.PULL_UP)
countA8 = 0
A9 = Pin(12, Pin.IN, Pin.PULL_UP)
countA9 = 0
A10 = Pin(14, Pin.IN, Pin.PULL_UP)
countA10 = 0
B1 = Pin(15, Pin.IN, Pin.PULL_UP)
countB1 = 0
B2 = Pin(16, Pin.IN, Pin.PULL_UP)
countB2 = 0
B3 = Pin(17, Pin.IN, Pin.PULL_UP)
countB3 = 0
B4 = Pin(19, Pin.IN, Pin.PULL_UP)
countB4 = 0
B5 = Pin(20, Pin.IN, Pin.PULL_UP)
countB5 = 0
B6 = Pin(21, Pin.IN, Pin.PULL_UP)
countB6 = 0
B7 = Pin(22, Pin.IN, Pin.PULL_UP)
countB7 = 0
B8 = Pin(24, Pin.IN, Pin.PULL_UP)
countB8 = 0
B9 = Pin(25, Pin.IN, Pin.PULL_UP)
countB9 = 0
B10 = Pin(26, Pin.IN, Pin.PULL_UP)
countB10 = 0
RESET = Pin(27, Pin.IN, Pin.PULL_UP)
while True:
if Pin.input(2) is False:
countA1 = countA1 + 1
else:
countA1 = countA1
if Pin.input(4) is False:
countA2 = countA2 + 1
else:
countA2 = countA2
if Pin.input(5) is False:
countA3 = countA3 + 1
else:
countA3 = countA3
if Pin.input(6) is False:
countA4 = countA4 + 1
else:
countA4 = countA4
if Pin.input(7) is False:
countA5 = countA5 + 1
else:
countA5 = countA5
if Pin.input(9) is False:
countA6 = countA6 + 1
else:
countA6 = countA6
if Pin.input(10) is False:
countA7 = countA7 + 1
else:
countA7 = countA7
if Pin.input(11) is False:
countA8 = countA8 + 1
else:
countA8 = countA8
if Pin.input(12) is False:
countA9 = countA9 + 1
else:
countA9 = countA9
if Pin.input(14) is False:
countA10 = countA10 + 1
else:
countA10 = countA10
if Pin.input(15) is True:
countB1 = countB1 + 1
else:
countB1 = countB1
if Pin.input(16) is True:
countB2 = countB2 + 1
else:
countB2 = countB2
if Pin.input(17) is True:
countB3 = countB3 + 1
else:
countB3 = countB3
if Pin.input(19) is True:
countB4 = countB4 + 1
else:
countB4 = countB4
if Pin.input(20) is True:
countB5 = countB5 + 1
else:
countB5 = countB5
if Pin.input(21) is True:
countB6 = countB6 + 1
else:
countB6 = countB6
if Pin.input(22) is True:
countB7 = countB7 + 1
else:
countB7 = countB7
if Pin.input(24) is True:
countB8 = countB8 + 1
else:
countB8 = countB8
if Pin.input(25) is True:
countB9 = countB9 + 1
else:
countB9 = countB9
if Pin.input(26) is True:
countB10 = countB10 + 1
else:
countA10 = countA10
if Pin.input(27) is True:
print("Reseting counters")
break
print("A1:", countA1, " A2:", countA2,
" A3:", countA3, " A4:", countA4,
" A5:", countA5, " A6:", countA6,
" A7:", countA7, " A8:", countA8,
" A9:", countA9, " A10:", countA10,
" B1:", countB1, " B2:", countB2,
" B3:", countB3, " B4:", countB4,
" B5:", countB5, " B6:", countB6,
" B7:", countB7, " B8:", countB8,
" B9:", countB9, " B10:", countB10)
time.sleep(2)
Your code will count a button press every two seconds if you keep your button pressed. You just want to detect a falling edge on your button, i.e. an ON => OFF transition. For that you must memorize the last seen state somewhere. I believe your button shows an open state as 1 or True, so use something like
old_state = False
while True:
state = Pin.input(2)
if old_state == False and state == True: # detect falling edge
countA1 += 1
old_state = state # memorize last state
# ...
time.sleep(.01) # only a short delay do detect short button presses
For T2, you need to distinguish between short button presses and long-termm toggles. So you need to track the time between the state changes.
old_state = False
old_time = time.time() # get seconds since EPOCH (just an arbitrary point in history)
while True:
state = Pin.input(4)
if old_state == False and state == True: # detect falling edge
now = time.time() # get current time
delta = now - old_time
if delta <= 2: # duration of press was 2s or less?
countA2 += 1
if old_state == True and state == False: # detect rising edge
old_time = now
old_state = state # memorize last state
# ...
time.sleep(.01) # only a short delay do detect short button presses
Now if you hold the button for more than 2 seconds the counter will not be increased upon the release.