0

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!

1 Answer 1

2

How about using a config file to store the last run script:

scripta:

# scripta

import json

abort = False
# 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')

# write config
with open('config.json', 'w') as outfile:
    json.dump(data, outfile, sort_keys=True, indent=4)

scriptb:

# scriptb

import json

abort = False
# 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('turn motor anti-clockwise')

# write config
with open('config.json', 'w') as outfile:
    json.dump(data, outfile, sort_keys=True, indent=4)
10
  • thanks. This sounds like what I am looking for. I edited the question to hopefully make it clearer but it seems that what you have provided should do the job. Just need to study it a little as I'm not familiar with the procedure. If it works I will mark your answer as the solution. Thanks!
    – Nick C
    Jan 5, 2019 at 16:55
  • 1
    How does the remote button work - is it connected to the Pi?
    – CoderMike
    Jan 5, 2019 at 16:57
  • Yes it is. The idea is to prevent anything happening if the wrong button is pressed. It also solves the problem of double presses though I suspect that latter could be achieved within lirc itself.
    – Nick C
    Jan 5, 2019 at 17:02
  • I will mark this as solved anyway as I'm sure it is. Just not sure I will have the chance to test it today.
    – Nick C
    Jan 5, 2019 at 17:03
  • 2
    So I assume there are 2 buttons - why not just use one python script - would be much simpler? If so post your existing code.
    – CoderMike
    Jan 5, 2019 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.