python-3.xmotordriver

Python3 SyntaxError: expected an indented block


I want my program to be : when I enter '3' key to from the main menu selection > 'N' key to return back to the main menu selection.

But now It state "the expected an indented block" at

def Menu():

This is my full coding :

import RPi.GPIO as GPIO
import Slush
import math
import time
import sys

from inputs import get_key
from time import sleep


b = Slush.sBoard()
m = [Slush.Motor(0), Slush.Motor(1), Slush.Motor(2), Slush.Motor(3)]

m[0].setMaxSpeed(150)
m[1].setMaxSpeed(150)
m[2].setMaxSpeed(250)
m[3].setMaxSpeed(150)

m[0].setCurrent(150, 150, 150, 150)
m[1].setCurrent(100, 100, 100, 100)
m[2].setCurrent(150, 150, 150, 150)
m[3].setCurrent(100, 100, 100, 100)


GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 50)
pwm.start(0)
GPIO.output(18, 1)


def Menu():
print("Please Enter Number From List Below:")
print("1: Start Program")
print("2: Control Manually")
print("3: Exit Program")
inputkey = int(input("Enter a number & Press Enter:"))


#START PROGRAM
if inputkey == 1:
    print("Press Home Key To Start Program")
#def_StartProgram()

    while 1:
        events = get_key()
        for event in events:
                if event.code == 'KEY_HOME':
                    print("PROGRAM START")
                    print("\nMotor0 Position is", m[0].getPosition())
                    print("\nMotor1 Position is", m[1].getPosition())
                    print("\nMotor2 Position is", m[2].getPosition())
                    print("\nMotor3 Position is", m[3].getPosition())
                    m[1].goTo(-10000)
                    time.sleep(3)
                    print("\nMotor1 Position is", m[1].getPosition())


#CONTROL MANUAL
if inputkey == 2:
    print("Manual Mode")

    while 1:
        events = get_key()
        for event in events:

            if event.code == 'KEY_Q':
                value = event.state
                if value == 1:
                    if not m[0].isBusy(): m[0].run(1, 35)
            if event.code == 'KEY_A':
                value = event.state
                if value == 1:
                    if not m[0].isBusy(): m[0].softStop()
            if event.code == 'KEY_Z':
                value = event.state
                if value == 1:
                    if not m[0].isBusy(): m[0].run(0, 35)



            if event.code == 'KEY_W':
                value = event.state
                if value == 1:
                    if not m[1].isBusy(): m[1].run(1, 20)
            if event.code == 'KEY_S':
                value = event.state
                if value == 1:
                    if not m[1].isBusy(): m[1].softStop()
            if event.code == 'KEY_X':
                value = event.state
                if value == 1:
                    if not m[1].isBusy(): m[1].run(0, 20)



            if event.code == 'KEY_E':
                value = event.state
                if value == 1:
                    if not m[2].isBusy(): m[2].run(1, 100)
            if event.code == 'KEY_D':
                value = event.state
                if value == 1:
                    if not m[2].isBusy(): m[2].softStop()
            if event.code == 'KEY_C':
                value = event.state
                if value == 1:
                    if not m[2].isBusy(): m[2].run(0, 100)




            if event.code == 'KEY_R':
                value = event.state
                if value == 1:
                    if not m[3].isBusy(): m[3].run(1, 10)
            if event.code == 'KEY_F':
                value = event.state
                if value == 1:
                    if not m[3].isBusy(): m[3].softStop()
            if event.code == 'KEY_V':
                value = event.state
                if value == 1:
                    if not m[3].isBusy(): m[3].run(0, 10)



            if event.code == 'KEY_1':
                value = event.state
                if value == 1:
                    pwm.ChangeDutyCycle(1)
                    sleep(0.5)
                    if not pwm.stop(): pwm.start(0)


            if event.code == 'KEY_3':
                value = event.state
                if value == 1:
                    if not pwm.ChangeDutyCycle(0): pwm.ChangeDutyCycle(99)

#EXIT PROGRAM
if inputkey == 3:
    print("Press 'Y/N' key = Exit/Back To Menu ")


    while 1:
        events = get_key()
        for event in events:

            if event.code == 'KEY_Y':
                sys.exit(0)
            if event.code == 'KEY_N':
                Menu()

This is how I want my program to work. FLOW CHART : enter image description here


Solution

  • You have missed indentation in the def Menu. Please try the following code, since I don't know you exact logic, so the program might not work as per your need, but you will get a fair idea on the logic.

    import RPi.GPIO as GPIO
    import Slush
    import math
    import time
    import sys
    
    from inputs import get_key
    from time import sleep
    
    
    b = Slush.sBoard()
    m = [Slush.Motor(0), Slush.Motor(1), Slush.Motor(2), Slush.Motor(3)]
    
    m[0].setMaxSpeed(150)
    m[1].setMaxSpeed(150)
    m[2].setMaxSpeed(250)
    m[3].setMaxSpeed(150)
    
    m[0].setCurrent(150, 150, 150, 150)
    m[1].setCurrent(100, 100, 100, 100)
    m[2].setCurrent(150, 150, 150, 150)
    m[3].setCurrent(100, 100, 100, 100)
    
    
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(18, GPIO.OUT)
    pwm = GPIO.PWM(18, 50)
    pwm.start(0)
    GPIO.output(18, 1)
    
    def Menu():
        while 1:    
            print("Please Enter Number From List Below:")
            print("1: Start Program")
            print("2: Control Manually")
            print("3: Exit Program")
            inputkey = int(input("Enter a number & Press Enter:"))
    
    
            #START PROGRAM
            if inputkey == 1:
                print("Press Home Key To Start Program")
            #def_StartProgram()
    
                while 1:
                    events = get_key()
                    for event in events:
                            if event.code == 'KEY_HOME':
                                print("PROGRAM START")
                                print("\nMotor0 Position is", m[0].getPosition())
                                print("\nMotor1 Position is", m[1].getPosition())
                                print("\nMotor2 Position is", m[2].getPosition())
                                print("\nMotor3 Position is", m[3].getPosition())
                                m[1].goTo(-10000)
                                time.sleep(3)
                                print("\nMotor1 Position is", m[1].getPosition())
    
    
            #CONTROL MANUAL
            if inputkey == 2:
                print("Manual Mode")
    
                while 1:
                    events = get_key()
                    for event in events:
    
                        if event.code == 'KEY_Q':
                            value = event.state
                            if value == 1:
                                if not m[0].isBusy(): m[0].run(1, 35)
                        if event.code == 'KEY_A':
                            value = event.state
                            if value == 1:
                                if not m[0].isBusy(): m[0].softStop()
                        if event.code == 'KEY_Z':
                            value = event.state
                            if value == 1:
                                if not m[0].isBusy(): m[0].run(0, 35)
    
    
    
                        if event.code == 'KEY_W':
                            value = event.state
                            if value == 1:
                                if not m[1].isBusy(): m[1].run(1, 20)
                        if event.code == 'KEY_S':
                            value = event.state
                            if value == 1:
                                if not m[1].isBusy(): m[1].softStop()
                        if event.code == 'KEY_X':
                            value = event.state
                            if value == 1:
                                if not m[1].isBusy(): m[1].run(0, 20)
    
    
    
                        if event.code == 'KEY_E':
                            value = event.state
                            if value == 1:
                                if not m[2].isBusy(): m[2].run(1, 100)
                        if event.code == 'KEY_D':
                            value = event.state
                            if value == 1:
                                if not m[2].isBusy(): m[2].softStop()
                        if event.code == 'KEY_C':
                            value = event.state
                            if value == 1:
                                if not m[2].isBusy(): m[2].run(0, 100)
    
    
    
    
                        if event.code == 'KEY_R':
                            value = event.state
                            if value == 1:
                                if not m[3].isBusy(): m[3].run(1, 10)
                        if event.code == 'KEY_F':
                            value = event.state
                            if value == 1:
                                if not m[3].isBusy(): m[3].softStop()
                        if event.code == 'KEY_V':
                            value = event.state
                            if value == 1:
                                if not m[3].isBusy(): m[3].run(0, 10)
    
    
    
                        if event.code == 'KEY_1':
                            value = event.state
                            if value == 1:
                                pwm.ChangeDutyCycle(1)
                                sleep(0.5)
                                if not pwm.stop(): pwm.start(0)
    
    
                        if event.code == 'KEY_3':
                            value = event.state
                            if value == 1:
                                if not pwm.ChangeDutyCycle(0): pwm.ChangeDutyCycle(99)
    
            #EXIT PROGRAM
            if inputkey == 3:
                print("Press 'Y/N' key = Exit/Back To Menu ")
    
                exit_flag = True
                while exit_flag:
                    events = get_key()
                    for event in events:
    
                        if event.code == 'KEY_Y':
                            sys.exit(0)
                        elif event.code == 'KEY_N':
                            exit_flag = False
                            break
    Menu()