adjust brightness of led using QSlider and PWM in arduino.
-
wrote on 22 Nov 2021, 06:22 last edited by dev_liya
I am try to control the led brightness using qslider with pwm value 0 to 255. but when i slide the slider the led not go through the slider value (it's not happening like "on the fly"). it's take i think rendom value. but when i slide the slider with the delay of 1 1 second and increment the value with 1, 2, 3... up to 255 then it will go ok. i cannot understand what was the issue.
here is my qt code.mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <string> #include <QDebug> #include <QSerialPort> #include <QSerialPortInfo> #include <QMessageBox> #include <QRegularExpression> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); arduino = new QSerialPort(this); bool is_arduino_avilable = false; QString portNmae; databuf = ""; foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { if(info.hasProductIdentifier() && info.hasVendorIdentifier()) { if(info.productIdentifier() == arduino_productId && info.vendorIdentifier() == arduino_vendorId) { is_arduino_avilable = true; portNmae = info.portName(); } } } if(is_arduino_avilable) { qDebug() << "Arduino port is found " + portNmae; arduino->setPortName(portNmae); arduino->open(QSerialPort::ReadWrite); arduino->setBaudRate(QSerialPort::Baud9600); arduino->setDataBits(QSerialPort::Data8); arduino->setFlowControl(QSerialPort::NoFlowControl); arduino->setParity(QSerialPort::NoParity); arduino->setStopBits(QSerialPort::OneStop); QObject::connect(arduino,SIGNAL(readyRead()),this,SLOT(readSerialdata())); } else { qDebug() << "Couldn't find the correct port for the arduino.\n"; QMessageBox::information(this,"Serial Port Error","Couldn't open serial port to arduino"); } } MainWindow::~MainWindow() { if(arduino->isOpen()) { arduino->close(); } delete ui; } void MainWindow::on_pluse_slider_valueChanged(int value) { ui->slider_value->setText(QString("<span style=\"font-size:14pt; font-weight:600;\">%1</span>").arg(value)); qDebug()<<value; arduino->write(QString("%1").arg(value).toStdString().c_str()); QByteArray data_val; data_val = arduino->readAll(); // qDebug()<<QString::fromStdString(data_val.toStdString()); // if(arduino->isWritable()) // { // qDebug()<<value; // arduino->write(QString("%1").arg(value).toStdString().c_str()); // } // else { // qDebug()<<"couldn't write to arduino"; // } }
and here is my arduino code.
pwm.ino
const int LED = 11; int rsv_data; void setup() { Serial.begin(9600); pinMode(LED,OUTPUT); } void loop() { if(Serial.available()) { rsv_data = Serial.parseInt(); analogWrite(LED,rsv_data); Serial.print(rsv_data); delay(100); } }
-
I am try to control the led brightness using qslider with pwm value 0 to 255. but when i slide the slider the led not go through the slider value (it's not happening like "on the fly"). it's take i think rendom value. but when i slide the slider with the delay of 1 1 second and increment the value with 1, 2, 3... up to 255 then it will go ok. i cannot understand what was the issue.
here is my qt code.mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <string> #include <QDebug> #include <QSerialPort> #include <QSerialPortInfo> #include <QMessageBox> #include <QRegularExpression> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); arduino = new QSerialPort(this); bool is_arduino_avilable = false; QString portNmae; databuf = ""; foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) { if(info.hasProductIdentifier() && info.hasVendorIdentifier()) { if(info.productIdentifier() == arduino_productId && info.vendorIdentifier() == arduino_vendorId) { is_arduino_avilable = true; portNmae = info.portName(); } } } if(is_arduino_avilable) { qDebug() << "Arduino port is found " + portNmae; arduino->setPortName(portNmae); arduino->open(QSerialPort::ReadWrite); arduino->setBaudRate(QSerialPort::Baud9600); arduino->setDataBits(QSerialPort::Data8); arduino->setFlowControl(QSerialPort::NoFlowControl); arduino->setParity(QSerialPort::NoParity); arduino->setStopBits(QSerialPort::OneStop); QObject::connect(arduino,SIGNAL(readyRead()),this,SLOT(readSerialdata())); } else { qDebug() << "Couldn't find the correct port for the arduino.\n"; QMessageBox::information(this,"Serial Port Error","Couldn't open serial port to arduino"); } } MainWindow::~MainWindow() { if(arduino->isOpen()) { arduino->close(); } delete ui; } void MainWindow::on_pluse_slider_valueChanged(int value) { ui->slider_value->setText(QString("<span style=\"font-size:14pt; font-weight:600;\">%1</span>").arg(value)); qDebug()<<value; arduino->write(QString("%1").arg(value).toStdString().c_str()); QByteArray data_val; data_val = arduino->readAll(); // qDebug()<<QString::fromStdString(data_val.toStdString()); // if(arduino->isWritable()) // { // qDebug()<<value; // arduino->write(QString("%1").arg(value).toStdString().c_str()); // } // else { // qDebug()<<"couldn't write to arduino"; // } }
and here is my arduino code.
pwm.ino
const int LED = 11; int rsv_data; void setup() { Serial.begin(9600); pinMode(LED,OUTPUT); } void loop() { if(Serial.available()) { rsv_data = Serial.parseInt(); analogWrite(LED,rsv_data); Serial.print(rsv_data); delay(100); } }
please reply and help ASAP. thank you in dvance.
I would suggest the following, set tracking to false on your slider
setTracking(false)
that way value changed is only emitted once, when the user releases the slider handle. -
I would suggest the following, set tracking to false on your slider
setTracking(false)
that way value changed is only emitted once, when the user releases the slider handle.wrote on 22 Nov 2021, 07:34 last edited by@J-Hilk said in adjust brightness of led using QSlider and PWM in arduino.:
setTracking(false)
@J-Hilk
Thank you for your reply. but i need on the fly means when i slide at a time adjust the brightness of led according to value. -
@J-Hilk said in adjust brightness of led using QSlider and PWM in arduino.:
setTracking(false)
@J-Hilk
Thank you for your reply. but i need on the fly means when i slide at a time adjust the brightness of led according to value.@dev_liya
well you're currently overwhelming your connectionthe value changed signal will be triggered multiple times per second, the serial port will probably flush only once with all data accumulated.
on the Arduino side you only read the serial port every 100 ms and expect exactly 1 value.
you'll have to slowdown or speed up somewhere, or update your arduino code.
Make it parse the whole data and extract the value correctly. -
@dev_liya
well you're currently overwhelming your connectionthe value changed signal will be triggered multiple times per second, the serial port will probably flush only once with all data accumulated.
on the Arduino side you only read the serial port every 100 ms and expect exactly 1 value.
you'll have to slowdown or speed up somewhere, or update your arduino code.
Make it parse the whole data and extract the value correctly.wrote on 22 Nov 2021, 07:48 last edited by@J-Hilk
Thanks for your reply. can you please help me for this how it is possible? which you said in this reply what should i need to changed in arduino code ? -
@J-Hilk
Thanks for your reply. can you please help me for this how it is possible? which you said in this reply what should i need to changed in arduino code ?@dev_liya I haven't yet coded for Arduino, but from a quick look in the documentation
I would suggest reading the while buffer via:
stream.readBytes(buffer, length)than parse that buffer for the last integer.
Btw, you actually have to make sure that sizeOf(long) match between the Arduino and your PC, that's not guaranteed. long may (typically ) be 4 or 8 bytes.
-
wrote on 23 Nov 2021, 06:16 last edited by
i have solved this issue using below solution:
i have added the comma( , ) as separator befor the value in this below line
arduino>write(QString(",%1").arg(val).toStdString().c_str());
this line is in this function
void MainWindow::on_pluse_slider_valueChanged(int value)
1/7