I have set up a little script that, after checking the distance with a ultrasonic sensor (HC-SR04), do the led blink if the distance is less of 8 cm.
My setup is made by 4 MCP23S17 chips with a 5v external power supply, connected to the raspberry with mosfets (SPI), and the ultrasonic sensor connected to one chip (with a resistor to pull down the echo pin).
Everything works really well exept 2 things.
1- If I don't insert this: 'stop = time.time()' as first line of 'def measure()' I have this error (but not always....just random.... ):
python UnboundLocalError: local variable 'stop' referenced before assignment
I have copy/paste this script from http://www.raspberrypi-spy.co.uk/2013/0 ... n-part-2/' .... so I think that it must works well... and the error is random.... really strange.....
2- The script after run for a while (5 minutes....10 minutes) hang up....2/3 /10 minutes (no error messages), and after, start again to works.... really, really extrange.....
When the process hang up, the cpu use go at 50%.... Normally is it at 1% level.
Could be these problems related to the SPI buffer or the frequency of the MCP23S17 that it is not enough?
edit: I have change this value in the mcp23s17.c file:
#define MCP_SPEED 4000000
#define MCP_SPEED 10000000
Now my chips speed is 10Mhz? Correct? ...or I need to do something else?
I'm testing it..... I will give you a feedback in 1 hour..
Edit2: Doesn't works....
Please help me, because I don't really know how to fix it
Thank you in advance
Code: Select all
import wiringpi2 as wiringpi from time import sleep import time wiringpi.wiringPiSetup() # initialise wiringpi wiringpi.mcp23s17Setup(65,0,0) # set up the pins, spi port, address (mcp23s17-0) wiringpi.mcp23s17Setup(81,0,1) # set up the pins, spi port, address (mcp23s17-1) wiringpi.mcp23s17Setup(97,1,0) wiringpi.mcp23s17Setup(113,1,1) wiringpi.pinMode(104,0) wiringpi.digitalWrite(104,0) wiringpi.pinMode(119,0) wiringpi.digitalWrite(119,0) wiringpi.pinMode(72,1) wiringpi.digitalWrite(72,0) wiringpi.pinMode(121,1) wiringpi.digitalWrite(121,0) wiringpi.pinMode(88,1) wiringpi.digitalWrite(88,0) wiringpi.pinMode(128,0) wiringpi.pinMode(127,1) wiringpi.digitalWrite(127,0) GPIO_TRIGGER = 127 GPIO_ECHO = 128 def blinking_led1_button1_er1(): wiringpi.digitalWrite(88,1) wiringpi.digitalWrite(72,1) sleep(0.5) wiringpi.digitalWrite(88,0) wiringpi.digitalWrite(72,0) sleep(0.5) wiringpi.digitalWrite(88,1) wiringpi.digitalWrite(72,1) sleep(0.5) wiringpi.digitalWrite(88,0) wiringpi.digitalWrite(72,0) sleep(0.5) wiringpi.digitalWrite(88,1) wiringpi.digitalWrite(72,1) sleep(0.5) wiringpi.digitalWrite(88,0) wiringpi.digitalWrite(72,0) sleep(0.5) def measure(): # This function measures a distance stop = time.time() wiringpi.digitalWrite(GPIO_TRIGGER, 1) time.sleep(0.00001) wiringpi.digitalWrite(GPIO_TRIGGER, 0) start = time.time() while wiringpi.digitalRead(GPIO_ECHO)==0: start = time.time() while wiringpi.digitalRead(GPIO_ECHO)==1: stop = time.time() elapsed = stop-start distance = (elapsed * 34300)/2 return distance def measure_average(): # This function takes 5 measurements and # returns the average. distance1=measure() time.sleep(0.1) distance2=measure() time.sleep(0.1) distance3=measure() time.sleep(0.1) distance4=measure() time.sleep(0.1) distance5=measure() time.sleep(0.1) distance = distance1 + distance2 + distance3 + distance4 + distance5 distance = distance / 5 return distance while True: distance = measure_average() if distance > 8: if wiringpi.digitalRead(104) == 1 or wiringpi.digitalRead(119) == 1: blinking_led1_button1_er1() else: wiringpi.digitalWrite(121,1) time.sleep(0.5) wiringpi.digitalWrite(121,0) time.sleep(0.5) time.sleep(0.1) # Wrap main content in a try block so we can # catch the user pressing CTRL-C and run the # GPIO cleanup function. This will also prevent # the user seeing lots of unnecessary error # messages. # distance = measure_average() # print "Ultrasonic Measurement" # print "Distance : %.1f" % distance # if distance < 8: # wiringpi.digitalWrite(121,1) # else: # wiringpi.digitalWrite(121,0) # time.sleep(2)