I am having trouble trying to get my program to continually send the string "move 200"
while I hold down a button. I have the button set to auto repeat however it only sends once the button is released not while it is holding down. However while being held down the counter is adding how many times the message should have been sent. I am at a lost.
mainwindow.cpp
void MainWindow::on_forwardButton_clicked()
{
if(arduino->isWritable()){
arduino->write(command.toStdString().c_str());
qDebug() << i;
}else{
qDebug() << "Couldn't write to serial!";
}
ui->label->setText("Moving");
i++;
}
mainwindow.h
ifndef MAINWINDOW_H
define MAINWINDOW_H
include <QMainWindow>
include <QDialog>
include <QSerialPort>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_forwardButton_clicked();
private:
Ui::MainWindow *ui;
QSerialPort *arduino; //makes arduino a pointer to the SerialPort
bool arduino_is_available;
QString command = "move 200";
bool buttonReleased = false;
};
endif // MAINWINDOW_H
Code added following @dtech suggestion
pButtonTimer = new QTimer;
connect(pButtonTimer, SIGNAL(timeout()), this, SLOT(sendData()));
int i = 0;
void MainWindow::on_forwardButton_pressed()
{
pButtonTimer->start(1000);
ui->label->setText("Moving");
qDebug() << "Button Pushed";
}
void MainWindow::on_forwardButton_released()
{
pButtonTimer->stop();
}
void MainWindow::sendData(){
i++; //used to count how many times the command should have been sent
qDebug() << i << "sendData is running"; //notifies me the function has been called
if(arduino->isWritable()){
arduino->write(command.toStdString().c_str());
qDebug() << i << "arduino is writable with command " << command; //lets me know the Arduino is writable
}
else{qDebug() << "Couldn't write to serial!";}
}
After releasing the button the serial monitor in the Arduino then shows everything sent and the robot moves
I suggest you expand on your design somewhat:
QTimer
with an interval depending on the rate you want to send the string at, and the timer to the function that sends the stringEvents are sent only once, thus the handlers will be executed only once, if you want to keep on repeating it, you will have to use a timer or some other event driven way. You cannot use a loop as that would block the GUI thread and your application will stop responding.
Sure, you could use the button's auto repeat, and there is the option to adjust the triggering and repeating intervals, but a solution that puts a line between logic and GUI is better. You should really rely on the GUI for storing data or controlling the internal logic. The GUI should only be a front end.
You need more work on the serial port though. If you are going to use it from the GUI thread, you will have to use the non-blocking API. Which will require to extend on your implementation a little bit more. There is a good example on how to achieve that, you only need to modify it to simply enable the sending of further payloads once the previous payload has been successfully sent. In pseudo code:
Of course, you will also have to do some error checking, you can't just expect it to work. The example linked contains basic error handling.
From the docs:
So please use pressed/released instead of clicked.
clicked
is send once a mouse clicks the button, maybe after it was released. I don't know how Qt "knows" to handle single and double clicks.pressed
is send by the "push down" action andreleased
by the release action. So simply set your flag accordingly to the both signals.BTW: You have to use some kind of loop around you sending function, typically calls periodically or always if your file io becomes writeable. Simply firing on a io will not do what you expect.
A "blind" or unconfirmed auto-repeat is not a good idea, because presumably it takes the Arduino some time to react to the command. Given that by default you have no flow control anywhere, you'll overflow the buffers along the way - in the USB-to-serial chip (if any), and also in Arduino. Since your packets (lines) have no error checking, you'll end up executing junk commands on Arduino, with varying effects.
At the very minimum, the Arduino should send a message indicating that a command was finished. It can be a simple
Serial.println("OK")
. You would then send the next command as soon as you receive the successful reply.This slows things down a bit since the next command can only be processed after you've finished receiving the reply and finished sending the command. Instead, you can pre-send one or more commands ahead of time, so that the Arduino is always busy.
We can leverage Qt to concisely model both the PC side of it, as well as Arduino.
A complete example follows, written in the literate programming style.
First, we'll need a local pipe to communicate between the PC and the mockup Arduino. This is much easier than using
QLocalServer
.To manage the communications, the controller allows up to two commands "in flight" at any given time. This is a very simple controller - in production code, we should have an explicit state machine that would allow error handling etc. See e.g. this question.
A user interface provides a button and a status indicator:
We're mostly done with the PC side of things - the test setup will come later, inside of
main
.We now turn to the Arduino side, and mock up a minimal Arduino environment. Recall that the Arduino "language" is really C++11! We implement Arduino functionality using Qt classes.
We can now write the Arduino code, exactly as it would appear on the real Arduino. The
LineEditor
is a class I find missing in Arduino - it provides asynchronous input tokenization, and allows interactive line editing whenTTY
is set. When run on an actual Arduino, you could callLine.setTTY(true)
and connect to the Arduino via PUTTY or any other terminal program. Yes - PUTTY is a general-purpose terminal that can connect to a serial port.An adapter class executes the Arduino environment:
Finally, we set up the test and connect all of the involved components. The
Arduino
object runs in its own thread.This concludes the example. You can copy-paste it into an empty
main.cpp
, or you can fetch the complete project from github.