Potentialmeter AnalogRead from Arduino to graph in Qt



  • 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:)



  • Hi @Macive-Xiong,
    Nice project test... would be happy to see your final code to.

    Maybe do use QSerialPort class (which other one for catch serial signal?).
    From there the design could be (but it is an arbitrary choice, maybe there is better ideas)
    1/ to use a timer to go read the port
    or
    2/ to directly from arduino send event to be read from his own timer...
    depend witch one you want to be the master who control the cadence (i think that trust arduino should be more stable and precise for timing also).

    Also from there, hang a signal to a slot who will convert the data to a coordinate point to plot on the graph, then link them or not depend of a graphic option for render them (just add a switch condition on a enum variable define from an optional dialogBox or what ever UI input linked with signal/slot to for dynamically define them... ellipse, or plot points, or lines, or... what ever...), and act for a scale render on x and/or y (y should be the time, x should be the signal amplitude collected).

    so from your code i see, maybe you need first to consider the design pattern... and maybe use and update graphical member function by pass the amplitude signal and make this update function to be a slot. then link it with a signal occur from the event... event from arduino emit signal, or event from a timer (thread ?) initialized that will be linked to a slot for catch the signal value, and print/update it finally.

    so i would maybe choose also to define brushes as private global variables... for win time to not have to declare/define them each time a signal is catch for update, then this memory part stay ready all the time and reduce maybe the process occurrences.

    i think then that design pattern for this kind of project is crucial for find the nice way because signal and slot is design of powerfully use Qt framework.



  • Hi @jerome_isAviable ,

    Thank you so much for your reply. Yes, I am connecting my Arduino with Qt thru QSerialPort. But I have trouble to receive the signal from Arduino to Qt. I suppose to put some command in my qt and let qt knows when to get the signal of potentialmeter. So my question is, what kind of the command I should put in my Qt for receiving the potentialmeter's signal?? Is the signal and slot command work ?? Could you please give me more information and suggestion or some sample ??

    Thank you so much.


  • Lifetime Qt Champion

    Hi,

    Then first question, what command are you supposed to send to your Arduino for it to return potentiometer data ?



  • @Macive-Xiong

    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.



  • 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!



  • @rosietesmen

    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?



  • @Macive-Xiong

    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 your updateP function which accepts a QString 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.



  • @Macive-Xiong

    Assuming the QByteArray has come from a QSerialPort and contains only one float value represented as a string (you really should check all of these assumptions!) then getting a float 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(){}
    


  • @jazzycamel

    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.



  • This post is deleted!


  • 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:)



  • This post is deleted!

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.