Unsolved Qt GUI to control Arduino value(How to get second value directly)?
-
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 -
@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?
-
@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.
-
@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.
-
@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?? -
@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.