## How to combine two infrared sensor codes for one program?

rpinewsymphony
Posts: 6
Joined: Mon Aug 19, 2019 2:13 pm

### How to combine two infrared sensor codes for one program?

Hello, I am new to python and i would like to run my program based on the two infrared sensor to read two different readings of rpm of the two motor at the same time. Here is the code that we tried to make.

Code: Select all

``````#!/usr/bin/python3
import RPi.GPIO as GPIO
from time import sleep
import time, math

dist_meas = 0.00
dist_meas1 = 0.00
km_per_hour = 0
km_per_hour1 = 0
rpm = 0
rpm1 = 0
elapse = 0
elapse1 = 0
sensor = 24
sensor1 = 27
pulse = 0
pulse2 = 0
start_timer = time.time()

def init_GPIO():					# initialize GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(sensor,GPIO.IN,GPIO.PUD_UP)

def calculate_elapse(channel):				# callback function
global pulse, start_timer, elapse
pulse+=1								# increase pulse by 1 whenever interrupt occurred
elapse = time.time() - start_timer		# elapse for every 1 complete rotation made!
start_timer = time.time()				# let current time equals to start_timer

def calculate_speed(7):
global pulse,elapse,rpm,dist_km,dist_meas,km_per_sec,km_per_hour
if elapse !=0:							# to avoid DivisionByZero error
rpm = 1/elapse * 60
circ_cm = (2*math.pi)*6			# calculate wheel circumference in CM
dist_km = circ_cm/100000 			# convert cm to km
km_per_sec = dist_km / elapse		# calculate KM/sec
km_per_hour = km_per_sec * 3600		# calculate KM/h
dist_meas = (dist_km*pulse)*1000	# measure distance traverse in meter
return km_per_hour

def init_interrupt():
GPIO.add_event_detect(sensor, GPIO.FALLING, callback = calculate_elapse, bouncetime = 20)

def init_GPIO1():					# initialize GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(sensor1,GPIO.IN,GPIO.PUD_UP)

def calculate_elapse1(channel):				# callback function
global pulse1, start_timer, elapse1
pulse1+=1								# increase pulse by 1 whenever interrupt occurred
elapse1 = time.time() - start_timer		# elapse for every 1 complete rotation made!
start_timer = time.time()				# let current time equals to start_timer

def calculate_speed1(6):
global pulse1,elapse1,rpm1,dist_km1,dist_meas1,km_per_sec1,km_per_hour1
if elapse1 !=0:							# to avoid DivisionByZero error
rpm1 = 1/elapse1 * 60
circ_cm1 = (2*math.pi)*6			# calculate wheel circumference in CM
dist_km1 = circ_cm/100000 			# convert cm to km
km_per_sec1 = dist_km1 / elapse1		# calculate KM/sec
km_per_hour1 = km_per_sec1 * 3600		# calculate KM/h
dist_meas1 = (dist_km1*pulse1)*1000	# measure distance traverse in meter
return km_per_hour1

def init_interrupt1():
GPIO.add_event_detect(sensor1, GPIO.FALLING, callback = calculate_elapse1, bouncetime = 20)

if __name__ == '__main__':
init_GPIO()
init_interrupt()
while True:
calculate_speed(6)	# call this function with wheel radius as parameter
print('rpm:{0:.0f}-RPM kmh:{1:.0f}-KMH dist_meas:{2:.2f}m pulse:{3}'.format(rpm,km_per_hour,dist_meas,pulse))
sleep(0.1)

init_GPIO1()
init_interrupt1()
while True:
calculate_speed(6)	# call this function with wheel radius as parameter
print('rpm1:{0:.0f}-RPM kmh1:{1:.0f}-KMH dist_meas1:{2:.2f}m pulse1:{3}'.format(rpm1,km_per_hour1,dist_meas1,pulse1))
sleep(0.1)
``````

B.Goode
Posts: 9354
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

### Re: How to combine two infrared sensor codes for one program?

Welcome to the Raspberry Pi forums.
rpinewsymphony wrote:
Mon Aug 19, 2019 2:30 pm
Hello, I am new to python and i would like to run my program based on the two infrared sensor to read two different readings of rpm of the two motor at the same time. Here is the code that we tried to make.

Code: Select all

``````#!/usr/bin/python3
import RPi.GPIO as GPIO
from time import sleep
import time, math

dist_meas = 0.00
dist_meas1 = 0.00
km_per_hour = 0
km_per_hour1 = 0
rpm = 0
rpm1 = 0
elapse = 0
elapse1 = 0
sensor = 24
sensor1 = 27
pulse = 0
pulse2 = 0
start_timer = time.time()

def init_GPIO():					# initialize GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(sensor,GPIO.IN,GPIO.PUD_UP)

def calculate_elapse(channel):				# callback function
global pulse, start_timer, elapse
pulse+=1								# increase pulse by 1 whenever interrupt occurred
elapse = time.time() - start_timer		# elapse for every 1 complete rotation made!
start_timer = time.time()				# let current time equals to start_timer

def calculate_speed(7):
global pulse,elapse,rpm,dist_km,dist_meas,km_per_sec,km_per_hour
if elapse !=0:							# to avoid DivisionByZero error
rpm = 1/elapse * 60
circ_cm = (2*math.pi)*6			# calculate wheel circumference in CM
dist_km = circ_cm/100000 			# convert cm to km
km_per_sec = dist_km / elapse		# calculate KM/sec
km_per_hour = km_per_sec * 3600		# calculate KM/h
dist_meas = (dist_km*pulse)*1000	# measure distance traverse in meter
return km_per_hour

def init_interrupt():
GPIO.add_event_detect(sensor, GPIO.FALLING, callback = calculate_elapse, bouncetime = 20)

def init_GPIO1():					# initialize GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(sensor1,GPIO.IN,GPIO.PUD_UP)

def calculate_elapse1(channel):				# callback function
global pulse1, start_timer, elapse1
pulse1+=1								# increase pulse by 1 whenever interrupt occurred
elapse1 = time.time() - start_timer		# elapse for every 1 complete rotation made!
start_timer = time.time()				# let current time equals to start_timer

def calculate_speed1(6):
global pulse1,elapse1,rpm1,dist_km1,dist_meas1,km_per_sec1,km_per_hour1
if elapse1 !=0:							# to avoid DivisionByZero error
rpm1 = 1/elapse1 * 60
circ_cm1 = (2*math.pi)*6			# calculate wheel circumference in CM
dist_km1 = circ_cm/100000 			# convert cm to km
km_per_sec1 = dist_km1 / elapse1		# calculate KM/sec
km_per_hour1 = km_per_sec1 * 3600		# calculate KM/h
dist_meas1 = (dist_km1*pulse1)*1000	# measure distance traverse in meter
return km_per_hour1

def init_interrupt1():
GPIO.add_event_detect(sensor1, GPIO.FALLING, callback = calculate_elapse1, bouncetime = 20)

if __name__ == '__main__':
init_GPIO()
init_interrupt()
while True:
calculate_speed(6)	# call this function with wheel radius as parameter
print('rpm:{0:.0f}-RPM kmh:{1:.0f}-KMH dist_meas:{2:.2f}m pulse:{3}'.format(rpm,km_per_hour,dist_meas,pulse))
sleep(0.1)

init_GPIO1()
init_interrupt1()
while True:
calculate_speed(6)	# call this function with wheel radius as parameter
print('rpm1:{0:.0f}-RPM kmh1:{1:.0f}-KMH dist_meas1:{2:.2f}m pulse1:{3}'.format(rpm1,km_per_hour1,dist_meas1,pulse1))
sleep(0.1)
``````

You need to rethink your logic and the corresponding code implementation.

The point of a while True: loop is that it never exits. Because the constant True is always logically True.

So once you get to the first loop -

Code: Select all

``````	while True:
calculate_speed(6)	# call this function with wheel radius as parameter
print('rpm:{0:.0f}-RPM kmh:{1:.0f}-KMH dist_meas:{2:.2f}m pulse:{3}'.format(rpm,km_per_hour,dist_meas,pulse))
sleep(0.1)``````
... that's where your code carries on running until externally interrupted.

That is probably not what you intended?

rpinewsymphony
Posts: 6
Joined: Mon Aug 19, 2019 2:13 pm

### Re: How to combine two infrared sensor codes for one program?

sorry for late reps...been busy researching how to fix the code but I did not know how until now sir...what should I do?

PhatFil
Posts: 1566
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

### Re: How to combine two infrared sensor codes for one program?

currently you have

Code: Select all

``````init first thing
while true
do first thing

init second thing
while true
do Second thing
``````

Code: Select all

``````init first thing
init second thing
while true
do first thing
do second thing``````

rpinewsymphony
Posts: 6
Joined: Mon Aug 19, 2019 2:13 pm

It worked thanks