QT Framework Display Motor Speed Simulator by Proteus

QT Framework Display Motor Speed Simulator by Proteus

Project simulator control and display encoder motor.

  • 225 views
  • 0 comments
  • 5 respects

Components and supplies

Apps and online services

About this project

Story

I have been learning and getting things running on my QT framework to simulation encoder motor. Think using Raspberry Pi will receive signal from Arduino is better than only Arduino. It will upgrade some application like : media, weather or maybe GPS. Well, this is my first step on making some IoT project with QT framework + Arduino or Raspberry Pi.

Watch the video!

Project has 4 displays:

+ Driver: Display speed motor with Simple-time 1s and control speed, and Open/Close Door , Windscreen wiper by Servo Motor

+ Light: Control brightness of light by pulse PWM (from 0 to 255, step size 30)

+Slidebar: Display on left-hand, show real-time

+ Music : playing music

Notes : This project just simulation by Serial port so it only has one-way transmitting or communication.


Code

Code_arduinoC/C++
int pinMotor = 8 ;
int pinEncoder= 2;
int pinMotor2 = 9 ;
int pinEncoder2 = 3;
int forth = 11;
int behind = 10 ;
int center = 12;
int coner =13;
int state ;
int forward = 0 ;
volatile unsigned int counter = 0 ;
volatile unsigned int counter2 = 0 ;
unsigned int motorSpeedRPM = 0 ;
unsigned int motorSpeedRPM2 = 0 ;
unsigned long interruptTime ;
static unsigned long lastInterruptTime = 0;
byte brightness = 0 ;
void setup() {
pinMode(pinMotor2,OUTPUT);
pinMode(pinEncoder2,INPUT);
pinMode (pinMotor,OUTPUT);
pinMode (pinEncoder,INPUT);
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(pinEncoder),encoderCounter,RISING);
attachInterrupt(digitalPinToInterrupt(pinEncoder2),encoderCounter2 ,RISING);
}

void loop() {
  interruptTime = millis();
 while(Serial.available()>0)
  {
    state = Serial.read();
   
    if (state == '1' )
    {
         forward = 1 ; 
    }
    if (state == '2' )
    {
          forward = 0 ; 
    }
   if(char(state) >= '0' && char(state) <='9')
    {
    brightness = brightness*10 + char(state) - '0';
    analogWrite(forth,brightness);
    analogWrite(behind,brightness);
    analogWrite(center,brightness);
    analogWrite(coner,brightness);
    delay(5);
    }
    }
   brightness = 0 ;

// dk run
 if ( forward == 1 ) 
 {
 analogWrite(pinMotor,250);
 analogWrite(pinMotor2,250);
 }
 if(forward == 0) {
 analogWrite(pinMotor,0);
 analogWrite(pinMotor2,0);
}
if(( interruptTime - lastInterruptTime) == 1000)
{  
 motorSpeedRPM= (counter*60)/48;
 counter = 0 ;
 lastInterruptTime = interruptTime ;
 motorSpeedRPM= (counter2*60)/48;
 counter2 = 0 ;
 lastInterruptTime = interruptTime ;
  Serial.print(motorSpeedRPM);

} 
}
 
void encoderCounter()
{
  
    counter+=1;
  }
void encoderCounter2()
{
  
    counter2+=1;
  }
Header_ QT_codeC/C++
#ifndef ConnectCOM
#define ConnectCOM
#define SPEED_H
#include <QtSerialPort>
#include <QObject>
#include <QWidget>

class MainWindow : public QObject
{
    Q_OBJECT
    Q_PROPERTY(int speed READ speed NOTIFY speedChanged)
    Q_PROPERTY(int light_F READ light_F WRITE setLight_F )
    Q_PROPERTY(int light_B READ light_B WRITE setLight_B )
    Q_PROPERTY(int light_Ce READ light_Ce WRITE setLight_Ce )
    Q_PROPERTY(int light_Co READ light_Co WRITE setLight_Co )
public:
    explicit MainWindow(QObject *parent = nullptr);
    void setLight_F(int &a);
    int light_F();
    void setLight_B(int &b);
    int light_B();
    void setLight_Ce(int &c);
    int light_Ce();
    void setLight_Co(int &d);
    int light_Co();

private:
    int speed() const;
    QSerialPort *arduino;
     QTimer *timer = new QTimer(this);
        int m_speedData;
        int m_lightData_F;
         int m_lightData_B;
          int m_lightData_Ce;
           int m_lightData_Co;

signals:
 void  speedChanged();

public slots :
   void turnOn();
   void turnOff();
   void updateSpeed();
  void onForth();
   void onBehind();
   void onCenter();
  void onConer();



 };




#endif // GiaoTiepCongCOM.h
Connect with Serial Port , call funtion C++ from QML _QT_codeC/C++
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <GiaoTiepCongCom.h> // libary of connect
#include <QtSerialPort>
#include <QtDebug>
#include <QMessageLogContext>
#include <QWidget>
#include <QtEndian>
MainWindow:: MainWindow(QObject *parent) : QObject(parent)
{
       arduino  = new QSerialPort ;
       arduino ->setPortName("Com1");
       arduino ->setBaudRate(QSerialPort::Baud9600);
       arduino ->setParity(QSerialPort::NoParity);
       arduino ->setDataBits(QSerialPort::Data8);
       arduino ->setStopBits(QSerialPort::OneStop);
       arduino->setFlowControl(QSerialPort::NoFlowControl);
       if (arduino->open(QIODevice::ReadWrite)){
            qDebug()<<"Opened successfully";
                } else {
           qDebug()<<"Error:";
           qDebug()<<arduino->error();
       }
     connect(arduino,SIGNAL(readyRead),this,SLOT(serialreceived));
     connect(timer, SIGNAL(timeout()), this, SLOT(updateSpeed()));
        timer->start(1000);
        m_speedData = 0;
}
void MainWindow::setLight_F (int &a)
{
    if (a != m_lightData_F) {
        m_lightData_F = a;
      qDebug () << "Forth" << m_lightData_F;
        QByteArray light;
        light.setNum(m_lightData_F);
        arduino ->write(light);
        qDebug() << light ;

    }
}
int MainWindow::light_F() {
    return  m_lightData_F;
}
void MainWindow::setLight_B (int &b)
{
    if (b != m_lightData_B) {
        m_lightData_B = b;
      qDebug () << "Behind" << m_lightData_B;
        QByteArray light;
        light.setNum(m_lightData_B);
        arduino ->write(light);
    }
}
int MainWindow::light_B() {
    return  m_lightData_B;
}
void MainWindow::setLight_Ce (int &c)
{
    if (c != m_lightData_Ce) {
        m_lightData_Ce = c;
      qDebug () << "Center" << m_lightData_Ce;
        QByteArray light;
        light.setNum(m_lightData_Ce);
        arduino ->write(light);
          }
}
int MainWindow::light_Ce() {
    return  m_lightData_Ce;
}void MainWindow::setLight_Co (int &d)
{
    if (d != m_lightData_Co) {
        m_lightData_Co = d;
      qDebug () << "Coner" << m_lightData_Co;
        QByteArray light;
        light.setNum(m_lightData_Co);
        arduino ->write(light);
       }
}
int MainWindow::light_Co() {
    return  m_lightData_Co;
}
void MainWindow::turnOn()
{

    if (arduino->isWritable())
    {
        arduino->write("1");

    }
    qDebug() << "Into On" ;
}
void MainWindow::turnOff()
{
    if (arduino->isWritable())
      {
          arduino->write("2");
      }
    qDebug() << "Into off" ;

}
void MainWindow::updateSpeed() {
    QByteArray speed ;
     bool ok;
     speed = arduino->readAll();
      qDebug() << "toc do " << speed;
     m_speedData = speed.toInt(&ok);
     emit speedChanged();
}

void MainWindow::onForth()
{
    if (arduino->isWritable())
      {
          arduino->write("3");
      }
    qDebug() << "forth" ;
}

void MainWindow::onBehind()
{
    if (arduino->isWritable())
      {
          arduino->write("4");
      }
    qDebug() << "behind" ;
}

void MainWindow::onCenter()
{
    if (arduino->isWritable())
      {
          arduino->write("5");
      }
    qDebug() << "center" ;
}

void MainWindow::onConer()
{
    if (arduino->isWritable())
      {
          arduino->write("6");
      }
    qDebug() << "coner" ;
}


int MainWindow:: speed() const
{
    return m_speedData;
}
main.qml_QT_codeC/C++
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.5
import QtQuick.Layouts 1.3
import QtQuick.Controls.Material 2.2
import GiaoTiepCongCOM 1.0

ApplicationWindow
{
    id: applicationWindow
    visible: true
    width: 1280
    height:800
    title: qsTr("Car Display Controller")
    Material.accent: Material.DeepOrange
    Material.theme: Material.Dark
    StackLayout{
        id : swipeView
        anchors.right: parent.right
        anchors.rightMargin: 0
        anchors.left: slideBar.right
        anchors.leftMargin: 0
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 0
        anchors.top: parent.top
        anchors.topMargin: 0
        currentIndex: tabBar.currentIndex
        GiaoTiepCongCOM{
            id: speedData
            light_F:  lightN.forth
            light_B:  lightN.behind
            light_Ce: lightN.center
            light_Co: lightN.coner
        }
        SpeedGauge {
            id: speedGauge
            value_speed: speedData.speed
            value_rpm:   speedData.speed * 0.025
            Button {
                           id: bt_Start
                           x: 189
                           y: 610
                           text: qsTr("START")
                            onClicked: speedData.turnOn()
                       }

                       Button {
                           id: bt_Stop
                           x: 717
                           y: 610
                           text: qsTr("STOP")
                           onClicked: speedData.turnOff()
           }

        }

        Light
        {
            id: lightN
        }
    }

    TabBar {
        id : tabBar
        y: 752
        height: 48
        anchors.leftMargin: 0
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        anchors.left: slideBar.right


        TabButton
        {
            text: qsTr("Driver")
        }
        TabButton
        {
            text: qsTr("Light")
        }

        TabButton {
            text : qsTr("Media")

        }
    }

    SlideBar {
        id: slideBar
        x: 0
        y: 0
        width: 312
        height: 800

    }




}
main.cpp_QT_codeC/C++
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtSerialPort>
#include <GiaoTiepCongCom.h>
#include <QMessageLogContext>
#include<QQmlEngine>
#include<QQmlContext>
#include <qdialog.h>
#include <QDir>
#include <QStandardPaths>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QWindow>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    qmlRegisterType<MainWindow>("GiaoTiepCongCOM", 1, 0, "GiaoTiepCongCOM");
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);


    return app.exec();
      }

Schematics

Commpim
Screenshot (5) kiln4yjncz
screenshot_(8)_NT3Gb0kPC3.png
Screenshot (8) nt3gb0kpc3

Comments

Similar projects you might like

Control the Speed of Brushless DC Motor Using Bluetooth

Project tutorial by mohitmehlawat

  • 7,725 views
  • 1 comment
  • 17 respects

High Speed Arduino RC car

by Muhammad Aqib

  • 6,120 views
  • 4 comments
  • 17 respects

LED Display with Arduino ADC and PWM

Project tutorial by Ian Etheridge

  • 3,211 views
  • 0 comments
  • 9 respects

Control Speed and Direction of Motor

Project in progress by ahmed soliman

  • 17,828 views
  • 5 comments
  • 25 respects

Check a Stepper Motor From the DVD-ROM

by Ingo Lohs

  • 13,674 views
  • 2 comments
  • 43 respects
Add projectSign up / Login