Или вы можете получить весь проект с GitHub.
я возникают проблемы при попытке заставить мою программу постоянно отправлять строку"move 200"
пока я удерживаю кнопку. У меня установлена кнопка автоповтора, но она отправляется только после того, как кнопка отпущена, а не при ее удержании. Однако при удерживании счетчик добавляет, сколько раз сообщение должно было быть отправлено. Я в растерянности.
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
Код добавлен после предложения @dtech
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!";}
}
После отпускания кнопки последовательный монитор в Arduino показывает все отправленное и робот движется