Maker Pro
Maker Pro

[Project Help]Controlling LEDs with Visual Basic

Hey guys, we have an interfacing project using Visual Basic.
Basically, we should have a circuit with multiple LEDs(we're planning to use 14 pcs.), And so, by using Visual Basic,
we should be able to turn on or off each LED just by pressing a button on the VB program.

Problem is, we can't use an Arduino board, and instead we need to use a serial port as output.

Correct me if I'm wrong, I would need to purchase a USB to RS232 Cable right?
Other than that, what other components do we need to use?

Furthermore, when I have all the needed components, in what sequence should they be in(from USB port to the LED itself)?

Any help would be greatly appreciated, Thanks!
(If I'm posting in the wrong subforum, I apologize)
 

Harald Kapp

Moderator
Moderator
Using a serial port (with or without USB to serial converter, this depends on the PC hardware you have) is not sufficient to control external LEDs. Teh serial port can only send data (or commands) to an external circuit. The external circuit needs to interpret the data and act accordingly. Any microcontroler with serial interface and enough I/OL to control the required number of LEDs can be used. An arduino is not a bad choice.

The VB program would send an on/offf command plus the number of the LED to be turned on or off to the external controller. The external controller will decode the command and turn on or off the respective LED.

A USB-to-serial cable is required only if your PC doesn't have a serial interface. The USB-to-serial cable will require installation of a driver which will create a virtual COM port to be accessed by the VB program.
 
Further to what Harald said about an Arduino or similar external controller, you're better off going with a USB to TTL converter, rather than USB to RS232. RS232 has +Ve and -Ve levels, whereas for a micro-controller you ideally want 0V to 5V TTL levels to interface easily with the micro.

USB to TTL converters are pretty cheap, you want something along these lines, (AU$1.04 delivered):-
http://www.ebay.com.au/itm/CH340G-C...2102-PL2303-/291517200596?hash=item43dfc778d4

You'll need to install a CP2102 driver for the CP2102-based boards, but they're easily found (free) online here:-
https://www.silabs.com/products/mcu/Pages/USBtoUARTBridgeVCPDrivers.aspx

And rather than the more expensive original Arduino, a clone is worth considering. You can tell the difference, (besides from price), because only the original Arduino UNO boards have the name "Arduino" on them. The others simply say "UNO".
One like this, (AU$8.00 delivered):-
http://www.ebay.com.au/itm/201406721290

And while I'm at it, the (free) Arduino IDE, needed to program the Arduino, is available here:-
https://www.arduino.cc/en/Main/Software
 
You could use a USB to !2C convertor for $15. There are heaps of little led controllers that will controll 8 or 16 leds with one chip and only cost a few bucks.
 
Now that we're allowed to use an Arduino, I was able to control the LED at PIN13 on the Arduino using this code.
http://www.instructables.com/id/Using-Visual-Basic-to-control-Arduino-Uno/

However, when modifying the codes in order to control another LED, it doesn't work anymore.
Mostly probably because I don't quite well know what I'm doing, any help as to what lines I should add to
control another LED(like one connected to PIN12) would be really helpful. Thanks!
 

Harald Kapp

Moderator
Moderator
How do you conect the other LED? (which pin, which orientation, which resistor -> show us a little schematic circuit diagram.)

Where and how did you modify the code? (VB-code, arduino code) Show us the respective code snipets.
 
How do you conect the other LED? (which pin, which orientation, which resistor -> show us a little schematic circuit diagram.)

Where and how did you modify the code? (VB-code, arduino code) Show us the respective code snipets.
I was planning to use PIN0 to PIN13 as outputs, from each pin is a 390 ohm resistor which then connects to an LED and then to the ground.

After a while, I was able to get this to work on 5 LEDs but I guess the problem I'm encountering now is that the Arduino cannot read two digit numbers at once so LED6 (val == 10) and higher don't work at all.
Here are my current codes:
Code:
Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1
    Shared _continue As Boolean
    Shared _serialPort As SerialPort

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Close()
        SerialPort1.PortName = "com4"
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default
    End Sub


    Private Sub btn1ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1ON.Click

        SerialPort1.Open()
        SerialPort1.Write("0")
        SerialPort1.Close()
    End Sub


    Private Sub btn1OFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1OFF.Click
        SerialPort1.Open()
        SerialPort1.Write("1")
        SerialPort1.Close()
    End Sub

    Private Sub btn2ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2ON.Click


        SerialPort1.Open()
        SerialPort1.Write("2")
        SerialPort1.Close()

    End Sub

    Private Sub btn2OFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2OFF.Click


        SerialPort1.Open()
        SerialPort1.Write("3")
        SerialPort1.Close()

    End Sub

    Private Sub btn3ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3ON.Click

        SerialPort1.Open()
        SerialPort1.Write("4")
        SerialPort1.Close()

    End Sub

    Private Sub btn3OFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3OFF.Click

        SerialPort1.Open()
        SerialPort1.Write("5")
        SerialPort1.Close()

    End Sub

    Private Sub btn4ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4ON.Click

        SerialPort1.Open()
        SerialPort1.Write("6")
        SerialPort1.Close()

    End Sub

    Private Sub btn4OFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4OFF.Click
        SerialPort1.Open()
        SerialPort1.Write("7")
        SerialPort1.Close()

    End Sub

    Private Sub btn5ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5ON.Click

        SerialPort1.Open()
        SerialPort1.Write("8")
        SerialPort1.Close()

    End Sub

    Private Sub btn5OFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5OFF.Click
        SerialPort1.Open()
        SerialPort1.Write("9")
        SerialPort1.Close()
    End Sub

    Private Sub btn6ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6ON.Click
        SerialPort1.Open()
        SerialPort1.Write("10")
        SerialPort1.Close()
    End Sub

    Private Sub btn6OFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6OFF.Click
        SerialPort1.Open()
        SerialPort1.Write("11")
        SerialPort1.Close()
    End Sub

    Private Sub btn7ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7ON.Click
        SerialPort1.Open()
        SerialPort1.Write("12")
        SerialPort1.Close()
    End Sub

    Private Sub btn7OFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7OFF.Click
        SerialPort1.Open()
        SerialPort1.Write("13")
        SerialPort1.Close()
    End Sub

    Private Sub btn8ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8ON.Click
        SerialPort1.Open()
        SerialPort1.Write("14")
        SerialPort1.Close()
    End Sub

    Private Sub btn8OFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8OFF.Click
        SerialPort1.Open()
        SerialPort1.Write("15")
        SerialPort1.Close()
    End Sub

    Private Sub btn9ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9ON.Click
        SerialPort1.Open()
        SerialPort1.Write("16")
        SerialPort1.Close()
    End Sub

    Private Sub btn9OFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9OFF.Click
        SerialPort1.Open()
        SerialPort1.Write("17")
        SerialPort1.Close()
    End Sub

    Private Sub btn10ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn10ON.Click
        SerialPort1.Open()
        SerialPort1.Write("18")
        SerialPort1.Close()

    End Sub

    Private Sub btn10OFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn10OFF.Click
        SerialPort1.Open()
        SerialPort1.Write("19")
        SerialPort1.Close()
    End Sub

    Private Sub btn11ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn11ON.Click
        SerialPort1.Open()
        SerialPort1.Write("20")
        SerialPort1.Close()
    End Sub

    Private Sub btn11OFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn11OFF.Click
        SerialPort1.Open()
        SerialPort1.Write("21")
        SerialPort1.Close()
    End Sub

    Private Sub btn12ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn12ON.Click
        SerialPort1.Open()
        SerialPort1.Write("22")
        SerialPort1.Close()
    End Sub

    Private Sub btn12OFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn12OFF.Click
        SerialPort1.Open()
        SerialPort1.Write("23")
        SerialPort1.Close()
    End Sub
End Class

Code:
int ledPin1 = 0;
int ledPin2 = 1;
int ledPin3= 2;
int ledPin4= 3;
int ledPin5= 4;
int ledPin6 = 5;
int ledPin7 = 6;
int ledPin8= 7;
int ledPin9= 8;
int ledPin10= 9;
int ledPin11 = 10;
int ledPin12 = 11;


void setup()
{
Serial.begin(9600);
pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);
pinMode(ledPin3,OUTPUT);
pinMode(ledPin4,OUTPUT);
pinMode(ledPin5,OUTPUT);
pinMode(ledPin6,OUTPUT);
pinMode(ledPin7,OUTPUT);
pinMode(ledPin8,OUTPUT);
pinMode(ledPin9,OUTPUT);
pinMode(ledPin10,OUTPUT);
pinMode(ledPin11,OUTPUT);
pinMode(ledPin12,OUTPUT);

digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,LOW);
digitalWrite(ledPin8,LOW);
digitalWrite(ledPin9,LOW);
digitalWrite(ledPin10,LOW);
digitalWrite(ledPin11,LOW);
digitalWrite(ledPin12,LOW);

}


void loop()

{

char str = Serial.read() - '0';

if (str == 0)
{
digitalWrite(ledPin1, HIGH);
}

  else if (str == 1)

{

digitalWrite(ledPin1, LOW);
}



else if (str == 2)
{

digitalWrite(ledPin2, HIGH);
}

else if (str == 3)
{

digitalWrite(ledPin2, LOW);
}

else if (str == 4)
{

digitalWrite(ledPin3, HIGH);
}

else if (str == 5)
{

digitalWrite(ledPin3, LOW);
}

else if (str == 6)
{

digitalWrite(ledPin4, HIGH);
}

else if (str == 7)
{

digitalWrite(ledPin4, LOW);
}
else if (str == 8)
{

digitalWrite(ledPin5, HIGH);
}

else if (str == 9)
{

digitalWrite(ledPin5, LOW);
}

else if (str == 10)
{

digitalWrite(ledPin6, HIGH);
}

else if (str == 11)
{

digitalWrite(ledPin6, LOW);
}
else if (str == 12)
{

digitalWrite(ledPin7, HIGH);
}

else if (str == 13)
{

digitalWrite(ledPin7, LOW);
}

else if (str == 14)
{

digitalWrite(ledPin8, HIGH);
}

else if (str == 15)
{

digitalWrite(ledPin8, LOW);
}
else if (str == 16)
{

digitalWrite(ledPin9, HIGH);
}

else if (str == 17)
{

digitalWrite(ledPin9, LOW);
}

else if (str == 18)
{

digitalWrite(ledPin10, HIGH);
}

else if (str == 19)
{

digitalWrite(ledPin10, LOW);
}
else if (str == 20)
{

digitalWrite(ledPin11, HIGH);
}

else if (str == 21)
{

digitalWrite(ledPin11, LOW);
}

else if (str == 22)
{

digitalWrite(ledPin12, HIGH);
}

else if (str == 23)
{

digitalWrite(ledPin12, LOW);
}

}
 

Harald Kapp

Moderator
Moderator
the problem I'm encountering now is that the Arduino cannot read two digit numbers at once
It can't because you don't tell him to do so:
Code:
char str = Serial.read() - '0';
reads a single character from the serial port, but 10, 1 etc. are 2 digit numbers.

There are at least 2 diferent ways to counter that issue:
  1. encode 10, 11, 12 as A, B, C etc.. This way you need only one char to encode these 2 digit numbers. This will work for small numbers, but will get out of your hand when the number of available different characters is exceeded (X, Y, Z ???).
  2. read the number as string using ReadString(), then convert the string to a number using ToInt(). This has the advantage that the numbers you can transmit can easily be very high, even negavitve, if required.
I'd prefer method 2.

Another note on your code:
Code:
if (str == 0)
{
digitalWrite(ledPin1, HIGH);
}
  else if (str == 1)
{
digitalWrite(ledPin1, LOW);
}
else if (str == 2)
{
digitalWrite(ledPin2, HIGH);
}
else if (str == 3)
...
is not easy to read and will be hard to maintain.
Use a switch case statement (Google for examples) to beautify the code.
 
Top