Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Potentialmeter AnalogRead from Arduino to graph in Qt

Potentialmeter AnalogRead from Arduino to graph in Qt

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 5 Posters 7.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • jazzycamelJ Offline
    jazzycamelJ Offline
    jazzycamel
    wrote on last edited by
    #7

    @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.

    For the avoidance of doubt:

    1. All my code samples (C++ or Python) are tested before posting
    2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
    1 Reply Last reply
    2
    • Macive XiongM Offline
      Macive XiongM Offline
      Macive Xiong
      wrote on last edited by
      #8

      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?

      1 Reply Last reply
      0
      • jazzycamelJ Offline
        jazzycamelJ Offline
        jazzycamel
        wrote on last edited by
        #9

        @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();
        }
        

        For the avoidance of doubt:

        1. All my code samples (C++ or Python) are tested before posting
        2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
        Macive XiongM 1 Reply Last reply
        3
        • jazzycamelJ jazzycamel

          @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();
          }
          
          Macive XiongM Offline
          Macive XiongM Offline
          Macive Xiong
          wrote on last edited by Macive Xiong
          #10

          @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.

          1 Reply Last reply
          0
          • jazzycamelJ Offline
            jazzycamelJ Offline
            jazzycamel
            wrote on last edited by jazzycamel
            #11

            @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(){}
            

            For the avoidance of doubt:

            1. All my code samples (C++ or Python) are tested before posting
            2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
            Macive XiongM 1 Reply Last reply
            2
            • jazzycamelJ jazzycamel

              @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(){}
              
              Macive XiongM Offline
              Macive XiongM Offline
              Macive Xiong
              wrote on last edited by Macive Xiong
              #12

              @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.

              1 Reply Last reply
              0
              • jazzycamelJ Offline
                jazzycamelJ Offline
                jazzycamel
                wrote on last edited by
                #13

                @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.

                For the avoidance of doubt:

                1. All my code samples (C++ or Python) are tested before posting
                2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
                Macive XiongM 3 Replies Last reply
                0
                • jazzycamelJ jazzycamel

                  @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 XiongM Offline
                  Macive XiongM Offline
                  Macive Xiong
                  wrote on last edited by Macive Xiong
                  #14
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • jazzycamelJ jazzycamel

                    @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 XiongM Offline
                    Macive XiongM Offline
                    Macive Xiong
                    wrote on last edited by Macive Xiong
                    #15

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

                    1 Reply Last reply
                    0
                    • jazzycamelJ jazzycamel

                      @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 XiongM Offline
                      Macive XiongM Offline
                      Macive Xiong
                      wrote on last edited by Macive Xiong
                      #16
                      This post is deleted!
                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved