Introduction: Getting Started With Arduino and Visual Basic 2008

About: We are group of makers who love and create different products related to hobby electronics, automation and OEM consumer and industrial products.

Making of Gui is always one of the interesting and enjoyable thing in controlling system. in this instructable we are going to learn making of Gui with visual basic 2008 and arduino .So here we start With Led

Part to be Required

1.LED

2.220 ohm Resistor

3.Arduino board

Step 1: Bread Board Circuit

Arduino Program

int led= 2;

void setup() {

Serial.begin(9600);

pinMode(led, OUTPUT);

}

void loop() {

if (Serial.available())

{

int Blinkdata = Serial.read();

if (Blinkdata=='a')

{

digitalWrite(led,HIGH);

}

else if(Blinkdata=='b')

{

digitalWrite(led,LOW);

}}

}

Step 2: Making of Visual Basic Form

Make Form as Shown in Image with the help of

2 buttons,

Label,

2 Combo box,

1 Serial Port

Step 3: Visual Basic 2008 Source Code

Define IO Libraries

Imports System.IO

Imports System

Imports System.ComponentModel

Imports System.Threading

Imports System.IO.Ports

Public Class Form1

Dim myPort As Array

Shared _continue As Boolean

Shared _serialPort As SerialPort

Exit Button

Private Sub Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

Application.Exit()

End Sub

Connect Button

Private Sub Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Connect.Click

SerialPort1.PortName = ComboBox1.Text

SerialPort1.BaudRate = ComboBox2.Text

SerialPort1.Parity = IO.Ports.Parity.None

SerialPort1.StopBits = IO.Ports.StopBits.One

SerialPort1.DataBits = 8

SerialPort1.Open()

Button2.Enabled = True

Button3.Enabled = True

End Sub

Disconnect Button

Private Sub Disconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Disconnect.Click

SerialPort1.Close()

Button2.Enabled = False

Button3.Enabled = False

End Sub

Commport Combo box

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

If SerialPort1.IsOpen = False Then

SerialPort1.PortName = ComboBox1.Text

Else

MsgBox("You cant change comm Port While its open!", vbCritical)

End If

End Sub

Baudrate Combo box

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

If SerialPort1.IsOpen = False Then

SerialPort1.BaudRate = ComboBox2.Text

Else

MsgBox("You cant change comm Port While its open!", vbCritical)

End If

End Sub

Form Load

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Button2.Enabled = False

Button3.Enabled = False

'Partial Load

myPort = IO.Ports.SerialPort.GetPortNames()

ComboBox2.Items.Add(9600)

ComboBox2.Items.Add(38400)

For i = 0 To UBound(myPort)

ComboBox1.Items.Add(myPort(i))

Next

End Sub

LED on

Private Sub ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

SerialPort1.Write("a")

End Sub

LED OFF

Private Sub OFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

SerialPort1.Write("b")

End Sub

End Class

Step 4: Modify Form and Visual Basic 2008 Program to Control Robokits Wireless 865-869 MHz Robot

Part to be required

Robokits RF 865 Mhz Robot

http://robokits.co.in/robot-kits/absolute-beginner-robot-kit/arduino-uno-r3-compatible-865mhz-wireless-robot-diy-kit

Cp2102 USB to Uart Module

http://robokits.co.in/development-boards/interface...


Visual Basic 2008 Program

Define IO Library

Imports System.IO

Imports System

Imports System.ComponentModel

Imports System.Threading

Imports System.IO.Ports

Public Class Form 1

Public Class Form1

Dim myPort As Array

Shared _continue As Boolean

Shared _serialPort As SerialPort

Connect Button

Private Sub Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

SerialPort1.PortName = ComboBox1.Text

SerialPort1.BaudRate = ComboBox2.Text

SerialPort1.Parity = IO.Ports.Parity.None

SerialPort1.StopBits = IO.Ports.StopBits.One

SerialPort1.DataBits = 8

SerialPort1.Open()

Button3.Enabled = True

Button4.Enabled = True

Button5.Enabled = True

Button6.Enabled = True

Button8.BackColor = Color.Green

End Sub

Disconnect Button

Private Sub Disconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

SerialPort1.Close()

Button8.BackColor = Color.Red

Button3.Enabled = False

Button4.Enabled = False

Button5.Enabled = False

Button6.Enabled = False

End Sub

Combobox 1 Commport

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

If SerialPort1.IsOpen = False Then

SerialPort1.PortName = ComboBox1.Text

Else

MsgBox("you cant change port while connected!", vbCritical)

End If

End Sub

Combobox 2 Buadrate

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged

If SerialPort1.IsOpen = False Then

SerialPort1.BaudRate = ComboBox2.Text

Else

MsgBox("you cant change port while connected!", vbCritical)

End If

End Sub

Form Load

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Button8.BackColor = Color.Red

Button3.Enabled = False

Button4.Enabled = False

Button5.Enabled = False

Button6.Enabled = False

myPort = IO.ports.serialport.getportnames()

ComboBox2.Items.Add(9600)

ComboBox2.Items.Add(38400)

For i = 0 To UBound(myPort)

ComboBox1.Items.Add(myPort(i))

Next

End Sub

Exit Button

Private Sub Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click

Application.Exit()

End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

End Sub

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

End Sub

About Button

Private Sub ABOUT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UA.Click

MsgBox("THIS SOFTWARE IS CREATED BY ROBOTICS INDIA FOR MORE INFO PLEASE VISIT WWW.ROBOKITS.CO.IN")

End Sub

Movement Buttons

Private Sub Button5_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Button5.KeyPress
SerialPort1.Write("D")

End Sub

Private Sub Button6_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Button6.KeyPress

SerialPort1.Write("A")

End Sub

Private Sub Button4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Button4.KeyPress

SerialPort1.Write("S")

End Sub

Private Sub Button3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Button3.KeyPress

SerialPort1.Write("W")

End Sub

End Class

Step 5: Assembling Robokits WIRELESS RF SERIAL LINK 865-869 MHZ ROBOT.

Step 6: Upload Program to Robokits Roboduino Board

#include //Include motor shield lib into arduino lib

for motor related functions.

#define Duration 100 //Duration for exiting the loop

DCMotor Amotor(1); // Creating instances for Motor 1

DCMotor Bmotor(2); // Creating instances for Motor 2

long previousMillis; // variables to scan for time alterations

long currentMillis;

byte byteRead; // variable to store Serial data

void setup()

{

Serial.begin(9600);

}

void loop()

{

currentMillis = millis(); //note the current time

if (Serial.available())

{

byteRead = Serial.read(); //Read Serial data

previousMillis = currentMillis;

if(byteRead == 'W' || byteRead == 'w' ) // Compare serial data

{

Forward(255);

}

else if(byteRead == 'S' || byteRead == 's')

{

Backward(255);

}

else if(byteRead == 'A' || byteRead == 'a')

{

Left(255);

}

else if(byteRead == 'D' || byteRead == 'd')

{

Right(255);

} }

else if(currentMillis - previousMillis >= Duration) // If no signal is

{

Stop();

} }

void Forward(unsigned char Speed) //Forward Function

{

Amotor.run(FORWARD);

Bmotor.run(FORWARD);

Amotor.setSpeed(Speed);

Bmotor.setSpeed(Speed);

}

void Backward(unsigned char Speed) //Backward Function

{

Amotor.run(BACKWARD);

Bmotor.run(BACKWARD);

Amotor.setSpeed(Speed);

Bmotor.setSpeed(Speed);

}

void Left(unsigned char Speed) //Turn Left Function

{

Amotor.run(BACKWARD);

Bmotor.run(FORWARD);

Amotor.setSpeed(Speed);

Bmotor.setSpeed(Speed);

}

void Right(unsigned char Speed) //Turn Right Function

{

Amotor.run(FORWARD);

Bmotor.run(BACKWARD);

Amotor.setSpeed(Speed);

Bmotor.setSpeed(Speed);

}

void LMF(unsigned char Speed) //left motor Forward

{

Amotor.run(FORWARD);

Amotor.setSpeed(Speed);

}

void LMB(unsigned char Speed) //left motor Backward

{

Amotor.run(BACKWARD);

Amotor.setSpeed(Speed);

}

void RMF(unsigned char Speed) //Right motor Forward

{

Bmotor.run(FORWARD);

Bmotor.setSpeed(Speed);

}

void RMB(unsigned char Speed) //Right motor backward

{

Bmotor.run(BACKWARD);

Bmotor.setSpeed(Speed);

}

void Stop(void) //All motors stop to stop the robot

{

Amotor.setSpeed(0);

Bmotor.setSpeed(0);

// delay(10);

}

Step 7: Download Zip File

Download Source code and All related Documents

http://robokits.download/downloads/Wire%20_1186%20Robot.iso