Hello i was looking for away to control a RGB led from my pc and i found a instructables Site. So i followed all directions to the T. But so i setup the com port in viual basic to match the arduino port of my uno board. However nothing happens when i try to control the leds. But the strange thing is when i visual basic application i made i get a Slight led flick off and on saying something is there it is trying to do but i need help. Can someone please help me. Sketching both below of the arduino sketch and the visual basic code as well.
Arduino sketch
int ledR= 8;
int ledG= 9;
int ledB= 10;
void setup()
{
pinMode(ledR,OUTPUT);
pinMode(ledG,OUTPUT);
pinMode(ledB,OUTPUT);
Serial.begin(9600);
}
void loop()
{
int val, r, g, b;
if (Serial.available())
{
delay(100);
while (Serial.available ()>9)
{
val=Serial.read();
int number[8] = {0,0,0,0,0,0,0,0};
for (int i = 0; i < 9; i = i++)
{
number[i]= Serial.read();
}
int num0 = number[0]-'0';
int num1 = number[1]-'0';
int num2 = number[2]-'0';
r= num0*100+ num1*10 + num2;
int num3= number[3]-'0';
int num4 = number[4]-'0';
int num5 = number[5]-'0';
g= num3*100+ num4*10 + num5;
int num6 = number[6]-'0';
int num7= number[7]-'0';
int num8= number[8]-'0';
b= num6*100+ num7*10 + num8;
color( r, g, b);
}
Serial.flush();
}
}
void color(int red, int green, int blue)
{
analogWrite(ledR, red);
analogWrite(ledG, green);
analogWrite(ledB, blue);
}
Visual basic sketch
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.PortName = "COM9"
SerialPort1.Open()
TextBox_R.Text = 0
TextBox_G.Text = 0
TextBox_B.Text = 0
TextBox_RGB.Text = 0 '( 1000000000 + R*1000000 + G*1000 + B)
TextBox_RGB.Hide()
End Sub
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar_R.Scroll
TextBox_R.Text = TrackBar_R.Value
Panel1.BackColor = System.Drawing.Color.FromArgb(TextBox_R.Text, TextBox_G.Text, TextBox_B.Text)
End Sub
Private Sub TrackBar2_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar_G.Scroll
TextBox_G.Text = TrackBar_G.Value
Panel1.BackColor = System.Drawing.Color.FromArgb(TextBox_R.Text, TextBox_G.Text, TextBox_B.Text)
End Sub
Private Sub TrackBar3_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar_B.Scroll
TextBox_B.Text = TrackBar_B.Value
Panel1.BackColor = System.Drawing.Color.FromArgb(TextBox_R.Text, TextBox_G.Text, TextBox_B.Text)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EnviarDatos.Click
TextBox_RGB.Text = 1000000000 + TextBox_R.Text * 1000000 + TextBox_G.Text * 1000 + TextBox_B.Text
SerialPort1.Write(TextBox_RGB.Text)
'System.Threading.Thread.Sleep(200)
'TextBox5.Text = SerialPort1.ReadExisting()
End Sub
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
End Sub
Private ClD As New ColorDialog
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If ClD.ShowDialog = Windows.Forms.DialogResult.OK Then
Panel1.BackColor = ClD.Color
Dim tmpBrush As New SolidBrush(Panel1.BackColor)
With tmpBrush.Color
TextBox_B.Text = .B
TextBox_G.Text = .G
TextBox_R.Text = .R
TrackBar_R.Value = TextBox_R.Text
TrackBar_G.Value = TextBox_G.Text
TrackBar_B.Value = TextBox_B.Text
End With
'Without the With statement, you need to put in:
'for example.
End If
End Sub
Private Sub Delet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delet.Click
Panel1.BackColor = Color.Black
TextBox_B.Text = 0
TextBox_G.Text = 0
TextBox_R.Text = 0
TrackBar_R.Value = 0
TrackBar_G.Value = 0
TrackBar_B.Value = 0
SerialPort1.Write(1000000000)
End Sub
End Class