0

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

coder
  • 52
  • 1
  • 6
  • 1
    you have no separator between the numbers so it sends 1234567891011.... parseInt waits a second for the next digit – Juraj Nov 22 '21 at 10:34
  • @Juraj so what should i do for this please help. – coder Nov 22 '21 at 11:09
  • add some separator.. parseInt stops on any not digit character – Juraj Nov 22 '21 at 11:38
  • @Juraj it's not working like which is my expectation. it work like if we slide the slider then led has turn on with pwm value. after 1 second led will automatically off. – coder Nov 22 '21 at 12:22
  • 1
    what exactly have you tried? from your code it looks like Juraj's right. you only send numbers but no separators so Arduino will timeout after 1 second. what is that arduino->readAll() good for? shouldn't you wait for something to read befor you read? please improve your post. add more information. what exactly is happening? – Piglet Nov 22 '21 at 12:33
  • @Piglet first of all i try to control the brightness of the led with QSlider qt ui. then i was facing issue which is led not turn on with the slider value. i think it has taken value like 123456.. which is juraj said in 1st comment. then i tried solution which is juraj told in the comment number two which is add some separator.... . 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. – coder Nov 22 '21 at 13:11
  • 1
    please stop adding information through comments. update your question where you can properly format your text. also if you have fixed the 123456 issue this question is obsolete. post a new question or change this one until you receive a satisfactory answer – Piglet Nov 22 '21 at 13:46
  • @Piglet thanks for info. i have changed the question. now please help. – coder Nov 22 '21 at 13:59
  • 1
    have your Arduino print all received characters for debugging. you probably receive an extra non-digit character so parseInt times out and returns 0 after 1 second.. you failed to update your code. it is still sending just the numbers. – Piglet Nov 22 '21 at 14:32

1 Answers1

2

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)

coder
  • 52
  • 1
  • 6