Dave, going back at your last suggestion regarding the code, I changed it as recommended (sleep only when timer < interval), and I think that fixed the first sleep. But maybe I still have problems with the second sleep, the one after the buzzes and the button, until the end of the 30 minutes slice.dastels wrote: ↑Fri Sep 02, 2022 5:33 pmWhen the loop starts (after updating the switch) you compute the value for timer based on the current elapsed time (LINE 1). Then you sleep for interval seconds (LINE 2). After returning from the sleep (interval seconds later) you use the value of timer that was computed interval seconds in the past.Code: Select all
while True: # loop switch.update() # all'inizio di ogni loop controlla il pulsante timer = time.monotonic() - start_time // LINE 1 time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + interval) # NUOVO alarm.light_sleep_until_alarms(time_alarm) # NUOVO // LINE 2 # definisce timer come il tempo trascorso in ogni loop if timer >= interval and timer <= (interval + buzz): // LINE 3
Looking at it more, you are unconditionally sleeping for interval seconds each time through the loop. That's probably not what you want. You probably want that only when timer < interval.
Dave
I post the current code.
What I am currently getting is only the first set of buzzes (it does only 3 buzzes instead of 4), it records regularly my button input, but then it doesn't seem to do anything else.
I tried to solve this by myself for several days, I broke down and played each step of the loop, but could not find the bug...
Code: Select all
import time
import board
import random
import alarm # NUOVO
from digitalio import DigitalInOut, Direction, Pull
from adafruit_debouncer import Button
pulsante = DigitalInOut(board.A0)
pulsante.direction = Direction.INPUT
pulsante.pull = Pull.UP
vibrating_disc = DigitalInOut(board.A2)
vibrating_disc.direction = Direction.OUTPUT
switch = Button(pulsante)
buzz = 0.3 # buzz time
pause = 1.7 # pausa tra i buzz
interval = random.randint(1, 1732) # 8" buzz, 60" attesa pulsante e complemento a 1800
contatore = 1 # serve per contare i buzz nel file
print_contatore = repr(contatore) # converte variabile in stringa stampabile
delta = 1800 - (interval + 4 * buzz + 3 * pause + 60) # NUOVO
start_time = time.monotonic() # definisce start_time come il tempo attuale
while True: # loop
switch.update() # all'inizio di ogni loop controlla il pulsante
timer = time.monotonic() - start_time
if timer <= interval:
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + interval)
alarm.light_sleep_until_alarms(time_alarm)
elif timer > interval and timer <= (interval + buzz):
vibrating_disc.value = True # buzz on
elif timer >= (interval + buzz) and timer <= (interval + buzz + pause):
vibrating_disc.value = False # buzz off
elif timer >= (interval + buzz + pause) and \
timer <= (interval + buzz + pause + buzz):
vibrating_disc.value = True
elif timer >= (interval + buzz + pause + buzz) and \
timer <= (interval + buzz + pause + buzz + pause):
vibrating_disc.value = False
elif timer >= (interval + buzz + pause + buzz + pause) and \
timer <= (interval + buzz + pause + buzz + pause + buzz):
vibrating_disc.value = True
elif timer >= (interval + buzz + pause + buzz + pause + buzz) and \
timer <= (interval + buzz + pause + buzz + pause + buzz + pause):
vibrating_disc.value = False
elif timer >= (interval + buzz + pause + buzz + pause + buzz + pause) and \
timer <= (interval + buzz + pause + buzz + pause + buzz + pause + buzz):
vibrating_disc.value = True
elif timer >= (interval + buzz + pause + buzz + pause + buzz + pause + buzz) and \
timer < (interval + buzz + pause + buzz + pause + buzz + pause + buzz + 60):
vibrating_disc.value = False
if switch.short_count == 1:
file1 = open("mindfulness_log.txt", "a")
file1.write(print_contatore + "ok ")
file1.close()
elif switch.short_count == 2:
file1 = open("mindfulness_log.txt", "a")
file1.write(print_contatore + "no ")
file1.close()
elif timer >= (interval + buzz + pause + buzz + pause + buzz + pause + buzz + 60) \
and timer < 1800:
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + delta)
alarm.light_sleep_until_alarms(time_alarm) # NUOVO
elif timer >= 1800: # aggiornamento contatori per prossimo loop
start_time = time.monotonic()
interval = random.randint(1, 1732)
contatore = (contatore + 1)
print_contatore = repr(contatore) # converte variabile in stringa stampabile
delta = 1800 - (interval + 4 * buzz + 3 * pause + 60) # NUOVO