I have the following two Python scripts one sends a motor forward while the other script sends it backwards. Each script is operated by it's own button on a remote.
Script A
import RPi.GPIO as GPIO
from time import sleep
import json
GPIO.setmode(GPIO.BOARD)
GPIO.setup(22,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(19, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
abort = False
counter = 0
# read config
try:
with open('config.json') as readfile:
data = json.load(readfile)
if data['lastrun'] == 'scripta':
print('scripta already run')
abort = True
except:
pass
data = {'lastrun': 'scripta'}
if abort:
quit()
print('turn motor clockwise')
pwm=GPIO.PWM(23, 100)
pwm.start(0)
GPIO.output(19, False)
GPIO.output(21, True)
pwm.ChangeDutyCycle(35)
GPIO.output(23, True)
sleep (0.5)
while True:
input_state = GPIO.input(22)
if input_state == False:
counter = counter + 1
print counter
if counter == 2:
GPIO.output(23,GPIO.LOW)
exit()
GPIO.cleanup()
sleep (0.3)
pwm.stop()
# write config
with open('config.json', 'w') as outfile:
json.dump(data, outfile, sort_keys=True, indent=4)
GPIO.cleanup()
Script B
from time import sleep
import RPi.GPIO as GPIO
import json
GPIO.setmode(GPIO.BOARD)
GPIO.setup(22,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(19,GPIO.OUT)
GPIO.setup(21,GPIO.OUT)
GPIO.setup(23,GPIO.OUT)
abort = False
counter = 0
# read config
try:
with open('config.json') as readfile:
data = json.load(readfile)
if data['lastrun'] == 'scriptb':
print('scriptb already run')
abort = True
except:
pass
data = {'lastrun': 'scriptb'}
if abort:
quit()
print('blind going up')
GPIO.output(19,GPIO.HIGH)
GPIO.output(21,GPIO.LOW)
GPIO.output(23,GPIO.HIGH)
while True:
input_state = GPIO.input(22)
if input_state == False:
counter = counter + 1
print counter
if counter == 2:
GPIO.output(23,GPIO.LOW)
exit()
GPIO.cleanup()
sleep (0.3)
# write config
with open('config.json', 'w') as outfile:
json.dump(data, outfile, sort_keys=True, indent=4)
GPIO.cleanup()
The scripts include code designed to prevent a script running twice consecutively. Having run script A when I try to run it again it aborts as it should. Then I run script B which is fine however it doesn't abort on a second button press for some reason indicating that perhaps it isn't writing to the file as it should.
Many thanks!