Logan Ge Logan Ge - 2 months ago 6
Python Question

How to completely restart a python program

For a project, i used a Raspberry Pi (running dexter industries modified raspbian) and Brick Pi to run lego motors. I wroted a program with python and it works great and all, but i need the entire program to run repeatedly if the pressure sensor was not pressed. I tried calling the function sensorValue() (which detects whether the pressure sensor was being pushed) under while True:. But once i did that stuff became weird. It would just continue to repeat indefinitely and even if i pushed the sensor, the recurring 0 would turn to 1 but it wouldn't call the next function i need it to run.

Please help, this is my first time actually using python to write anything and i am a massive beginner so any help is GREATLY APPRECIATED.

Thanks Again

from BrickPi import *

BrickPiSetup()
BrickPi.MotorEnable[PORT_A] = 1
BrickPi.SensorType[PORT_4] = TYPE_SENSOR_TOUCH

BrickPiSetupSensors()

def sensorValue():
result = BrickPiUpdateValues()
if not result :
print BrickPi.Sensor[PORT_4]
time.sleep(.01)
if BrickPi.Sensor[PORT_4] == 0:

def programBody():

print ("program rest/pause")
BrickPi.MotorSpeed[PORT_A] = 0
BrickPiUpdateValues()
time.sleep(3)

print ("reminder/alarm = 200 value")
BrickPi.MotorSpeed[PORT_A] = 200
ot = time.time()
while(time.time() - ot <3):
BrickPiUpdateValues()
time.sleep(.01)

print ("reminder/alarm = 125 value")
BrickPi.MotorSpeed[PORT_A] = 125
ot = time.time()
while(time.time() - ot <3):
BrickPiUpdateValues()
time.sleep(.01)

sensorValue() #I would put while True: here but...

if BrickPi.Sensor[PORT_4]:
print "program successfully initiatied"
programBody()

Answer

try this

import BrickPi,time

BrickPiSetup()
BrickPi.MotorEnable[PORT_A] = 1
BrickPi.SensorType[PORT_4] = TYPE_SENSOR_TOUCH

BrickPiSetupSensors()

z = 0
def mainprogram():
    print ("running")
    while x == 1:
        z = z + 1
        print ("the plate has been pressed for %s seconds" % z)
        time.sleep(1)

while True:
    time.sleep(.1)
    if BrickPi.Sensor[PORT_4]:
        print "program successfully initiatied"
        mainprogram()