I'm currently trying to write a program for a raspberry pi in Python 3 to control a set of linear actuators situated on a table that will both change the length of the legs and allow for the table top to be angled. One of the features of the table is an automatic leveling feature to ensure that the tabletop is is always level and will self correct if it detects it is not by pulling data from an onboard accelerator. The autolevel feature works perfectly fine however I am having difficulty programming on and off conditions. Ideally typing a(1) in the console will call the autolevel function and cause it to run continuously until typing a(0) stops it. I managed to get the function to run by typing a(1) but once that command is accepted the program stops listening for further commands and loops endlessly. The code in its current state is posted bellow.
def a(n):
while n== 1:
acceleration = sense.get_accelerometer_raw()
x = acceleration['x']
y = acceleration['y']
z = acceleration['z']
x=round(x, 3)
y=round(y, 3)
z=round(z, 3)
if x >0.00:
GPIO.output(12,0)
GPIO.output(13,1)#leg 1 retract
GPIO.output(15,0)
GPIO.output(29,1)#leg 2 retract
GPIO.output(31,1)
GPIO.output(32,0)#leg 3 extend
GPIO.output(33,1)
GPIO.output(36,0)#leg 4 extend
sense.show_message('Correcting forward tilt')
elif x <0.00:
GPIO.output(12,1)
GPIO.output(13,0)#leg 1 extend
GPIO.output(15,1)
GPIO.output(29,0)#leg 2 extend
GPIO.output(31,0)
GPIO.output(32,1)#leg 3 retract
GPIO.output(33,0)
GPIO.output(36,1)#leg 4 retract
sense.show_message('Correcting rearward tilt')
else:
if y >0.00:
GPIO.output(12,0)
GPIO.output(13,1)#leg 1 retract
GPIO.output(15,1)
GPIO.output(29,0)#leg 2 extend
GPIO.output(31,0)
GPIO.output(32,1)#leg 3 retract
GPIO.output(33,1)
GPIO.output(36,0)#leg 4 extend
sense.show_message('Correcting right side tilt')
elif y <0.00:
GPIO.output(12,1)
GPIO.output(13,0)#leg 1 extend
GPIO.output(15,0)
GPIO.output(29,1)#leg 2 retract
GPIO.output(31,1)
GPIO.output(32,0)#leg 3 extend
GPIO.output(33,0)
GPIO.output(36,1)#leg 4 retract
sense.show_message('Correcting left side tilt')
else:
sense.show_message("No tilt to correct")
print ("Stand currently level, standing by for reactive adjustment.")