How to set a code to run for certain time

Wearable electronics: boards, conductive materials, and projects from Adafruit!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Ilnowstuffandbbq
 
Posts: 1
Joined: Tue Nov 30, 2021 12:26 pm

How to set a code to run for certain time

Post by Ilnowstuffandbbq »

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

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

User avatar
dastels
 
Posts: 15653
Joined: Tue Oct 20, 2015 3:22 pm

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

Post by dastels »

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

Locked
Please be positive and constructive with your questions and comments.

Return to “Wearables”