Due to high demand expect some shipping delays at this time, orders may not ship for 3-4 business days. On MLK Day no orders will be shipped.
0

How to set a code to run for certain time
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

How to set a code to run for certain time

by Ilnowstuffandbbq on Tue Nov 30, 2021 6:48 pm

Hi all! First project with Gemma and Circuit python. A little back story, I have a daughter that struggles with anxiety at night and so I am developing a mindfulness breathing timer that is on for 0.5 seconds and then off for 5 seconds. I would like for it to repeat for 10 minutes and then stop. I have tried adding a delay and a stop.time and a time.sleep but it doesn't seem to be working. The code is taken from the Mindfulness Bracelet tutorial. Any help is appreciated.!
Code: Select all | TOGGLE FULL SIZE
import time
import board
from digitalio import DigitalInOut, Direction

# vibrating disc mini motor disc connected on D2
vibrating_disc = DigitalInOut(board.D1)
vibrating_disc.direction = Direction.OUTPUT

on_time = .5     # Vibration motor run time, in seconds
interval = 2   # Time between reminders, in seconds

start_time = time.monotonic()

while True:

    timer = time.monotonic() - start_time

    if timer >= interval and timer <= (interval + on_time):
        vibrating_disc.value = True

    elif timer >= (interval + on_time):
        vibrating_disc.value = False
        start_time = time.monotonic()
Last edited by dastels on Tue Nov 30, 2021 7:56 pm, edited 1 time in total.
Reason: Add code block

Ilnowstuffandbbq
 
Posts: 1
Joined: Tue Nov 30, 2021 12:26 pm

Re: How to set a code to run for certain time

by dastels on Tue Nov 30, 2021 8:57 pm

What's it doing and/or not doing?

From the look of the code it should buzz for half a second, be quiet for 2 seconds, and repeat.

What you need to add is a way to have it go quiet after 10 minute, as well as a way to start the 10 minute buzz-pause cycle.

Dave

dastels
 
Posts: 8419
Joined: Tue Oct 20, 2015 3:22 pm

Please be positive and constructive with your questions and comments.