samedi 15 août 2020

How to measure if a value stayed same for a period of time?

I'm making a smart bicycle backlight with Raspberry Pi and a SenseHat on top of it.

I'm measuring the output values of the SenseHat's accelerometer. There are actually three values reported as x, y, z and what I'm trying to know is whether my bicycle has been stationary for let's say 15 seconds or more. So if these values have been staying the same for over 15 seconds I'll turn off the backlight. And then if they start changing and stay like that for over 15 seconds, I want it to engage and start functioning again.

So far I've implemented strobe effect that auto activates when the bicycle is in idle. A steering detection - I show arrow animations to left and right based on the object detection sensor i put near the handlebar. I also implemented a brake detection. Once the accelerometer detects the braking, i show full red light.

If you check the code, you can see it's a bit tricky since the whole thing is in while loop and I need to make this detection as an if condition and then add else below, put rest of my existing if conditions there (such as deacceleration or turning detection)

So how do I make Python measure certain values for 15 seconds without using time.delay and do things based on whether they change or stay the same?

while True:
    
    ser.flushInput()
    ser.flushOutput()
    
    x, y, z = sense.get_accelerometer_raw().values()
    x = round(x, 2)
    y = round(y, 2)
    z = round(z, 2)
    print("x=%s, y=%s, z=%s" % (x, y, z))
    
    input = ser.read() #serial input i'm getting from arduino, it tells me if my left or right steering sensors are triggerred.
    yon = input.decode("utf-8")
    int(yon)

    if (z > 0.20): #If deacceleration is detected
        fren() # brake function is called
    else: # if no breaking is detected...
        if (yon == "1"): #if left turn sensor triggered
            sag_ok() #show left turn animation on led matrix
        elif (yon =="2"): # if right turn sensor triggered
            sol_ok() #show right turn animation on led matrix
        else: #anything else
            strobe() #show strobe effect if nothing else is detected

Aucun commentaire:

Enregistrer un commentaire