Skip to main content

Visual Basic Code in a Mimic Panel

Visual Basic is what is known as on Object Oriented Language, so you add Objects (Lines, Boxes, Shapes) to a form and then create some matching code which is executed when you Click (usually) on that object. 

The modified Form for the Points Program now looks like.

The objects on this form are Lines 1 and 2, Shapes 6 and 7 and a couple of Command Buttons called Clockwise and Anti Clockwise.  When we Click on Clockwise, then some code is executed.

This lot !!

Private Sub Clockwise_Click()
    MSComm1.Output = "C"                  ' Sends a "C" to the Arduino code which makes the Servo turn
    Clockwise.Enabled = False               ' Disables the "Clockwise" button so we can't press it by mistake
    Line2.BorderColor = &HC0C0FF    ' Changes Line 2 to a beige kind of colour
    Line1.BorderColor = &HC0C0FF    ' Does the same to line 1, which you can't really make out
    Shape7.FillColor = vbRed                 ' Changes the Signal (Shape7) from Green to Red
End Sub

At the end of the Arduino Code which rotates the Servo Clockwise, there's a line which looks like

   Serial.print("H"); delay(2000); Serial.write("h");

This sends an "H" back to the VB Program, pauses for a couple of seconds, then sends an "h".

This is the Code in the VB Program to receive the Input character and then carry out further code instructions depending on which character has been received.

Private Sub MSComm1_OnComm()                        'checks if something has happened at the Serial Port
    If MSComm1.CommEvent = comEvReceive Then      ' Checks to see if "something" is a receive
         inputchar = ""                                                           ' gets rid of anything left from beofre
         inputchar = MSComm1.Input                                 ' reads what character has just been received
            If inputchar = "H" Then Anticlock_Lines            ' If it is an "H" then go to a procedure called
                                                                                          '  Anticlock Lines    see below for what it's like.
            If inputchar = "h" Then Anticlock_Lights           ' Same idea
            If inputchar = "L" Then Clock_Lines
            If inputchar = "l" Then Clock_Lights
   End If
End Sub

Sub Anticlock_Lines()
      Line2.BorderColor = &H80FF80       'Changes the line colour to a kind of Beige
      Shape8.FillColor = vbRed                  ' Changes the Signal Colour to Red
      Line1.BorderColor = vbRed              ' Changes the Line1 colour to Red
      Line1.ZOrder (1)                               ' Brings line 1 to the top        (aesthetic appearance or the)
      Line2.ZOrder (0)                               ' Sends line 2 to the bottom  (overlap would look weird.)
End Sub

The complete Program

Private Sub EndProg_Click()
  MSComm1.Output = "E"
  End
End Sub

Private Sub Selectport_Click()
  On Error GoTo porterror
  portnumber = InputBox("Enter a number", "Choose Port 1 . . 8", 4)
  MSComm1.RThreshold = 1
  MSComm1.CommPort = portnumber
  MSComm1.PortOpen = True
  MSComm1.DTREnable = False
  MSComm1.Output = "X"
  Selectport.Visible = False
  EndProg.Visible = True
  Exit Sub
porterror:
  MsgBox ("Invalid Port Number" & Chr(13) & "Use numbers from 1 to 8")
End Sub

Private Sub AntiClock_Click()
  MSComm1.Output = "A"   ' Sends  A to Arduino (Anticlockwise)
  AntiClock.Enabled = False  ' VB Command to disable Anticlockwise Button
  MSComm1.Output = "g"       ' Sends  G  Switch on Green LED
  MSComm1.Output = "R"       ' Sends  r  Switch off Red LED
  Line1.BorderColor = &HC0C0FF
  Line2.BorderColor = &HC0C0FF
  Shape8.FillColor = vbRed
End Sub

Private Sub Clockwise_Click()
  MSComm1.Output = "C"
  Clockwise.Enabled = False
  Line2.BorderColor = &HC0C0FF
  Line1.BorderColor = &HC0C0FF
  Shape7.FillColor = vbRed
End Sub

Private Sub MSComm1_OnComm()
 If MSComm1.CommEvent = comEvReceive Then
   inputchar = ""
   inputchar = MSComm1.Input
     If inputchar = "H" Then Anticlock_Lines
     If inputchar = "h" Then Anticlock_Lights
     If inputchar = "L" Then Clock_Lines
     If inputchar = "l" Then Clock_Lights
 End If
End Sub

Sub Clock_Lines()
    Line2.BorderColor = vbRed
    Line1.BorderColor = &H80FF80
    Line1.ZOrder (0)
    Line2.ZOrder (1)
End Sub

Sub Clock_Lights()
    Shape8.FillColor = vbGreen
    AntiClock.Enabled = True
End Sub

Sub Anticlock_Lines()
      Line2.BorderColor = &H80FF80
      Shape8.FillColor = vbRed
      Line1.BorderColor = vbRed
      Line1.ZOrder (1)
      Line2.ZOrder (0)
End Sub

Sub Anticlock_Lights()
      Shape7.FillColor = vbGreen
      Clockwise.Enabled = True
End Sub

3 Pictures showing how some of the colours change and a video of the Servo working.
                      Before.                                     During                                         After  







Comments

  1. What are you using for serial communication between the PC and the Arduino?

    ReplyDelete

Post a Comment

Popular posts from this blog

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 mak...

Push button Ezypoints

 A few months ago I produced a batch of Ezypoint kits for our local members to overcome a shortage in the Kitlocker.  At that time the kit was produced on stripboard which some members find problematic. An additional PCB board normally available from MERG was not available either.  Alan Turner ( the PCB designer) had only a few boards available but graciously provided me with the gerber files to allow me to order PCBs directly from China . Having sourced all necessary components, the kits were duly made up. I find this simple kit is really useful for automation projects. A toggle switch provides either a HIGH or LOW on the PIC input pin to determine which of the two servo positions is selected. For a particular application, I wanted to use a push button to toggle the position on alternate presses of the button. This involved a rewrite of some of the code in the PIC. I initially developed the program using an Arduino as a means of testing the logic. When this was successfu...

Cumbria Group Meeting Friday 9th October

 Although this was only a coffee and chat meeting, eleven members joined us for an interesting session which basically covered what was on our workbenches. Some members suffered from technical gremlins on the Zoom session but hopefully they will have that sorted for next time. Andy Woolass had volunteered to be a guinea pig for building the latest version of an EzyBus Input Module. It was a fairly straightforward build but the lack of sufficient ground points for external connections was a suggested improvement. The original Input board which used an MCP23017 Module was originally supplied by Davy Dick for £2. Members trying to build their own using a provided PCB, sometimes fell foul of the different form factors of this Module which caused obvious fitting problems. It is for this reason the PCB has been redesigned to accept a more standard 28 Pin DIL device. The Kit will be available in the Kitlocker, but at £7.20 it is a little expensive. Various members showed Arduino developme...