Arduino Rgb Controller From Vb.net
by Miky94x in Circuits > Arduino
6055 Views, 22 Favorites, 0 Comments
Arduino Rgb Controller From Vb.net
Hi, this is my first instractables, today i'll show you how to make an rgb controller with an arduino UNO R3, controlled by an application made in vb.net
With this project you can simply control your led strip and make what colour you like
excuse me for my english, i hope you like my instructable
Parts and Material
These are the materials:
- 1 arduino uno
- 1 usb for arduino (programming or connection)
- 1 bluetooth module (optional but make this project wireless)
- Arduino IDE
- Visual studio
Arduino Code
The arduino code is very simple but powerfull
code:
int ledrPin = 9; int ledgPin = 10; int ledbPin = 11; void setup() { Serial.begin(9600); pinMode(ledrPin, OUTPUT); pinMode(ledgPin, OUTPUT); pinMode(ledbPin, OUTPUT); } void loop() { if (Serial.available() > 0) { int r = Serial.parseInt(); int g = Serial.parseInt(); int b = Serial.parseInt(); analogWrite(ledrPin, r); analogWrite(ledgPin, g); analogWrite(ledbPin, b); } }<br>
Copy and paste this code into the arduino ide
now you have to upload it to your board
Vb.net Code
You have to open visual studio or visual basic
- Create a new project
- name it like "rgb controller"
Once the form is created drag and drop this control to the form:
- 1 label and set the text to: "Select the COM port"
- 1 combobox
- 1 button and name it: "refresh"
- 3 trackbar (set minimum value to 0 and maximum value to 255)
- 1 label at the bottom and set the text to: "Rgb: 255,255,255"
- 1 picturebox (set the backcolor to white, to see what colour we have select)
- 1 timer
Now we have to write the code for the rgb controller
before all write:
Imports System.IO.Ports
under Public Class Form1 write:
Dim connected As Boolean = False Dim r As Integer Dim port As New SerialPort Dim r As Integer = 255<br>Dim g As Integer = 255 Dim b As Integer = 255 Dim rgb As String = TrackBar1.Maximum - TrackBar1.Value + TrackBar1.Minimum & "," & TrackBar2.Maximum - TrackBar2.Value + TrackBar2.Minimum & "," & TrackBar3.Maximum - TrackBar3.Value + TrackBar3.Minimum & "M"
create a new function called COMport to search all the serial port avaible and add to the combobox
Function COMport()<br> ComboBox1.Items.Clear() ComboBox1.Items.AddRange(SerialPort.GetPortNames) End Function
in the form1.load call COMport()
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load<br> COMport() End Sub
Double click on the refresh button and add the same call COMport()
Double click on the combobox and add this code to set the port option
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged connected = True port.Close() port.PortName = ComboBox1.SelectedItem.ToString port.BaudRate = 9600 port.DataBits = 8 port.Parity = Parity.None port.StopBits = StopBits.One port.Handshake = Handshake.None port.Encoding = System.Text.Encoding.Default End Sub
Now set the timer to enabled with an interval of 1 and add this code to the timer tick event
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick rgb = TrackBar1.Maximum - TrackBar1.Value + TrackBar1.Minimum & "," & TrackBar2.Maximum - TrackBar2.Value + TrackBar2.Minimum & "," & TrackBar3.Maximum - TrackBar3.Value + TrackBar3.Minimum & "M" Label2.Text = "Rgb: " & r & "," & g & "," & b PictureBox1.BackColor = Color.FromArgb(r, g, b) If connected = True Then If port.IsOpen Then Try port.Write(rgb) port.Close() Catch ex As Exception End Try Else Try port.Open() port.Write(rgb) port.Close() Catch ex As Exception End Try End If End If End Sub<br>
Now there is the part of the code for change the code through the three trackbar
Double click on the first trackbar and here is the code
r = TrackBar1.Value
The second trackbar code
g = TrackBar2.Value
the third trackbar code
b = TrackBar3.Value
The Connection
If you connect only 1 led, it only need arduino, wire, resistor and the led, but if you want to connect an rgb strip, you have to use an amplifier for strip like this
the connection is:
- pin 9 to resistor to - red
- pin 10 to resistor to - green
- pin 11 to resistor to - blue
- vcc to + led
It's time to compile and test the app
i've only tested the connection through usb, before you choose the serial port, you have to connect arduino to usb and connect the program through the arduino COM
Conclusion
This is the program i use with my custom animated theme and more option like effect, on, off...
if you have a basic programming skill, you can make this and more function...
i hope you enjoy