I am trying to plot a data coming from a photocell sensor in the real-time, is there anyway to do that by using matplotlib? I'm trying to find how to do that but I didn't find a way to plot my data directly from the GPIO bin without taking the data from a txt file.
Many thanks in advance
Here's my code:
#!/usr/local/bin/python
## Reading data from a photocell sensor
import RPi.GPIO as GPIO
import time
# Tell the GPIO library to use Broadcom GPIO references
GPIO.setmode(GPIO.BOARD)
#define the pin that goes to the circuit
Pin = 7
def RCtime (Pin):
measurement = 0
#Output on the pin for # Discharge capacitor
GPIO.setup(Pin, GPIO.OUT)
GPIO.output(Pin, GPIO.LOW)
time.sleep(0.0001)
GPIO.setup(Pin, GPIO.IN)
# Count loops until voltage across capacitor reads high on GPIO
while (GPIO.input(Pin) == GPIO.LOW):
measurement += 1
return measurement
# Main program loop
i = 1
while True:
file1 = open("Data%i.txt" %i ,"w")
i += 1
c = 1
while c <= 50:
print RCtime (Pin)*1.000
c += 1
file1.write(str(RCtime (Pin)))
file1.write("\n")
else:
file1.close()
I edited the code as below, now I'm getting this error:
File "/home/pi/Qt_Project/plot.py", line 43
for RCtime (Pin) in range(0, 500):
SyntaxError: can't assign to function call
The new code:
import RPi.GPIO as GPIO
import time
import matplotlib.pyplot as plt
import threading
GPIO.setmode(GPIO.BOARD)
Pin = 7
threading.Timer(2, RCtime (Pin)).start()
def RCtime (Pin):
measurement = 0
GPIO.setup(Pin, GPIO.OUT)
GPIO.output(Pin, GPIO.LOW)
time.sleep(0.0001)
GPIO.setup(Pin, GPIO.IN)
while (GPIO.input(Pin) == GPIO.LOW):
measurement += 1
return measurement
i = 1
while True:
file1 = open("Data%i.txt" %i ,"w")
i += 1
c = 1
while c <= 50:
print RCtime (Pin)*1.000
c += 1
file1.write(str(RCtime (Pin)))
file1.write("\n")
else:
file1.close()
figure, axis = plt.subplots(figsize=(9, 9))
for RCtime (Pin) in range(0, 500):
axis.plot(time.time(), RCtime (Pin), '0-')
plt.show()
Why not something like
import matplotlib.pyplot as plt
figure, axis = plt.subplots(figsize=(7.6, 6.1))
for x in range(0, 500):
axis.plot(x, x*2, 'o-')
plt.show()
combined with
import threading
threading.Timer(2, RCtime (Pin)).start() #calls func "RCtime" every 2 sec
Or perform a self recursion (whilst avoiding recursion depth exceeding..)
Greets Dr. Cobra
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.