Skip to main content

Visual Basic & Arduino


The project I'm working on just now involves writing a Program in Visual Basic 6.0 (a Programming Environment I'm comfortable in) linked to an Arduino Uno. So far, I've got as far as controlling a set of traffic lights (3 LEDs on a bit of Breadboard) but I'm happy with that progress so far.  There's a long way to go on this, a steep learning curve, but courtesy of Covid 19, I have a lot more time to devote to this kind of project than I would usually have at this time of year.
So far, I have one way communication, next challenge is to get information going the other way.  Web forums are very useful things.



For those of you who want to see the "nuts & bolts", here is the Arduino Code

int redled= 12;
int amberled=10;
int greenled=8;
char Inpchar;

void setup() 
{
pinMode(redled,OUTPUT);
pinMode(amberled,OUTPUT);
pinMode(greenled,OUTPUT);

Serial.begin(9600);

void loop()
{
if (Serial.available())  { Inpchar = Serial.read();}  // Reads single character from Input Port

if (Inpchar=='R')  { analogWrite(redled,170);   } //  f sets Red   LED On
if (Inpchar=='r')  { digitalWrite(redled,LOW);    } //  e sets Red   LED Off
if (Inpchar=='A')  { analogWrite(amberled,30); } //  d sets Amber LED On
if (Inpchar=='a')  { digitalWrite(amberled,LOW);  } //  c sets Amber LED Off
if (Inpchar=='G')  { digitalWrite(greenled,HIGH); } //  b sets Green LED On
if (Inpchar=='g')  { digitalWrite(greenled,LOW);  } //  a sets Green LED Off

if (Inpchar=='M')  { Serial.write("X");  }    //try and send a character back to VB

}

And here's the Visual Basic Code.

The Logic is to send an "R" to switch on the Red LED and an "r" to switch it off again.
MSComm1.Output is the VB command to send data to the serial Port
"Red" is the name used for one of the shapes on the screen, so when I want to change the colour, we need the VB command  Red.fillcolor = VBred   or   Red.fillcolor = VBwhite

Public sequence As Integer
Public Portnumber As Integer

Private Sub Command1_Click()

If sequence = 0 Then
  MSComm1.Output = "R"
  Red.FillColor = vbRed
  MSComm1.Output = "g"
  Green.FillColor = vbWhite
  MSComm1.Output = "a"
  Amber.FillColor = vbWhite
End If

If sequence = 1 Then
  MSComm1.Output = "A"  'Amber on
  Amber.FillColor = &HFFFF&
End If

If sequence = 2 Then
    MSComm1.Output = "a"  'Amber Off
    Amber.FillColor = vbWhite
    MSComm1.Output = "r"  ' Red off
    Red.FillColor = vbWhite
    MSComm1.Output = "G"  'Green On
    Green.FillColor = &HC000&
End If

If sequence = 3 Then
    MSComm1.Output = "g"  ' Green Off
    Green.FillColor = vbWhite
    MSComm1.Output = "A"  ' Amber on
    Amber.FillColor = &HFFFF&
End If

If sequence = 4 Then
    MSComm1.Output = "a"  'Amber Off
    Amber.FillColor = vbWhite
    MSComm1.Output = "R"  ' Red On
    Red.FillColor = vbRed
End If

sequence = sequence + 1
If sequence = 5 Then sequence = 1
Text1.Text = sequence
 
End Sub

Private Sub Command2_Click()

End Sub

Private Sub Form_Load()

Text1.Text = ""
sequence = 0
End Sub

Private Sub GreenLight_Click()
If Green.FillColor = &HC000& Then
   MSComm1.Output = "g"
  Text1.Text = "Green is OFF"
  Green.FillColor = vbWhite
Else
   MSComm1.Output = "G"
  Text1.Text = "Green is ON"
  Green.FillColor = &HC000&
End If
End Sub

Private Sub AmberLight_Click()
If Amber.FillColor = &HFFFF& Then
   MSComm1.Output = "a"
  Text1.Text = "Amber is OFF"
  Amber.FillColor = vbWhite
Else
   MSComm1.Output = "A"
  Text1.Text = "Amber is ON"
  Amber.FillColor = &HFFFF&
End If
End Sub

Private Sub RedLight_Click()
If Red.FillColor = vbRed Then
   MSComm1.Output = "r"
  Text1.Text = "Red is OFF"
  Red.FillColor = vbWhite
Else
   MSComm1.Output = "R"
  Text1.Text = "Red is ON"
  Red.FillColor = vbRed
End If
End Sub

Private Sub SelectPort_Click()
 On Error GoTo porterror
   Portnumber = InputBox("Enter a number for the Comm Port", "Choose Port 1 .. 8", 3)
   MSComm1.RThreshold = 3
   MSComm1.Settings = "9600,n,8,1"
   MSComm1.CommPort = Portnumber '  (Write Port Number on which your Arduino Board is available).
   MSComm1.PortOpen = True
   MSComm1.DTREnable = False
   MSComm1.Output = "X"
   SelectPort.Visible = False
   Exit Sub
porterror:
MsgBox ("Invalid Port Number" & Chr(13) & "Use numbers from 1 to 8")

End Sub

Private Sub TestSerial_Click()
  MSComm1.Output = "M"
End Sub


Private Sub Timer1_Timer()
If MSComm1.PortOpen = False Then Exit Sub
inputchar = MSComm1.Input
         If MSComm1.CommEvent = comEvReceive Then
           Text1.Text = "recieved data " & inbuffer
       '     Text1.Text = Text1.Text & InBuffer
         End If
If inputchar = "X" Then TrafficLights.BackColor = vbGreen
End Sub



Jim McPherson

Comments

Popular posts from this blog

3D Printing Presentation

Sunday's Zoom meeting had our largest audience so far with 19 members attending from various parts of the country. Alan Geekie gave us an excellent and well received presentation on 3D printing that included FDM (Fused Deposition Modeling) and SLA (Stereolithography apparatus) types He first showed how we can source "things to print" using sites like Thingiverse. His own filament printer was the Prusa i3 Mk 3S which is available either as a kit (£699) or pre-assembled (£899) and features a self leveling bed. It also benefited from an additional multi material upgrade kit and he demonstrated the start up sequence of leveling, homing, clearing remaining filament from the hot end and then beginning the print.  The process of slicing where a 3d object drawing file (.stl) is broken up into the x,y,z drawing coordinates for each individual layer was also explained. Alan then moved on to resin printers using another Prusa model, the SL1 and its associated curing and washing mach

Train sequence / timetable using Arduino

                              Following discussions in a MERG Zoom meeting about potential projects for the Cumbria virtual area group I have started this blog to share my ideas for building a train sequence / timetable system based on an Arduino UNO. The trigger for this project was Andy Robb's article in the MERG journal (June 2020 edition). In it Andy describes using an UNO with a OLED display to produce an electronic station display board.  Having tried out Andy's version I started thinking about expanding the idea and have come up the following list of possibilities: 1. Replace my card index train sequence with an electronic version. 2. Have the train sequence synchronised with the on platform displays. 3. Display an analogue clock on the station display and have it display the train times. 4. Store the position reached in the sequence so that it starts where it left off on power up.  To make the project of greater potential interest to other modellers the following

Arduino Book Recommendation

Welcome to the site which has been set up for all members of the Scottish Borders Group of MERG.  As you know, I have been working on various Arduino projects recently including the station and cafe displays.  There are many resources available online when you come across a problem but a good reference book which explains how things work is essential.  I have tried a few and found the following book the best so far. It is well written and laid out logically for the beginner to be able to develop new skills. The structure of the Chapters makes it easy to find the relevant section.   By clicking on the link below you will be able to look inside and see for yourself. https://amzn.to /38HfrWW If you decide to purchase through this link  you should be aware that I will receive a small commission on any sales generated which will help to finance further projects Andy R