0

I have been working on designing an automotive turn signal device to integrate into a prototype device after development. Currently, I am attempting to write Python script which will use it's GPIO output pins to signal the pins of a micro controller, which will, based on input, decide which set of LED's to blink.

As most people know, there are three states in a automotive turn signal device, which I will describe as:

  • No_Turn
  • Left_Turn
  • Right_Turn

Most tutorials only describe how to get one set of LED's to blink, but that won't be good enough for this project. Not only do I need more than one set of LED's to blink, but also there is a time they don't need to blink.

To arrive at my algorithm, I tried to adjust the simple code for a typical blink LED sketch, yet now, I'm using the conditions of two GPIO input pins,GPIO 24 & 25,to determine output in the the loop

I need to loop based on the conditions of two different inputs. Here is the code I am using to try to implement this:

#Step_1: Initialize script with necessary imports and setup pins
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(24,GPIO.IN)   
GPIO.setup(25,GPIO.IN)
GPIO.setup(18,GPIO.OUT)
GPIO.setup(17,GPIO.OUT)

#Step_2: I would like to loop continuously as the states of my inputs may 
#change(I'm driving, imagine driving through the nice country roads :))
try:
    while True:
        GPIO.input(24,False)      #This is the first state: No_Signal if
        if(GPIO.input(25)==False):#both inputs are LOW, then both outputs 
            GPIO.output(18,False) #should be low.
            GPIO.output(17,False)
    else:
        GPIO.input(24,True)        #This is the second state: Left_Turn.
        if(GPIO.input(25)==False): # If input 24 is HIGH, and 25 LOW,
            GPIO.output(18,False)  # Then output 18 LOW, 17 HIGH.
            GPIO.output(17,True)
    else:
        GPIO.input(24,False)       # This is the third state: Right_Turn
        if(GPIO.input(25)==True):  # If input 24 LOW, and 25 HIGH,
            GPIO.output(18,True)   # Then output 18 HIGH, 17 LOW.
            GPIO.output(17,False)
finally:
    GPIO.cleanup()
    exit()


SyntaxError: Line 21; else:

Basically, my solution, or algorithm, to get two different sets of LED's to blink at different times, is to define output based on the current state of two inputs. It be nice if I could do that with one input, but I thought deeply for a while an couldn't see how that could be possible, or at least practical.

Problem right now is >>>>> SyntaxError: Line 21; else:

I keep getting invalid syntax errors, but I know sometimes Error returns are generic, and don't indicate the actual problem, so it could be the syntax or some of the code itself.

And for some weird reason, when I move my code from laptop to pi, each IDE or editor keeps changing my code!Each one moves else indentation, even though I din't save it that way.

Does anybody know why this isn't working? Thank you.

CONCLUSION

I'd like to give a huge thanks to Scitronboy and Ghanima to help me edit my code. It works perfectly now.

No_TurnNo_Turn Left_TurnLeft_Turn Right_TurnRight_Turn(excuse me for accidentally inserting a blue LED.)

1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Ghanima
    May 2, 2018 at 18:01

1 Answer 1

2

Cool program, however, that is definitely incorrect syntax. You are mixing up your programming languages(python is very different from other languages). This is one way you could get what you want:

import RPi.GPIO as GPIO
from sys import exit # you have to import the exit() command
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(24,GPIO.IN)
GPIO.setup(25,GPIO.IN)
GPIO.setup(18,GPIO.OUT)
GPIO.setup(17,GPIO.OUT)


try:
    while True:
        if GPIO.input(25)==False and GPIO.input(24)==False:
            GPIO.output(18,False)
            GPIO.output(17,False)
        if GPIO.input(24)==True and GPIO.input(25)==False:
            GPIO.output(18,True)
            GPIO.output(17,False)
        if GPIO.input(24)==False and GPIO.input(25)==True:
            GPIO.output(18,False)
            GPIO.output(17,True)

finally:
    GPIO.cleanup()
    exit()

hope this helps!

6
  • 1
    I want to create a set of automotive turn signal lights. To do this, i have define and Loop through three states in a program, which will signal the microcontroller which will control the LED's. The three states are: Right_Turn, Left_Turn, No_Turn.If inputs 24 and 25 are Low, then there is no turn, meaning, outputs 18 and 17 are both low. If input 24 is high and 25 is low, then there is a right turn, output 18 high, 17 low. left turn would be input 24 low and 25 high, output low and 17 high. I'm going to edit my post to explain all this.
    – Iam Pyre
    May 1, 2018 at 19:33
  • 1
    I'm working on fixing your code for you @IamPyre ... May 1, 2018 at 19:55
  • 1
    That would be nice. I'm sorry for not initially providing more detail. I further explained what I am attempting to accomplish with my code, and added details about each step in the process in my latest draft. Thanks.
    – Iam Pyre
    May 1, 2018 at 20:01
  • @IamPyre , I finished fixing your code for you. I removed my downvote because you added more details. Thankyou for that! May 1, 2018 at 20:13
  • @scitronboy, your if statements contain redundancies .... you are using the form if true == true: instead of if true: ...... for example, the second if statement should really be if GPIO.input(24) and not GPIO.input(25):
    – jsotola
    May 1, 2018 at 21:18

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.