arnauinsti21
Posts: 15
Joined: Mon Aug 06, 2018 10:52 am

IR simultaniously

Sun Aug 19, 2018 3:07 pm

Hi everybody!
I'm programming a domotic house and I do really need help.
The thing is that I have (at the moment) an LDR, a stepper motor and an IR reciever.
The stepper motor will run if the light intensity caught by the LDR is not too high, this part of the program works fine, but now I have to tell the raspberry pi to stop the function of the stepper if I press the power button of an old TV remote. I'm doing it with LIRC, but I am stuck and don't really know how to solve it.

The thing is that if IRStatus is True the stepper has to do its thing, and if the IRStatus is False the stepper has not to do its thing, but it has to keep doing the IR loop because the stepper has to reactivate when I press the power button again.

All help is welcome, and I need help emergently, this projects is so important for me.

Thank you very much.

Code: Select all

# coding=utf-8
# sha dimportar les següents llibreries pel
#correcte funcionament de l'estepper

import sys # importa unes system specific parameters and functions
import time
import RPi.GPIO as GPIO 
import socket


SOCKPATH = "/var/run/lirc/lircd"

sock = None

mpin=26
tpin=23
cap=0.000001
adj=2.130620985
i=0
t=0

IREstat = True


GPIO.setmode(GPIO.BCM) #anomena els pins pel nom GPIO, no pel nombre
#trenca = False
StepPins = [17, 19, 27, 22] #array amb la menció dels pins

for pin in StepPins:
  print ("Setup pins")
  GPIO.setup(pin,GPIO.OUT)
  GPIO.output(pin, False)
  
# definir la seqüencia descrita al full tècnic del fabricant  
Seq = [[1,0,0,1],
       [1,0,0,0],
       [1,1,0,0],
       [0,1,0,0],
       [0,1,1,0],
       [0,0,1,0],
       [0,0,1,1],
       [0,0,0,1]]  

StepCount = len(Seq)
#print ("Introdueix el sentit de gir")
#StepDir = int(input(""))          
StepDir = 1

if len(sys.argv)>1:
  WaitTime = int(sys.argv[1])/float(1000)
else:
  WaitTime = 10/float(1000)

#StepCounter = 0

#timeout = time.time() + 5 #5 segons de bucle

def pujada():
				timeout = time.time() + 5 #5 segons de bucle
				StepDir = -1
				test = 0
				StepCounter = 0
				trenca = False
				while not trenca:
					
					print StepCounter
					print Seq[StepCounter]
				
					for pin in range (0,4):
						xpin = StepPins[pin]
						if Seq[StepCounter][pin]!=0:
							GPIO.output (xpin,True)
						else:
							GPIO.output(xpin,False)
					
					StepCounter += StepDir
				
					if StepCounter>=StepCount:
						StepCounter = 0
				
					if StepCounter<0:
						StepCounter = StepCount + StepDir
				
					if test == 5 or time.time() > timeout:
						trenca = True
					test = test - 1	
					time.sleep(WaitTime)


def baixada():
				timeout = time.time() + 5 #5 segons de bucle
				StepDir = 1
				test = 0
				StepCounter = 0
				trenca = False
				while not trenca:
					
					print StepCounter
					print Seq[StepCounter]
				
					for pin in range (0,4):
						xpin = StepPins[pin]
						if Seq[StepCounter][pin]!=0:
							GPIO.output (xpin,True)
						else:
							GPIO.output(xpin,False)
					
					StepCounter += StepDir
				
					if StepCounter>=StepCount:
						StepCounter = 0
				
					if StepCounter<0:
						StepCounter = StepCount + StepDir
				
					if test == 5 or time.time() > timeout:
						trenca = True
					test = test - 1	
					time.sleep(WaitTime)
					
def init_irw():
	global sock
	sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
	print ('Starting up on %s' % SOCKPATH)
	sock.connect(SOCKPATH)
	
init_irw()

def next_key():
	'''Get the next key pressed. Return keyname,updown.
	'''
	while True:
		data = sock.recv (128)
		data = data.strip()
		
		if data:
			break
	
	words = data.split()
	return words[2], words[1]

def IR():
	#if __name__ == '__main__':
	#	init_irw()
	
	keyname, updown = next_key()
	print('%s (%s)' % (keyname, updown))
	time.sleep(1)
	if keyname == 'KEY_POWER' and 'IREstat' == True:
		print "IREstat fals"
		IREstat = False
		time.sleep(1)
	elif keyname == 'KEY_POWER' and 'IREstat' == False:
		print "IREstat verdader"
		IREstat = True
		time.sleep(1)
	
while True:
	#if IREstat == True:
	#	print "IREstat és verdader MAIN"
	#else:
	#	print "IREstat és fals MAIN"
		
	#if IREstat == False:
	#	IR()
	
	#if IREstat == True:
			
	#while IREstat == True:
		
	#while True:
			
		IR()
		GPIO.setup(mpin, GPIO.OUT)
		GPIO.setup(tpin, GPIO.OUT)
		GPIO.output(mpin, False)
		GPIO.output(tpin, False)
		time.sleep(0.2)
		GPIO.setup(mpin, GPIO.IN)
		time.sleep(0.2)
		GPIO.output(tpin, True)
		starttime=time.time()
		endtime=time.time()
		
		
		while (GPIO.input(mpin) == GPIO.LOW):
			endtime=time.time()
			
			
		measureresistance=endtime-starttime
		
		res=(measureresistance/cap)*adj
		i=i+1
		t=t+res
		if i==10:
			
				t=t/i
				
				print(t)
				
				if t > 4000 and IREstat == True:
					pujada()
					time.sleep(5)
					baixada()
					time.sleep(5)
					#i=0
					#t=0
				i=0
				t=0
		time.sleep(1)
            
 

Return to “Python”

Who is online

Users browsing this forum: petermeigs and 13 guests