Unsolved How to control LED luminosity by entering number in Qt to Arduino
-
Hi everyone, I am a new learner of Qt. Recently, I am trying to develop a GUI to control the luminosity of LED thru Arduino by typing number (0~255) in Qt GUI.
Here is my Arduino code:
const int ledPin = 9; int ledlum; void setup() { pinMode(ledPin, OUTPUT); analogWrite(ledPin, 0); Serial.begin(9600); } void loop() { if (Serial.available()){ ledlum = Serial.parseInt(); write_led(ledlum); } } void write_led(int lum){ analogWrite(ledPin, 0 + lum); }
My question is, how can I create a textbox or any box for number input to enter the number to control luminosity? I already know how to connect between Arduino and Qt. The part I don't know is, what's the code for textbox to send number from Qt to Arduino?
Thank you guys in advance:) -
@Macive-Xiong You can use QSpinBox to enter a number. Use QSpinBox::value() to get its value.
See http://doc.qt.io/qt-5/qspinbox.html -
@jsulm thank you so much for your help:)
-
@jsulm Do you know any example for sending number to Arduino from Qt? I am not really sure how to write the code in Qt to send signal to Arduino thru SpinBox.
Thank you so much:)
-
@Macive-Xiong No, I don't. How do you communicate with Arduino? Via serial port? If so then take a look at http://doc.qt.io/qt-5/qtserialport-index.html
In your first post in this thread you actually already have code to communicate with Arduino. I'm not sure what the problem is: get the value from QSpinBox calling its value() method. You can even use http://doc.qt.io/qt-5/qspinbox.html#valueChanged signal to get notified about a new value and then send it to Arduino. -
@jsulm Thanks again for these info. I appreciate it:)