control Arduino with Qt Buttons

Hey,

I try to control a GPIO with Qt.

I use the example from GitHub:

https://github.com/cedoduarte/SerialPort

In this Template I can only control one GPIO with Qt but I want to control 3 …the problem is that i don’t know how.

Arduino Code:

#define PIN_LED 9

void setup()
{
  pinMode(PIN_LED, OUTPUT);
  analogWrite(PIN_LED, 0);
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available()){
    char led_specifier = Serial.read();
    int led_brightness = Serial.parseInt();
    analogWrite(PIN_LED, led_brightness);
  }
}

Qt:

#include "Widget.h"
#include "ui_Widget.h"
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QMessageBox>
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    arduino_is_available = false;
    arduino_port_name = "";
    arduino = new QSerialPort;


    qDebug() << "Number of available ports: " << QSerialPortInfo::availablePorts().length();
    foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
        qDebug() << "Has vendor ID: " << serialPortInfo.hasVendorIdentifier();
        if(serialPortInfo.hasVendorIdentifier()){
            qDebug() << "Vendor ID: " << serialPortInfo.vendorIdentifier();
        }
        qDebug() << "Has Product ID: " << serialPortInfo.hasProductIdentifier();
        if(serialPortInfo.hasProductIdentifier()){
            qDebug() << "Product ID: " << serialPortInfo.productIdentifier();
        }
    }


    foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
        if(serialPortInfo.hasVendorIdentifier() && serialPortInfo.hasProductIdentifier()){
            if(serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id){
                if(serialPortInfo.productIdentifier() == arduino_uno_product_id){
                    arduino_port_name = serialPortInfo.portName();
                    qDebug() << "HALLLLLOO" + arduino_port_name;
                    arduino_is_available = true;
                }
            }
        }
    }

    if(arduino_is_available){
        // open and configure the serialport
        arduino->setPortName(arduino_port_name);
        arduino->open(QSerialPort::WriteOnly);
        arduino->setBaudRate(QSerialPort::Baud9600);
        arduino->setDataBits(QSerialPort::Data8);
        arduino->setParity(QSerialPort::NoParity);
        arduino->setStopBits(QSerialPort::OneStop);
        arduino->setFlowControl(QSerialPort::NoFlowControl);
    }else{
        // give error message if not available
        QMessageBox::warning(this, "Port error", "Couldn't find the Arduino!");
    }
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_encenderPushButton_clicked()
{
    if(arduino->isWritable()){
        arduino->write("1");
    }else{
        qDebug() << "Couldn't write to serial!";
    }
}

void Widget::on_apagarPushButton_clicked()
{
    if(arduino->isWritable()){
        arduino->write("0");
    }else{
        qDebug() << "Couldn't write to serial!";
    }
}

Similar here

The code you posted is a bit confusing tome.

The Arduino Code looks like it is expecting to read a char, then an int value. That char value even when read is not used.

The QT code, depending on what button is pressed, will send a 0 or 1.

There is a mismatch in what your QT code is doing ver what the Arduino is expecting. Please explain.

In order to control several pins, you will need to not only send brightness, but you will also need to send the pin you wish to control.

MOC code Not intended to work, just show you need to have a contract between the receiver and sender. both sides must agree to a protocol, and follow it.

//ARDUINO

void loop()
{
  if (Serial.available()){
    int led_pin = Serial.parseInt();
    int led_brightness = Serial.parseInt();
    analogWrite(led_pin, led_brightness);
  }
}

Now, without understanding what your buttons are to do, I am just going to provide you some moc code for QT

void Widget::on_encenderPushButton_clicked()
{
    if(arduino->isWritable()){
        arduino->write("1"); // Pin Number
        arduino->write("25"); // brightness

    }else{
        qDebug() << "Couldn't write to serial!";
    }
}

Hey,

i want to set a GPIO to HIGH/LOW with one Button.
For example:

  • Button_D8_ON --> GPIO D8 HIGH
  • Button_D8_OFF --> GPIO D8 LOW
  • Button_D7_ON --> GPIO D7 HIGH
    ...
    ...

that's what I want to do. With the code from Github I can only control one GPIO

Lock post bycause crosposting.
Bye Uwe