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. Qt GUI to control Arduino value(How to get second value directly)?
Forum Updated to NodeBB v4.3 + New Features

Qt GUI to control Arduino value(How to get second value directly)?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.9k 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.
  • M Offline
    M Offline
    Macive Xiong
    wrote on 8 Aug 2016, 04:41 last edited by Macive Xiong 8 Aug 2016, 04:49
    #1

    Hey guys,

    I am doing the project that I want to input values to control Buzzer's voltage and frequency for three-axes. Here are my Arduino code:

    void loop(){
    if (Serial.available()){
          char Axis_pick = Serial.read();
          float Voltage = Serial.parseFloat();
          int Frequency = Serial.parseInt();
          write_Try(Axis_pick, Voltage, Frequency);
    }
    
    void write_Try(char Axis, float Volt, int Fre){
      if(Axis == 'x'){
        analogWrite(XPin, Volt * 51.48);
        return;
      }
      if(Axis == 'y'){
        analogWrite(YPin, Volt * 51.48);
        return;
      }
      if(Axis == 'z'){
        tone(ZPin, Fre);
        return;
      }
    

    As you see, I got two values here(volt and frequency) to read, and I know how to get the voltage(first value I read from serial) from Qt to Arduino, my Qt code is:

    void MainWindow::on_Scan_btn_clicked()
    {
    MainWindow::updatetry(QString("x%1").arg(ui->X_nm->value()));
    MainWindow::updatetry(QString("y%1").arg(ui->Y_nm->value()));
    }
    

    But, my problem is, in my X and Y axes, I get the value from entering number in Qt to control Arduino, it works fine which is controlling voltage output to Buzzers. But in my Z axis,

    MainWindow::updatetry(QString("z%1").arg(ui->Z_Fre->value()));
    

    Since my Arduino code for Z axis is only for Frequency control, not Voltage, but my Qt code here can only control Voltage which is the first value that serial read.

    So my question is, how can I get the value of Second value (frequency) from Qt in Z axis to control frequency on Z axis? I have tried to modify my code to

    MainWindow::updatetry(QString("z%2").arg(ui->Z_Fre->value()));
    

    Since if want to control multiple values can do "%1%2%3", and my frequency serial read is the second one.
    But it still reads voltage....
    Please give me some suggestion.

    All the best,
    Macive

    J 1 Reply Last reply 8 Aug 2016, 05:13
    0
    • M Macive Xiong
      8 Aug 2016, 04:41

      Hey guys,

      I am doing the project that I want to input values to control Buzzer's voltage and frequency for three-axes. Here are my Arduino code:

      void loop(){
      if (Serial.available()){
            char Axis_pick = Serial.read();
            float Voltage = Serial.parseFloat();
            int Frequency = Serial.parseInt();
            write_Try(Axis_pick, Voltage, Frequency);
      }
      
      void write_Try(char Axis, float Volt, int Fre){
        if(Axis == 'x'){
          analogWrite(XPin, Volt * 51.48);
          return;
        }
        if(Axis == 'y'){
          analogWrite(YPin, Volt * 51.48);
          return;
        }
        if(Axis == 'z'){
          tone(ZPin, Fre);
          return;
        }
      

      As you see, I got two values here(volt and frequency) to read, and I know how to get the voltage(first value I read from serial) from Qt to Arduino, my Qt code is:

      void MainWindow::on_Scan_btn_clicked()
      {
      MainWindow::updatetry(QString("x%1").arg(ui->X_nm->value()));
      MainWindow::updatetry(QString("y%1").arg(ui->Y_nm->value()));
      }
      

      But, my problem is, in my X and Y axes, I get the value from entering number in Qt to control Arduino, it works fine which is controlling voltage output to Buzzers. But in my Z axis,

      MainWindow::updatetry(QString("z%1").arg(ui->Z_Fre->value()));
      

      Since my Arduino code for Z axis is only for Frequency control, not Voltage, but my Qt code here can only control Voltage which is the first value that serial read.

      So my question is, how can I get the value of Second value (frequency) from Qt in Z axis to control frequency on Z axis? I have tried to modify my code to

      MainWindow::updatetry(QString("z%2").arg(ui->Z_Fre->value()));
      

      Since if want to control multiple values can do "%1%2%3", and my frequency serial read is the second one.
      But it still reads voltage....
      Please give me some suggestion.

      All the best,
      Macive

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 8 Aug 2016, 05:13 last edited by
      #2

      @Macive-Xiong Not sure whether I understand your description. If you want to send three values at once then do it like this:

      MainWindow::updatetry(QString("%1%2%3").arg(ui->X_nm->value()).arg(ui->Y_nm->value()).arg(ui->Z_Fre->value()));
      

      How does MainWindow::updatetry() look like?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply 8 Aug 2016, 07:38
      0
      • J jsulm
        8 Aug 2016, 05:13

        @Macive-Xiong Not sure whether I understand your description. If you want to send three values at once then do it like this:

        MainWindow::updatetry(QString("%1%2%3").arg(ui->X_nm->value()).arg(ui->Y_nm->value()).arg(ui->Z_Fre->value()));
        

        How does MainWindow::updatetry() look like?

        M Offline
        M Offline
        Macive Xiong
        wrote on 8 Aug 2016, 07:38 last edited by Macive Xiong 8 Aug 2016, 07:46
        #3

        @jsulm Thanks for your reply. Here are my MainWindow::updatetry code:

        void MainWindow::updatetry(QString command)
        {
            if(arduino->isWritable()){
                arduino->write(command.toStdString().c_str());
            }else{
                qDebug() << "Couldn't write to serial!";
            }
        }
        
        void MainWindow::on_Scan_btn_clicked()
        {
            MainWindow::updatetry(QString("x%1").arg(ui->X_nm->value()));
            MainWindow::updatetry(QString("y%1").arg(ui->Y_nm->value()));
            MainWindow::updatetry(QString("z%1").arg(ui->Z_Fre->value()));
        
        

        When I click the button then the value I input would send to Arduino and output to Buzzers. But since I want to output voltage to X and Y, frequency to Z, QString("z%1") is not working because %1 is for voltage and I don't want to output voltage to Z axis. Please see:

        void loop(){
        if (Serial.available()){
              char Axis_pick = Serial.read(); 
              float Voltage = Serial.parseFloat(); // first value
              int Frequency = Serial.parseInt(); // second value
              write_Try(Axis_pick, Voltage, Frequency);
        }
        
        void write_Try(char Axis, float Volt, int Fre){
          if(Axis == 'x'){
            analogWrite(XPin, Volt * 51.48);  //"X%1"
            return;
          }
          if(Axis == 'y'){
            analogWrite(YPin, Volt * 51.48); //"Y%1"
            return;
          }
          if(Axis == 'z'){
            tone(ZPin, Fre); //"Z%2"??
            return;
          }
        

        Is there any way to control frequency to Z axis directly? Without output first value (voltage). Thanks.

        J 1 Reply Last reply 8 Aug 2016, 08:05
        0
        • M Macive Xiong
          8 Aug 2016, 07:38

          @jsulm Thanks for your reply. Here are my MainWindow::updatetry code:

          void MainWindow::updatetry(QString command)
          {
              if(arduino->isWritable()){
                  arduino->write(command.toStdString().c_str());
              }else{
                  qDebug() << "Couldn't write to serial!";
              }
          }
          
          void MainWindow::on_Scan_btn_clicked()
          {
              MainWindow::updatetry(QString("x%1").arg(ui->X_nm->value()));
              MainWindow::updatetry(QString("y%1").arg(ui->Y_nm->value()));
              MainWindow::updatetry(QString("z%1").arg(ui->Z_Fre->value()));
          
          

          When I click the button then the value I input would send to Arduino and output to Buzzers. But since I want to output voltage to X and Y, frequency to Z, QString("z%1") is not working because %1 is for voltage and I don't want to output voltage to Z axis. Please see:

          void loop(){
          if (Serial.available()){
                char Axis_pick = Serial.read(); 
                float Voltage = Serial.parseFloat(); // first value
                int Frequency = Serial.parseInt(); // second value
                write_Try(Axis_pick, Voltage, Frequency);
          }
          
          void write_Try(char Axis, float Volt, int Fre){
            if(Axis == 'x'){
              analogWrite(XPin, Volt * 51.48);  //"X%1"
              return;
            }
            if(Axis == 'y'){
              analogWrite(YPin, Volt * 51.48); //"Y%1"
              return;
            }
            if(Axis == 'z'){
              tone(ZPin, Fre); //"Z%2"??
              return;
            }
          

          Is there any way to control frequency to Z axis directly? Without output first value (voltage). Thanks.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 8 Aug 2016, 08:05 last edited by
          #4

          @Macive-Xiong How about:

          void loop(){
          if (Serial.available()){
                char Axis_pick = Serial.read(); 
                float Voltage = Serial.parseFloat(); // first value
                write_Try(Axis_pick, Voltage);
          }
          
          void write_Try(char Axis, float Volt){
            if(Axis == 'x'){
              analogWrite(XPin, Volt * 51.48);  //"X%1"
              return;
            }
            if(Axis == 'y'){
              analogWrite(YPin, Volt * 51.48); //"Y%1"
              return;
            }
            if(Axis == 'z'){
              tone(ZPin, (int)Volt);
              return;
            }
          

          You handle Z achsis in the same way as X/Y, you just use the value differently.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply 8 Aug 2016, 08:22
          0
          • J jsulm
            8 Aug 2016, 08:05

            @Macive-Xiong How about:

            void loop(){
            if (Serial.available()){
                  char Axis_pick = Serial.read(); 
                  float Voltage = Serial.parseFloat(); // first value
                  write_Try(Axis_pick, Voltage);
            }
            
            void write_Try(char Axis, float Volt){
              if(Axis == 'x'){
                analogWrite(XPin, Volt * 51.48);  //"X%1"
                return;
              }
              if(Axis == 'y'){
                analogWrite(YPin, Volt * 51.48); //"Y%1"
                return;
              }
              if(Axis == 'z'){
                tone(ZPin, (int)Volt);
                return;
              }
            

            You handle Z achsis in the same way as X/Y, you just use the value differently.

            M Offline
            M Offline
            Macive Xiong
            wrote on 8 Aug 2016, 08:22 last edited by
            #5

            @jsulm Thank you so much. This is brilliant. It totally works.
            So basically, we can't pass over the %1 and go control %2 directly in Qt right?
            Or is there any other way for doing this??

            J 1 Reply Last reply 8 Aug 2016, 08:27
            0
            • M Macive Xiong
              8 Aug 2016, 08:22

              @jsulm Thank you so much. This is brilliant. It totally works.
              So basically, we can't pass over the %1 and go control %2 directly in Qt right?
              Or is there any other way for doing this??

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 8 Aug 2016, 08:27 last edited by
              #6

              @Macive-Xiong This is not related to %1/%2 at all. %1 means - the first parameter passed by first arg(), %2 addresses the second arg() and so on. If you write QString("%2").arg("something") then it will not work as there is only one parameter, not two.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              1/6

              8 Aug 2016, 04:41

              • Login

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