Potentialmeter AnalogRead from Arduino to graph in Qt
-
Firmata was designed to do exactly this. You basically load a standard sketch (StandardFirmata) on to the Arduino and then either use a client library or implement the simple protocol on the host computer to send commands and receive data.
There are a few different implementations of the client library you could try (FirmataC for example). I wrote a Qt5/C++ implementation a while ago that I've just put on github in case its useful to you/anyone: QtFirmata.
-
Hey guys,
I would like to make this project that I plug Potentialmeter with Arduino (A0) and graph in Qt. I am using Qt Graphics View and I hope I can let the potentialmeter be the variable of x axis or y axis, the ellipse would falls on different place while I change the value of Potentialmeter. Is there any information or suggestion about how can I read analog signal from Arduino in Qt??
My Arduino code:
void setup() { Serial.begin(9600); } void loop() { float sensorValue = analogRead(A0); Serial.println(sensorValue); delay(100); // delay in between reads for stability }
I think I should keep receiving signals from Arduino and place the ellipse down on my Graphics View canvas.
Qt code:
void MainWindow::updatepoistion(QString command) { QBrush redBrush(Qt::red); QBrush blueBrush(Qt::blue); QPen blackpen(Qt::black); blackpen.setWidth(1); ellipse = scene->addEllipse(x, 100, 10, 10,blackpen,redBrush); //any suggestion in here?? }
Is there any suggestion on my Qt code and Arduino code?
Thanks so much:)
I already know how to connect Qt and Arduino, But I don't know how to make it plot in Qt or display number in Qt. I have tried the qt code from this example: https://www.youtube.com/watch?v=AX-HhBXBzGg#t=511.742042, changed the temp sensor to potentiometer, but it didn't work. I used LCDNumber to display, but I don't know what codes should I write in LCDNumber to read the values from Arduino. Hope you guys could give me some suggestion or example. Thanks!
-
How are you getting the data from the Arduino? Are you using a library or code you have written yourself? Show us your (Qt/Arduino) code and we can be more help.
If you're using Firmata then data for pins configured as inputs is constantly being streamed to the host PC. Any good client (like mine QtFirmata :) ) will parse this data and store it for you. You then periodically check this data (via a timer for example) and update your plot.
As to plots, there are a number of very good libraries for plotting with Qt which you should definitely look at (QCustomPlot, Qwt). I've put a very basic example of drawing a custom plot (that I actually use for teaching the basics of drawing, not plotting!) on my GitHub in case it helps anyone: QtPlot.
-
Hey guys, thank you so much for your reply. I've been trying so hard on this project but I stuck on here.
void MainWindow::updateP(QString command) { if(arduino->isReadable()){ arduino->readAll(); }else{ qDebug() << "Couldn't write to serial!"; } QBrush redBrush(Qt::red); QBrush blueBrush(Qt::blue); QPen blackpen(Qt::black); blackpen.setWidth(1); ellipse = scene->addEllipse(x, 100, 10, 10,blackpen,redBrush); } void MainWindow::readSerial() { MainWindow::updateP(ui->graphicsView->scene()); }
I am trying to use the updateP for updating my potentialmeter's signal. I don't know what's the next step and how can I modify my code to the right one. The signal from potentialmeter will replace the value on X. How can I set the X = signal read of Potentialmeter?
-
First point is that
arduino->readAll()
will return any data that has been received so you a) need to save it to a variable (QByteArray data=arduino->readAll();
and b) need to process/parse it to retrieve the value.Second point is that you seem to be passing a
QGraphicsScene
object to yourupdateP
function which accepts aQString
argument.Thirdly, you seem to be calling your updateP function as if it was static, why? Is it defined this way?
Finally, you say you are stuck: what with? Specifics about your application and your current issue help us give you useful answers.
As a footnote, the code above should look something like the following if it is to compile:
void MainWindow::updateP(){ if(arduino->isReadable()) QByteArray data=arduino->readAll(); else { qDebug() << "Couldn't read from serial"; return; } // Parse data here and then create/draw your ellipse } void MainWindow::readSerial(){ this->updateP(); }
-
First point is that
arduino->readAll()
will return any data that has been received so you a) need to save it to a variable (QByteArray data=arduino->readAll();
and b) need to process/parse it to retrieve the value.Second point is that you seem to be passing a
QGraphicsScene
object to yourupdateP
function which accepts aQString
argument.Thirdly, you seem to be calling your updateP function as if it was static, why? Is it defined this way?
Finally, you say you are stuck: what with? Specifics about your application and your current issue help us give you useful answers.
As a footnote, the code above should look something like the following if it is to compile:
void MainWindow::updateP(){ if(arduino->isReadable()) QByteArray data=arduino->readAll(); else { qDebug() << "Couldn't read from serial"; return; } // Parse data here and then create/draw your ellipse } void MainWindow::readSerial(){ this->updateP(); }
@jazzycamel , thank you so much for your reply!! It's really helpful.
I was stuck cause I am not sure how can I get the value of potentialmeter into the ellipse parameter. Is that right if I write this way:void MainWindow::updateP() { if(arduino->isReadable()){ QByteArray X_position = arduino->readAll(); }else{ qDebug() << "Couldn't wrtie to serial!"; return; } //set scene QBrush redBrush(Qt::red); QPen blackpen(Qt::black); blackpen.setWidth(1); float x = X_position; ui->graphicsView->scene()->addEllipse(x , 100, 10, 10, blackpen, redBrush); } void MainWindow::readSeiral() { this->updateP(); }
And I would like to define the data = x, but it still come out errors.
The error message : [release/moc_mainwindow.cpp] Error 1
Is there any suggestion for my code?thank you so much.
-
Assuming the
QByteArray
has come from aQSerialPort
and contains only onefloat
value represented as a string (you really should check all of these assumptions!) then getting afloat
value is relatively simple. The following example shows doing just this and then plotting an ellipse of diameter 20px at position x:widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> class QGraphicsView; class QGraphicsScene; class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); private: QGraphicsView *view; QGraphicsScene *scene; }; #endif // WIDGET_H
widget.cpp
#include <QDebug> #include <QVBoxLayout> #include <QGraphicsView> #include <QGraphicsScene> #include "widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) { this->resize(1024,768); QVBoxLayout *l=new QVBoxLayout(this); scene=new QGraphicsScene(this); scene->setSceneRect(0,0,1024,768); view=new QGraphicsView(scene, this); l->addWidget(view); QByteArray data("255.0"); bool ok=false; float x=data.toFloat(&ok); if(!ok){ qDebug() << "Could not convert" << data << "to float!"; return; } qDebug() << "x:" << x; scene->addEllipse(x, 768/2., 20, 20); } Widget::~Widget(){}
-
Assuming the
QByteArray
has come from aQSerialPort
and contains only onefloat
value represented as a string (you really should check all of these assumptions!) then getting afloat
value is relatively simple. The following example shows doing just this and then plotting an ellipse of diameter 20px at position x:widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> class QGraphicsView; class QGraphicsScene; class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); private: QGraphicsView *view; QGraphicsScene *scene; }; #endif // WIDGET_H
widget.cpp
#include <QDebug> #include <QVBoxLayout> #include <QGraphicsView> #include <QGraphicsScene> #include "widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) { this->resize(1024,768); QVBoxLayout *l=new QVBoxLayout(this); scene=new QGraphicsScene(this); scene->setSceneRect(0,0,1024,768); view=new QGraphicsView(scene, this); l->addWidget(view); QByteArray data("255.0"); bool ok=false; float x=data.toFloat(&ok); if(!ok){ qDebug() << "Could not convert" << data << "to float!"; return; } qDebug() << "x:" << x; scene->addEllipse(x, 768/2., 20, 20); } Widget::~Widget(){}
Hey, thanks for your help. I've tried the information you gave and I still got this error message:
:-1: error: [release/moc_mainwindow.cpp] Error 1
Here's my code:
void MainWindow::updateP() { const QByteArray XP = arduino->readAll(); bool ok = false; float x = XP.toFloat(&ok); if(!ok){ qDebug() << "Could not convert" << XP << "to float!"; return; } //set scene QBrush redBrush(Qt::red); QPen blackpen(Qt::black); blackpen.setWidth(1); qDebug() << "x:" << x; scene->addEllipse(x , 100, 10, 10, blackpen, redBrush); } void MainWindow::readSeiral() { this->updateP(); }
Also, I added the signal and slots code:
QObject::connect(arduino, SIGNAL(readyRead()), this, SLOT(readSerial()));
Any suggestion? Thank you so much.
-
@Macive-Xiong
If you're using QtCreator, could you go to the 'Compile Output' tab and give a bit more information on the error message? I don't see anything immediately obviously wrong with the code, but this is only a snippet after all and the issue might be elsewhere in your application. -
@Macive-Xiong
If you're using QtCreator, could you go to the 'Compile Output' tab and give a bit more information on the error message? I don't see anything immediately obviously wrong with the code, but this is only a snippet after all and the issue might be elsewhere in your application.This post is deleted! -
@Macive-Xiong
If you're using QtCreator, could you go to the 'Compile Output' tab and give a bit more information on the error message? I don't see anything immediately obviously wrong with the code, but this is only a snippet after all and the issue might be elsewhere in your application.Thanks for all you guys help. The code works fine and I have finished that.
My final codes:void MainWindow::updateP() { const QByteArray data = arduino->readAll(); float x = data.toFloat(); qDebug() << "couldn't convert" << data << "to float!"; //set scene QBrush redBrush(Qt::red); QPen blackpen(Qt::black); blackpen.setWidth(1); qDebug() << "x:" << x; ellipse = scene->addEllipse( x , 100, 10, 10, blackpen, redBrush); return; } void MainWindow::readSerial() { this->updateP(); }
I appreciate all your help:)
Please feel free if you have some questions on my project:)
-
@Macive-Xiong
If you're using QtCreator, could you go to the 'Compile Output' tab and give a bit more information on the error message? I don't see anything immediately obviously wrong with the code, but this is only a snippet after all and the issue might be elsewhere in your application.This post is deleted!