0
I’m doing a Plot temperature vs Time via Arduino in a graphical interface. The program is crashing. Problem is in the function of time:
# -*- coding: utf-8 -*-
import matplotlib
import matplotlib.pyplot as plt
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import*
from PyQt4.QtGui import* # create winodow
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import numpy as np
import time
import pause
import matplotlib
import sys
from PyQt4 import QtGui
import serial
temperaturaLM35 = []
time_array = []
port = 'COM31'
velocity = 9600
tempo = 1
arduino = serial.Serial(port,velocity)
cnt = 0
class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50, 50, 600, 300)
self.setWindowTitle("Test Window for PyQt")
self.setWindowIcon(QtGui.QIcon('logo_python.png'))
QtGui.QApplication.setStyle(QtGui.QStyleFactory.create("Plastique"))
pg.setConfigOption('background', 'w')
pg.setConfigOption('foreground', 'k')
self.plot5 = pg.PlotWidget(self)
self.plot5.setTitle("TEMPERATURE LIVE PLOT")
self.plot5.setXRange(0, 11, padding=0, update = True)
self.plot5.setYRange(0, 50, padding=0, update = True)
self.plot5.setLabel('left','Temperature',units='ºC',
color='#c4380d',**{'font-size':'11pt'})
self.plot5.resize(500,200)
self.plot5.move(30,30)
self.plot5.showGrid(x = True, y = True, alpha = 0.7)
self.plot5.setLabel('bottom','Time',units='s',**{'font-size':'11pt'})
self.plot5.getAxis('left').setPen(pg.mkPen('#c4380d',widht=1.1))
self.plot5.setLabels(left=('Temperature', '°C'))
self.plot5.setLabels(bottom=('Time', 's'))
self.plot5.enableAutoRange('xy', True)
self.plot5.plot(temperaturaLM35, pen='b', symbol='o', symbolPen ='b', symbolSize = 5)
self.home()
def home(self):
global tempo
self.cb = QCheckBox(" ", self)
self.cb.move(5, 5)
self.cb.stateChanged.connect(self.makeFig)
self.btn1 = QPushButton("tempo",self)
self.btn1.move(10,250)
self.le1 = QLineEdit(self)
self.le1.setReadOnly(True)
self.le1.setFixedWidth(40)
self.le1.move(120,250)
self.btn1.clicked.connect(self.tempo1)
self.show()
def monitorar(self):
if self.checkbox.isChecked():
self.sensorTempLM35()
self.makeFig()
QtCore.QCoreApplication.processEvents()
pause.seconds(tempo)
self.plot5.plot(time_array, temperaturaLM35, pen='b', symbol='o', symbolPen ='b', symbolSize = 5)
else:
event.ignore()
def tempo1(self):
global tempo
tempo,ok = QInputDialog.getDouble(self, "Time","Enter the value of time (s):",tempo, 0.01, 10000, 2)
if ok:
self.le1.setText(str(tempo))
def sensorTempLM35(self):
global temperaturaLM35
arduino.write('x')
def makeFig(self):
global temperaturaLM35
global time_array
arduinoNumber = float(arduino.readline()[:-1])
timer = QElapsedTimer()
timer2 = np.round(np.float(timer.elapsed())/1000, decimals=2)
temperaturaLM35.append(arduinoNumber)
time_array.append(tempo)
cnt = cnt + 1
print temperaturaLM35
print time_array
self.plot5.plot( temperaturaLM35, pen='b', symbol='o', symbolPen ='b', symbolSize = 5)
if(cnt > 50):
temperaturaLM35.pop(0)
time_array.pop(0)
else:
pass
arduino.close()
def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
run()
Locking? You mean it loops?
– Leonardo Pessoa
When I call the checkbox to start the acquisition the program hangs. I believe it loops, but tbm this data acquisition time function may not be correct.. and the error I’m not finding
– Jose Bruno Cantuaria