qtarduinobrightnesspwmqslider

adjust brightness of led using QSlider and PWM in arduino


i try to control the brightness of the led with QSlider qt ui.i was facing issue which is led not turn on with the slider value. i think it has taken value like 123456... then i tried solution which is add some separator.. parseInt() stops on any not digit character. now i am facing the issue that led is turning on properly with pwm value which is passed from slider but it is turning off automatically after one second.

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());
}

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);
  }
}

Thank you in advance.

this is ui image

this is debug output image


Solution

  • 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)