serverius
Posts: 10
Joined: Tue Jan 09, 2018 9:17 pm

IF error (or my code sucks :-) )

Wed Jan 31, 2018 2:51 pm

Hey Folks,

I have a problem with a program we are working on.

The code below is something we want to use in a small game we are creating at work.
Now the idea is that an image is projected onto the floor, and this image is divided into 3 lanes (it has something to do with an interactive traffic circuit for children) and each lane has his own IR sensor to see over what lane the child is driving and depending on what lane it must play either one sound or another.

This is the code i am using for the moment, and it kinda works. When i start the program, the first image is displayed, when i trip the sensor, the sound starts playing, but then it stops. It does not go to the second step in the program.

I'm guessing the problem should be with the IF and ELSE statement. the thing is, it only needs to do something when drive past 1 of the 3 sensors.

So it needs to project the image.
Then check the 3 sensors at the same time for movement while still displaying the image.
If movement is detected, play the mp3 file
And this together should be 15 seconds before going to the next step. in the final code there are going to be a lot more steps but i just included the first 2 as an example.

Hope it is only a small thing and that somebody can help me out with this.

With kind regards,
Jimmy

Code: Select all

import RPi.GPIO as IO
from time import sleep
import os
import subprocess

while True:

#=========================================================================================
#stap1 - Image : pok01 - totale sequentie tussenstap duurt 15 seconden - Hindernis Midden
#=========================================================================================

	image = subprocess.Popen(["feh", "--hide-pointer", "-x", "-q", "-F", "-B", "black", "-g", "1280x800", "/home/pi/ameco/images/pok01.jpg"])

	IO.setwarnings(False)
	IO.setmode (IO.BCM)

#----------Linkersensor - Baan 1 - IO 14----------
	IO.setup(14, IO.IN, pull_up_down=IO.PUD_DOWN)
	IO.wait_for_edge(14, IO.RISING)
	if(IO.input(14)==True):
		os.system('mpg123 "/home/pi/ameco/sound/fail01.mp3" &') #afspelen geluid applaus
	else:

#----------Middensensor - Baan 2 - IO 18----------
	IO.setup(18, IO.IN, pull_up_down=IO.PUD_DOWN)
	IO.wait_for_edge(18, IO.RISING)
	if(IO.input(18)==True):
		os.system('mpg123 "/home/pi/ameco/sound/applause1.mp3" &') #afspelen geluid applaus
	else:

#----------rechtersensor - Baan 3 - IO 24----------
	IO.setup(24, IO.IN, pull_up_down=IO.PUD_DOWN)
	IO.wait_for_edge(24, IO.RISING)
	if(IO.input(24)==True):
		os.system('mpg123 "/home/pi/ameco/sound/fail02.mp3" &') #afspelen geluid applaus
	else:

	tijdb = 15

	sleep(tijdb) #wachttijd 15 seconden

	IO.cleanup()

	image.kill()

	os.system('clear') #scherm wissen na tijd pauze

#=========================================================================================
#stap2 - Image : pok02 - totale sequentie tussenstap duurt 15 seconden - Hindernis Links
#=========================================================================================

	image = subprocess.Popen(["feh", "--hide-pointer", "-x", "-q", "-F", "-B", "black", "-g", "1280x800", "/home/pi/ameco/images/pok02.jpg"]) #display foto

	IO.setwarnings(False)
	IO.setmode (IO.BCM)

#----------Linkersensor - Baan 1 - IO 14----------
	IO.setup(14, IO.IN, pull_up_down=IO.PUD_DOWN)
	IO.wait_for_edge(14, IO.RISING)
	if(IO.input(14)==True):
		os.system('mpg123 "/home/pi/ameco/sound/applause2.mp3" &') #afspelen geluid applaus
	else:

#----------Middensensor - Baan 2 - IO 18----------
	IO.setup(18, IO.IN, pull_up_down=IO.PUD_DOWN)
	IO.wait_for_edge(18, IO.RISING)
	if(IO.input(18)==True):
		os.system('mpg123 "/home/pi/ameco/sound/fail03.mp3" &') #afspelen geluid applaus
	else:

#----------rechtersensor - Baan 3 - IO 24----------
	IO.setup(24, IO.IN, pull_up_down=IO.PUD_DOWN)
	IO.wait_for_edge(24, IO.RISING)
	if(IO.input(24)==True):
		os.system('mpg123 "/home/pi/ameco/sound/fail01.mp3" &') #afspelen geluid applaus
	else:

	tijdb = 15

	sleep(tijdb) #wachttijd 15 seconden

	IO.cleanup()

	image.kill()

	os.system('clear') #scherm wissen na tijd pauze

User avatar
PeterO
Posts: 5829
Joined: Sun Jul 22, 2012 4:14 pm

Re: IF error (or my code sucks :-) )

Wed Jan 31, 2018 3:47 pm

I suggests you need to go and learn Python syntax for conditionals before you try to write any complex code. :shock:

https://docs.python.org/3/tutorial/controlflow.html
Note that is says "There can be zero or more elif parts, and the else part is optional."

But here's a hint: All your else statements look to be superfluous as there is no code to be executed.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

garetha
Posts: 38
Joined: Sat Jan 05, 2013 12:07 am

Re: IF error (or my code sucks :-) )

Wed Jan 31, 2018 3:53 pm

I’m reading this on my phone so the formatting may not show correctly but isn’t the problem just that your else statements aren’t indented?

Gareth

serverius
Posts: 10
Joined: Tue Jan 09, 2018 9:17 pm

Re: IF error (or my code sucks :-) )

Wed Jan 31, 2018 4:04 pm

Hi Peter,

Yeah, i really need to get into the code deeper. ;) Only my boss dumped this on my lap and i have absolutely no previous programming experience :oops: .

When i run the program, it starts, projects the first image and the first sensor works, but there it stops, the second and third sensor do not work together with the first one.

I think that i need to combine the 3 steps into 1 line or something like that. kind of lost at the moment. :cry: :oops:

Starting a python course in a few weeks, unfortunatly they need this by friday... :shock:

Regards,
Jimmy
PeterO wrote:
Wed Jan 31, 2018 3:47 pm
I suggests you need to go and learn Python syntax for conditionals before you try to write any complex code. :shock:

https://docs.python.org/3/tutorial/controlflow.html
Note that is says "There can be zero or more elif parts, and the else part is optional."

But here's a hint: All your else statements look to be superfluous as there is no code to be executed.

PeterO

User avatar
TC9000
Posts: 9
Joined: Wed Jan 31, 2018 3:53 pm

Re: IF error (or my code sucks :-) )

Wed Jan 31, 2018 4:09 pm

yes start by figuring out how python indentation works:

Pyton relies on your indentation to know what code to execute.

Code: Select all

IF condition:
  do something
else:
  do something else

you have

IF condition:
  do something
else:
do something else:
meaning you might "do something", you will always "do something else".

serverius
Posts: 10
Joined: Tue Jan 09, 2018 9:17 pm

Re: IF error (or my code sucks :-) )

Wed Jan 31, 2018 4:12 pm

Hi Gareth

Think i have bigger problems than the indentation. :D :D I don't even know if i need the else statement.

The 3 sensors should start checking at the same time, while projecting the image. So i only have a thing they need to do when they detect movement below the sensor, and when there is no movement during a 15 second period, it should automatically go to the next step and so on...

My guess is that i need to find a way to combine the checks for the sensors in 1 line.
When i test this now, it just projects the first image, checks on the first sensor and then stops, sensor 2 and 3 do not respond while the first one works perfect.

Regards,
Jimmy
garetha wrote:
Wed Jan 31, 2018 3:53 pm
I’m reading this on my phone so the formatting may not show correctly but isn’t the problem just that your else statements aren’t indented?

Gareth

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: IF error (or my code sucks :-) )

Wed Jan 31, 2018 4:16 pm

You should not be using

Code: Select all

IO.wait_for_edge(14, IO.RISING)
This is a 'blocking' action and nothing will happen in your code until a rising edge is detected on GPIO14.

Personally I'd use Threaded Callbacks for this, see the GPIO documentation

Concentrate on getting one sensor to work first and then worry about the rest.

(I don't speak Dutch so I don't know what the program is supposed to do from the comments. Looks like the code was written by someone who also has never written python before.)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

serverius
Posts: 10
Joined: Tue Jan 09, 2018 9:17 pm

Re: IF error (or my code sucks :-) )

Wed Jan 31, 2018 4:37 pm

Hi Scotty,

Ok thanks for the tip, i'll have a closer look at the documentation you sent.

And about the code, you are completely correct :) :) :) Have been fighting with this for a few weeks and before this time i had never used python. :oops:

Going to start a python course in a few weeks but till that time I'll try getting by with a lot of google and help from this forum.

Regards,
Jimmy
scotty101 wrote:
Wed Jan 31, 2018 4:16 pm
You should not be using

Code: Select all

IO.wait_for_edge(14, IO.RISING)
This is a 'blocking' action and nothing will happen in your code until a rising edge is detected on GPIO14.

Personally I'd use Threaded Callbacks for this, see the GPIO documentation

Concentrate on getting one sensor to work first and then worry about the rest.

(I don't speak Dutch so I don't know what the program is supposed to do from the comments. Looks like the code was written by someone who also has never written python before.)

pcmanbob
Posts: 9298
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: IF error (or my code sucks :-) )

Wed Jan 31, 2018 4:52 pm

Seeing as you need this by friday, I think this might work as long as you can accept a wait of about 0.1 sec between checks of your inputs.

So I moved your gpio set up to the top so it only occurs once seeing as you are using the same sensor inputs each time.

we then have a simple time loop created by comparing the start time to the time on each pass of the loop once 15 sec pass we exit the loop, kill the image ( used you command ) and start another loop doing the same thing.

how ever if one of the sensors is triggered we play the sound and then set the time so that we exit the loop.

you can repeat this over and over ( if it works ) just changing the image line and the sounds played on each sensor input.

ITS NOT PRETTY but if it gets you working for Friday then that's what counts.

please note this is untested there may be indent or other errors.....

Code: Select all


import RPi.GPIO as IO
from time
import os
import subprocess

IO.setwarnings(False)
IO.setmode (IO.BCM)
IO.setup(14, IO.IN, pull_up_down=IO.PUD_DOWN)
IO.setup(18, IO.IN, pull_up_down=IO.PUD_DOWN)
IO.setup(24, IO.IN, pull_up_down=IO.PUD_DOWN)



#=========================================================================================
#stap1 - Image : pok01 - totale sequentie tussenstap duurt 15 seconden - Hindernis Midden
#=========================================================================================

	image = subprocess.Popen(["feh", "--hide-pointer", "-x", "-q", "-F", "-B", "black", "-g", "1280x800", "/home/pi/ameco/images/pok01.jpg"])

now  = time.time
finish = now + 15

while now < finish:
    now = time.time
#----------Linkersensor - Baan 1 - IO 14----------
	

    if(IO.input(14)==True):
        os.system('mpg123 "/home/pi/ameco/sound/fail01.mp3" &') #afspelen geluid applaus
        now = finish + 1

#----------Middensensor - Baan 2 - IO 18----------
	
	
    if(IO.input(18)==True):
        os.system('mpg123 "/home/pi/ameco/sound/applause1.mp3" &') #afspelen geluid applaus
        now = finish + 1	

#----------rechtersensor - Baan 3 - IO 24----------
	
	
    if(IO.input(24)==True):
        os.system('mpg123 "/home/pi/ameco/sound/fail02.mp3" &') #afspelen geluid applaus
        now = finish + 1	

	time.sleep(0.1)

	

image.kill()

	

#=========================================================================================
#stap2 - Image : pok02 - totale sequentie tussenstap duurt 15 seconden - Hindernis Links
#=========================================================================================

	image = subprocess.Popen(["feh", "--hide-pointer", "-x", "-q", "-F", "-B", "black", "-g", "1280x800", "/home/pi/ameco/images/pok02.jpg"]) #display foto

now  = time.time
finish = now + 15

while now < finish:
    now = time.time

#----------Linkersensor - Baan 1 - IO 14----------

    if(IO.input(14)==True):
        os.system('mpg123 "/home/pi/ameco/sound/applause2.mp3" &') #afspelen geluid applaus
        now = finish + 1

#----------Middensensor - Baan 2 - IO 18----------

    if(IO.input(18)==True):
        os.system('mpg123 "/home/pi/ameco/sound/fail03.mp3" &') #afspelen geluid applaus
        now = finish + 1

#----------rechtersensor - Baan 3 - IO 24----------

    if(IO.input(24)==True):
        os.system('mpg123 "/home/pi/ameco/sound/fail01.mp3" &') #afspelen geluid applaus
        now = finish + 1

	time.sleep(0.1)

image.kill()

	
Good Luck.....
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

serverius
Posts: 10
Joined: Tue Jan 09, 2018 9:17 pm

Re: IF error (or my code sucks :-) )

Wed Jan 31, 2018 9:09 pm

Hi pcmanbob,

Thanks ever so much, i'll copy the code into my rpi tomorrow and see how it goes.
And the time between checks is absolutly no problem.

I'll post the feedback as soon as i get a chance to test this code.

Thanks a million,
Jimmy
pcmanbob wrote:
Wed Jan 31, 2018 4:52 pm
Seeing as you need this by friday, I think this might work as long as you can accept a wait of about 0.1 sec between checks of your inputs.

So I moved your gpio set up to the top so it only occurs once seeing as you are using the same sensor inputs each time.

we then have a simple time loop created by comparing the start time to the time on each pass of the loop once 15 sec pass we exit the loop, kill the image ( used you command ) and start another loop doing the same thing.

how ever if one of the sensors is triggered we play the sound and then set the time so that we exit the loop.

you can repeat this over and over ( if it works ) just changing the image line and the sounds played on each sensor input.

ITS NOT PRETTY but if it gets you working for Friday then that's what counts.

please note this is untested there may be indent or other errors.....

Code: Select all


import RPi.GPIO as IO
from time
import os
import subprocess

IO.setwarnings(False)
IO.setmode (IO.BCM)
IO.setup(14, IO.IN, pull_up_down=IO.PUD_DOWN)
IO.setup(18, IO.IN, pull_up_down=IO.PUD_DOWN)
IO.setup(24, IO.IN, pull_up_down=IO.PUD_DOWN)



#=========================================================================================
#stap1 - Image : pok01 - totale sequentie tussenstap duurt 15 seconden - Hindernis Midden
#=========================================================================================

	image = subprocess.Popen(["feh", "--hide-pointer", "-x", "-q", "-F", "-B", "black", "-g", "1280x800", "/home/pi/ameco/images/pok01.jpg"])

now  = time.time
finish = now + 15

while now < finish:
    now = time.time
#----------Linkersensor - Baan 1 - IO 14----------
	

    if(IO.input(14)==True):
        os.system('mpg123 "/home/pi/ameco/sound/fail01.mp3" &') #afspelen geluid applaus
        now = finish + 1

#----------Middensensor - Baan 2 - IO 18----------
	
	
    if(IO.input(18)==True):
        os.system('mpg123 "/home/pi/ameco/sound/applause1.mp3" &') #afspelen geluid applaus
        now = finish + 1	

#----------rechtersensor - Baan 3 - IO 24----------
	
	
    if(IO.input(24)==True):
        os.system('mpg123 "/home/pi/ameco/sound/fail02.mp3" &') #afspelen geluid applaus
        now = finish + 1	

	time.sleep(0.1)

	

image.kill()

	

#=========================================================================================
#stap2 - Image : pok02 - totale sequentie tussenstap duurt 15 seconden - Hindernis Links
#=========================================================================================

	image = subprocess.Popen(["feh", "--hide-pointer", "-x", "-q", "-F", "-B", "black", "-g", "1280x800", "/home/pi/ameco/images/pok02.jpg"]) #display foto

now  = time.time
finish = now + 15

while now < finish:
    now = time.time

#----------Linkersensor - Baan 1 - IO 14----------

    if(IO.input(14)==True):
        os.system('mpg123 "/home/pi/ameco/sound/applause2.mp3" &') #afspelen geluid applaus
        now = finish + 1

#----------Middensensor - Baan 2 - IO 18----------

    if(IO.input(18)==True):
        os.system('mpg123 "/home/pi/ameco/sound/fail03.mp3" &') #afspelen geluid applaus
        now = finish + 1

#----------rechtersensor - Baan 3 - IO 24----------

    if(IO.input(24)==True):
        os.system('mpg123 "/home/pi/ameco/sound/fail01.mp3" &') #afspelen geluid applaus
        now = finish + 1

	time.sleep(0.1)

image.kill()

	
Good Luck.....

Return to “Python”