Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. adjust brightness of led using QSlider and PWM in arduino.
Forum Updated to NodeBB v4.3 + New Features

adjust brightness of led using QSlider and PWM in arduino.

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 709 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dev_liya
    wrote on 22 Nov 2021, 06:22 last edited by dev_liya
    #1

    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.2.PNG 1.PNG

    J 1 Reply Last reply 22 Nov 2021, 07:02
    0
    • D dev_liya
      22 Nov 2021, 06:22

      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.2.PNG 1.PNG

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 22 Nov 2021, 07:02 last edited by
      #2

      @dev_liya

      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.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      D 1 Reply Last reply 22 Nov 2021, 07:34
      1
      • J J.Hilk
        22 Nov 2021, 07:02

        @dev_liya

        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.

        D Offline
        D Offline
        dev_liya
        wrote on 22 Nov 2021, 07:34 last edited by
        #3

        @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 1 Reply Last reply 22 Nov 2021, 07:40
        0
        • D dev_liya
          22 Nov 2021, 07:34

          @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 Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 22 Nov 2021, 07:40 last edited by
          #4

          @dev_liya
          well you're currently overwhelming your connection

          the 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.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          D 1 Reply Last reply 22 Nov 2021, 07:48
          2
          • J J.Hilk
            22 Nov 2021, 07:40

            @dev_liya
            well you're currently overwhelming your connection

            the 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.

            D Offline
            D Offline
            dev_liya
            wrote on 22 Nov 2021, 07:48 last edited by
            #5

            @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 1 Reply Last reply 22 Nov 2021, 07:58
            0
            • D dev_liya
              22 Nov 2021, 07:48

              @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 Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 22 Nov 2021, 07:58 last edited by
              #6

              @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.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              2
              • D Offline
                D Offline
                dev_liya
                wrote on 23 Nov 2021, 06:16 last edited by
                #7

                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 Reply Last reply
                1

                1/7

                22 Nov 2021, 06:22

                • Login

                • Login or register to search.
                1 out of 7
                • First post
                  1/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved