Arpitthumar
Posts: 32
Joined: Sat Jun 29, 2019 5:39 am

How to check output using timer at every second ?

Wed Jul 03, 2019 11:18 am

I want to use a timer to check my output every second continuously in the background of the process so from result output I can set my output from according to my output check by the timer. Post the example if anybody has.

gordon77
Posts: 4219
Joined: Sun Aug 05, 2012 3:12 pm

Re: How to check output using timer at every second ?

Wed Jul 03, 2019 11:31 am

What is the output?

Do you have any code you use for producing the output?

Arpitthumar
Posts: 32
Joined: Sat Jun 29, 2019 5:39 am

Re: How to check output using timer at every second ?

Thu Jul 04, 2019 4:17 am

Code: Select all

import serial
import time 
import RPi.GPIO as GPIO
count_enable=0
count=0
#i=0
flag=0
transmit_flag=0
rbuff=list(range(100))
temp=0
#zero_detect=1
print("staring program")
ser=serial.Serial('/dev/ttyAMA0',921600,timeout=1)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)

while True:
	#if transmit_flag==0:
	rbuff=ser.read()
	#for i in range(len(rbuff)):
	#ser.write(rbuff[i])
		#if i >= 100:
			#transmit_flag=1
	if count_enable == 1:
		count+=1
		#print(count)
	if rbuff == chr(0x02) and flag==0: # and zero_detect==1: 
		count_enable=1
		#ser.write(rbuff)
	#	print (count)
		flag=1
	if rbuff == chr(0x01) and flag==1  and count==1:
		#ser.write(rbuff)
		#print(count)
		flag=2
     	if rbuff == chr(0x04) and flag==2 and count==2: 
                #ser.write(rbuff)
		#print(count)
                flag=3
        if rbuff == chr(0x03) and flag==3 and count==3:
                #ser.write(rbuff)
		#print(count)
                flag=4


	if flag==4 and count==28:
		ser.write(rbuff)
		#print(count)
		count=0
		flag=0

		if temp == rbuff:
			GPIO.output(12,GPIO.LOW)
			#ser.write(temp)
		else:
  			GPIO.output(12,GPIO.HIGH)
			temp=rbuff
			#ser.write(temp)
	
	if count == 1 and rbuff != chr(0x01):
		count_enable=0
		count=0
		flag=0
my code compares hex data and it finds a particular number of data and it compares that data with the next cycle's same number of data. According to that, it gives output so i want to use a timer to compare that data at every second. Actual compare data is coming at every ms.

blimpyway
Posts: 183
Joined: Mon Mar 19, 2018 1:18 pm

Re: How to check output using timer at every second ?

Thu Jul 04, 2019 6:24 am

For something to run in "background" one can use either threading, multiprocessing, asyncio (or other async framework like gevent)

gordon77
Posts: 4219
Joined: Sun Aug 05, 2012 3:12 pm

Re: How to check output using timer at every second ?

Thu Jul 04, 2019 8:12 am

Try this.. I have no way of checking the serial input part.

Code: Select all

#!/usr/bin/python3
import threading
import queue
import serial
import time 
import RPi.GPIO as GPIO
count_enable=0
count=0
#i=0
flag=0
transmit_flag=0
rbuff=list(range(100))
temp=0
#zero_detect=1
print("staring program")
ser=serial.Serial('/dev/ttyAMA0',921600,timeout=1)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)

events_queue = queue.Queue()

start = time.time()

def thread_read():
    global count_enable, flag,temp, count
    while True:
        #if transmit_flag==0:
        rbuff=ser.read()
        #for i in range(len(rbuff)):
            #ser.write(rbuff[i])
            #if i >= 100:
            #transmit_flag=1
        if count_enable == 1:
            count+=1
            #print(count)
        if rbuff == chr(0x02) and flag==0: # and zero_detect==1: 
            count_enable=1
            #ser.write(rbuff)
            #print (count)
            flag=1
        if rbuff == chr(0x01) and flag==1  and count==1:
            #ser.write(rbuff)
            #print(count)
            flag=2
        if rbuff == chr(0x04) and flag==2 and count==2:
            #ser.write(rbuff)
            #print(count)
            flag=3
        if rbuff == chr(0x03) and flag==3 and count==3:
            #ser.write(rbuff)
            #print(count)
            flag=4

        if flag==4 and count==28:
            ser.write(rbuff)
            #print(count)
            count=0
            flag=0
        if temp == rbuff:
            GPIO.output(12,GPIO.LOW)
            #ser.write(temp)
        else:
            GPIO.output(12,GPIO.HIGH)
            temp=rbuff
            #ser.write(temp)
	
        if count == 1 and rbuff != chr(0x01):
            count_enable=0
            count=0
            flag=0

def thread_check():
    global start
    while True:
        if time.time() - start >= 1:
            start = time.time()
            print (GPIO.input(12))

read_thread = threading.Thread(target=thread_read)
read_thread.start()

check_thread = threading.Thread(target=thread_check)
check_thread.start()

event=threading.Event()
try:
    event.wait()
except KeyboardInterrupt:
    pass
    

Arpitthumar
Posts: 32
Joined: Sat Jun 29, 2019 5:39 am

Re: How to check output using timer at every second ?

Fri Jul 05, 2019 9:50 am

What if i want to make led on for suppose 20 seconds if i get object is greater than or less than previous object.What do i have to add and where?

gordon77
Posts: 4219
Joined: Sun Aug 05, 2012 3:12 pm

Re: How to check output using timer at every second ?

Fri Jul 05, 2019 10:18 am

Is the led an additional output?

Which variables are you comparing?

gordon77
Posts: 4219
Joined: Sun Aug 05, 2012 3:12 pm

Re: How to check output using timer at every second ?

Fri Jul 05, 2019 12:51 pm

Try this...

Lights LED on pin 26 for 20 seconds if the received rbuff string doesn't equal the last one..

I tested it with the arduino script below, I am not sure what you are using. I temp set the led on 26 to 2 seconds to test.

Code: Select all

#!/usr/bin/python3
import threading
import queue
import serial
import time 
import RPi.GPIO as GPIO
import sys

count_enable=0
count=0
#i=0
flag=0
transmit_flag=0
rbuff=list(range(100))
old_rbuff2 = "0"
temp=0
#zero_detect=1
print("staring program")
ser=serial.Serial('/dev/ttyAMA0',921600,timeout=1)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)
GPIO.setup(26,GPIO.OUT)

start1 = time.time()
start2 = time.time()

def thread_read():
    global count_enable, flag,temp, count, start2, old_rbuff2
    while True:
        #if transmit_flag==0:
        rbuff=ser.read()
        if sys.version_info[0] == 3:
            rbuff2 = rbuff.decode("utf-8","ignore")
        else:
            rbuff2 = rbuff

        #for i in range(len(rbuff)):
            #ser.write(rbuff[i])
            #if i >= 100:
            #transmit_flag=1
            
        if len(rbuff2)  > 0 and ord(rbuff2) != old_rbuff2:
            GPIO.output(26,GPIO.HIGH)
            start2 = time.time()
            
        if count_enable == 1:
            count+=1
            #print(count)
            
        if rbuff2 == chr(0x02) and flag==0: # and zero_detect==1: 
            count_enable=1
            #ser.write(rbuff)
            #print (count)
            flag=1
            old_rbuff2 = rbuff2
        if rbuff2 == chr(0x01) and flag==1  and count==1:
            print (ord(rbuff2),'test')
            #ser.write(rbuff)
            #print(count)
            flag=2
            old_rbuff2 = rbuff2
        if rbuff2 == chr(0x04) and flag==2 and count==2:
            #ser.write(rbuff)
            #print(count)
            flag=3
            old_rbuff2 = rbuff2
        if rbuff2 == chr(0x03) and flag==3 and count==3:
            #ser.write(rbuff)
            #print(count)
            flag=4
            old_rbuff2 = rbuff2

        if flag==4 and count==28:
            ser.write(rbuff)
            #print(count)
            count=0
            flag=0
            
        if len(rbuff2)  > 0 and temp == ord(rbuff2):
            GPIO.output(12,GPIO.LOW)
            #ser.write(temp)
            
        elif len(rbuff2)  > 0 :
            GPIO.output(12,GPIO.HIGH)
            temp = ord(rbuff2)
            #ser.write(temp)
	
        if count == 1 and rbuff2 != chr(0x01):
            count_enable=0
            count=0
            flag=0
            
        if time.time() - start2 > 20: # light led for 20 seconds
            GPIO.output(26,GPIO.LOW)
        
def thread_check():
    global start1
    while True:
        if time.time() - start1 >= 1:
            start1 = time.time()
            print (GPIO.input(12))

read_thread = threading.Thread(target=thread_read)
read_thread.start()

check_thread = threading.Thread(target=thread_check)
check_thread.start()

Code: Select all

void setup(){
  Serial.begin(921600);
}

void loop(){
  Serial.write(0x02);
  delay(3000);
  Serial.write(0x01);
  delay(3000);
  Serial.write(0x04);
  delay(3000);
  Serial.write(0x04);
  delay(3000);
  Serial.write(0x03);
  delay(3000);
  Serial.print('0');
  delay(3000);

}

Arpitthumar
Posts: 32
Joined: Sat Jun 29, 2019 5:39 am

Re: How to check output using timer at every second ?

Mon Jul 08, 2019 8:27 am

gordon77 wrote:
Fri Jul 05, 2019 10:18 am
Is the led an additional output?

Which variables are you comparing?
Yes led is an additional output to see increase and decrease in object. I am comparing rbuff value with previous rbuff value by taking this variable in temp.

gordon77
Posts: 4219
Joined: Sun Aug 05, 2012 3:12 pm

Re: How to check output using timer at every second ?

Mon Jul 08, 2019 12:26 pm

Can you explain what the serial input is?

Arpitthumar
Posts: 32
Joined: Sat Jun 29, 2019 5:39 am

Re: How to check output using timer at every second ?

Tue Jul 09, 2019 4:20 am

hex data is my serial input. But as per ser.read reads character that's why i am comparing incoming data using chr(). My data is coming 0x00 to 0xff.

Arpitthumar
Posts: 32
Joined: Sat Jun 29, 2019 5:39 am

Re: How to check output using timer at every second ?

Fri Jul 12, 2019 4:53 am

Hi Gordon77,

Thanks for the replay.

In this code when objects(rbuff) greater than or less than previous object(temp) Led must get on and when this happens it must on for 20 second and background process of checking led status should be continue. And during 20 seconds of led on period if it again detects that led status is again on due to increase or decrease in object led on period of 20 seconds extend for another 20 seconds and so on.

gordon77 wrote:
Fri Jul 05, 2019 12:51 pm
Try this...

Lights LED on pin 26 for 20 seconds if the received rbuff string doesn't equal the last one..


I tested it with the arduino script below, I am not sure what you are using. I temp set the led on 26 to 2 seconds to test.

Code: Select all

#!/usr/bin/python3
import threading
import queue
import serial
import time 
import RPi.GPIO as GPIO
import sys

count_enable=0
count=0
#i=0
flag=0
transmit_flag=0
rbuff=list(range(100))
old_rbuff2 = "0"
temp=0
#zero_detect=1
print("staring program")
ser=serial.Serial('/dev/ttyAMA0',921600,timeout=1)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)
GPIO.setup(26,GPIO.OUT)

start1 = time.time()
start2 = time.time()

def thread_read():
    global count_enable, flag,temp, count, start2, old_rbuff2
    while True:
        #if transmit_flag==0:
        rbuff=ser.read()
        if sys.version_info[0] == 3:
            rbuff2 = rbuff.decode("utf-8","ignore")
        else:
            rbuff2 = rbuff

        #for i in range(len(rbuff)):
            #ser.write(rbuff[i])
            #if i >= 100:
            #transmit_flag=1
            
        if len(rbuff2)  > 0 and ord(rbuff2) != old_rbuff2:
            GPIO.output(26,GPIO.HIGH)
            start2 = time.time()
            
        if count_enable == 1:
            count+=1
            #print(count)
            
        if rbuff2 == chr(0x02) and flag==0: # and zero_detect==1: 
            count_enable=1
            #ser.write(rbuff)
            #print (count)
            flag=1
            old_rbuff2 = rbuff2
        if rbuff2 == chr(0x01) and flag==1  and count==1:
            print (ord(rbuff2),'test')
            #ser.write(rbuff)
            #print(count)
            flag=2
            old_rbuff2 = rbuff2
        if rbuff2 == chr(0x04) and flag==2 and count==2:
            #ser.write(rbuff)
            #print(count)
            flag=3
            old_rbuff2 = rbuff2
        if rbuff2 == chr(0x03) and flag==3 and count==3:
            #ser.write(rbuff)
            #print(count)
            flag=4
            old_rbuff2 = rbuff2

        if flag==4 and count==28:
            ser.write(rbuff)
            #print(count)
            count=0
            flag=0
            
        if len(rbuff2)  > 0 and temp == ord(rbuff2):
            GPIO.output(12,GPIO.LOW)
            #ser.write(temp)
            
        elif len(rbuff2)  > 0 :
            GPIO.output(12,GPIO.HIGH)
            temp = ord(rbuff2)
            #ser.write(temp)
	
        if count == 1 and rbuff2 != chr(0x01):
            count_enable=0
            count=0
            flag=0
            
        if time.time() - start2 > 20: # light led for 20 seconds
            GPIO.output(26,GPIO.LOW)
        
def thread_check():
    global start1
    while True:
        if time.time() - start1 >= 1:
            start1 = time.time()
            print (GPIO.input(12))

read_thread = threading.Thread(target=thread_read)
read_thread.start()

check_thread = threading.Thread(target=thread_check)
check_thread.start()

Code: Select all

void setup(){
  Serial.begin(921600);
}

void loop(){
  Serial.write(0x02);
  delay(3000);
  Serial.write(0x01);
  delay(3000);
  Serial.write(0x04);
  delay(3000);
  Serial.write(0x04);
  delay(3000);
  Serial.write(0x03);
  delay(3000);
  Serial.print('0');
  delay(3000);

}

gordon77
Posts: 4219
Joined: Sun Aug 05, 2012 3:12 pm

Re: How to check output using timer at every second ?

Fri Jul 12, 2019 8:13 am

What do you see it doing when you run it?
It should keep the led on as you describe.
I can't fully test it without knowing more details of the serial stream you are feeding it.

Arpitthumar
Posts: 32
Joined: Sat Jun 29, 2019 5:39 am

Re: How to check output using timer at every second ?

Fri Jul 12, 2019 10:28 am

Here is one full stream om which i am testing this. It is an infinite stream. In this i have to detect the first 4 byte 20 01 04 03 if it is detected its 28th bit which is 06(its array so it starts from 0) which is a total number of objects. It may change as per increase and decrease in the total number of objects. So as per this, i have to make led on and off. I have tried your particular code for 20sec on time but haven't succeeded.
Here is the bit stream.

Code: Select all

02 01 04 03 06 05 08 07 04 00 00 02 80 00 00 00 42 16 0A 00 DC 2C 00 00 D7 63 24 7C 06 00 00 00 01 00 00 00 00 00 00 00 01 00 00 00 4C 00 00 00 06 00 07 00 05 00 00 00 C9 00 1D 00 9A 00 00 00 24 00 00 00 3D 00 BA FF 65 04 00 00 27 00 00 00 43 00 5C FE 7B 04 00 00 FB 00 00 00 09 00 48 F9 F7 1D 00 00 FF 00 00 00 2C 00 79 14 8A 17 00 00 FF 00 00 00 2C 00 39 1E BD 07 00 00 0F 0F 0F 0F 02 01 04 03 06 05 08 07 04 00 00 02 80 00 00 00 42 16 0A 00 DD 2C 00 00 F3 65 15 85 06 00 00 00 01 00 00 00 00 00 00 00 01 00 00 00 4C 00 00 00 06 00 07 00 05 00 00 00 B5 00 22 00 99 00 00 00 24 00 00 00 3D 00 BA FF 65 04 00 00 27 00 00 00 43 00 5C FE 7B 04 00 00 FB 00 00 00 07 00 53 F8 BC 1D 00 00 FF 00 00 00 2D 00 79 14 8A 17 00 00 FF 00 00 00 2A 00 39 1E BD 07 00 00 0F 0F 0F 0F 02 01 04 03 06 05 08 07 04 00 00 02 80 00 00 00 42 16 0A 00 DE 2C 00 00 19 BC 05 8E 04 00 00 00 01 00 00 00 00 00 00 00 01 00 00 00 34 00 00 00 04 00 07 00 24 00 00 00 3D 00 BA FF 65 04 00 00 27 00 00 00 44 00 5C FE 7B 04 00 00 FF 00 00 00 2D 00 79 14 8A 17 00 00 FF 00 00 00 2B 00 39 1E BD 07 00 00 0F 0F 0F 0F 0F 0F 0F 0F 0F 0F 0F 0F 0F 0F 0F 0F 0F 0F 0F 0F 0F 0F 0F 0F 0F 0F 0F 0F





I have modified this code a little bit.It does work with led on for 20 seconds but after 20 seconds it permanently off and never on till i again load the program. Here led on when there is a change in the object either plus 2 or minus 2 . Though adding this below code of "light led for 20 seconds" in the code i am not getting 20s on time when there is an increase and decrease in an object.

Code: Select all


		if time.time() - start2 > 20: # light led for 20 seconds
			GPIO.output(12,GPIO.LOW)
What my concern is Whenever in given below code else condition is satisfied led on for fraction of second but i want it to on for 20 seconds and if again else condition is satisfied it reset time and again on for 20 seconds and so on.So the time should reset to agin on for further 20 seconds whenever else condition get satisfied.

Code: Select all

if rbuff[0]-2 <= temp <= rbuff[0]+2:
	GPIO.output(12,GPIO.LOW)
	#ser.write(ord(buff))
else:
	GPIO.output(12,GPIO.HIGH)
	temp=rbuff[0]
	# ser.write(ord(buff))
complete code is as given below

Code: Select all

import threading
import queue
import serial
import time 
import RPi.GPIO as GPIO
count_enable=0
count=0
#i=0
flag=0
transmit_flag=0
#rbuff=[]
rbuff=list(range(100))
temp=0
#zero_detect=1
print("staring program")
ser=serial.Serial('/dev/ttyAMA0',921600,timeout=1)
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)

events_queue = queue.Queue()

start1 = time.time()
start2 = time.time()
def thread_read():
	global count_enable, flag,temp, count,start2
	while True:
		rbuff=ser.read()
		if count_enable == 1:
			count+=1
		if rbuff[0] == 0x02 and flag==0: # and zero_detect==1: 
			count_enable=1
			#ser.write(rbuff)
			#print (count)
			flag=1
		if rbuff[0] == 0x01 and flag==1  and count==1:
			#ser.write(rbuff)
			#print(count)
			flag=2
		if rbuff[0] == 0x04 and flag==2 and count==2:
			#ser.write(rbuff)
			#print(count)
			flag=3
		if rbuff[0] == 0x03 and flag==3 and count==3:
			#ser.write(rbuff)
			#print(count)
			flag=4

		if flag==4 and count==28:
			ser.write(rbuff)
			#print(count)
			count=0
			flag=0
			if rbuff[0]-2 <= temp <= rbuff[0]+2:
				GPIO.output(12,GPIO.LOW)
				#ser.write(ord(buff))
			else:
				GPIO.output(12,GPIO.HIGH)
				temp=rbuff[0]
				# ser.write(ord(buff))

		if count == 1 and rbuff[0] != 0x01:
			count_enable=0
			count=0
			flag=0

		if time.time() - start2 > 20: # light led for 20 seconds
			GPIO.output(12,GPIO.LOW)


def thread_check():
    global start1
    while True:
        if time.time() - start1 >= 1:
            start1 = time.time()
#            print (GPIO.input(12))

read_thread = threading.Thread(target=thread_read)
read_thread.start()

check_thread = threading.Thread(target=thread_check)
check_thread.start()

event=threading.Event()
try:
    event.wait()
except KeyboardInterrupt:
    pass





Return to “Python”