Skip to main content

Visual Basic, Arduino and a Servo

The overall plan is to end up with a Model Railway Layout which is being controlled from a Laptop computer by clicking (or pressing) buttons on some form of mimic panel displayed on the Laptop.

 The LEDs are working, so now on to getting a Servo to behave.

 Many thanks first of all to Paul McWhorter on Youtube for his series of video instructions

https://www.youtube.com/playlist?list=PLGs0VKk2DiYw-L-RibttcvK-WBZm8WLEP

Tutorials no. 30 and 31 are the ones about Servos, how to connect them and control them.

 The Visual Basic Code and Form

In VB, make up a form with 4 Buttons. 
For this simple version, we only need 2 of them.  We will ignore the "pretty coloured" ones just now!!


CW for Clockwise             ACW for Anti Clockwise

This is all the code needed for these buttons.

Private Sub Basic_AC_Click()     ' This is the code which is executed when the ACW button is used

      MSComm1.Output = "a"         
End Sub

Private Sub Basic_CW_Click()  ' This is the code which is executed when the CW button is used.
      MSComm1.Output = "c"
End Sub

Clicking on the “ACW” button sends a single character “a” to the Arduino. The serial Input in the Arduino reads this character and assigns it to a variable called Input_char

Arduino Code

The essential code in the Arduino looks like:-

          if (Input_char =='a') {servoPos = 135; myServo.write(servoPos);}              // instructs the Servo to turn to 135 degrees

          if (Input_char =='c') {servoPos = 0; myServo.write(servoPos);}                 // instructs the Servo to turn to 0 degrees

Essentially when the Arduino receives a single character “a”, then the Servo rotates anticlockwise by 1350  The character "c" makes it rotate Clockwise by  1350

These are the important lines and it now means that the Servo can be controlled by clicking buttons on a computer screen, or pressing them if it is a touch sensitive screen.

Those would be the functional lines of code and I wish it was that simple.  We need lots of extra lines however to enable communication. 

The nice thing is that having got the basics sorted, it will be a simple “Copy and Paste” job for future Programs.

VB6 Code to enable Serial Communication 

In VB6, the code to enable Serial communication will look like:-

    Portnumber = 4                               ‘sets port number as 4 – this may be different in other computers

    MSComm1.CommPort = portnumber            ‘tells the computer which is the selected Port
    MSComm1.PortOpen = True                              ‘Opens the port for Communication
    MSComm1.RThreshold = 1                                  ‘looks for a single character input
    MSComm1.DTREnable = False                           ‘I’m not quite sure what this does!!

 In the Arduino, we need additional code to enable Serial Communication as well as installing the Servo Library.

 #include <Servo.h>                    //  load up the Servo Library
 char Inpchar;                               // Single Character Input
 int servoPin=9;                           // allocates pin 9 to control the Servo
 int servoPos =00;                       // Create a Variable for the Servo position, give it a starting value of 0
 Servo myServo;                         //  Give the Servo a name

 

   // put your setup code here, to run once:
void setup() {
Serial.begin(9600);                      // Enable serial Communication
myServo.attach(servoPin);          // allocates Pin 9 to control the Servo

}

 void loop() {                                        //  This is the part of the Program that gets repeated continuously.
    if (Serial.available())                         // Check if Serial port is enabled.
    { Inpchar = Serial.read();}                // if it is enabled, then read a character.

 Once all that is done, if an Input Character is detected, these 2 lines are triggered if they are true.

if (Inpchar =='a') {servoPos = 135; myServo.write(servoPos);}              // instructs the Servo to turn to 135 degrees

if (Inpchar =='c') {servoPos = 0; myServo.write(servoPos);}                  // instructs the Servo to turn to 0 degrees

 Connecting the Servo
With this basic set up, we just need the 3 Servo cables to connect to 5V, Ground and Pin 9.

This runs fine for a single Servo, but you couldn’t use it to control too many as it would overload the Arduino.

With working Programs on the Laptop and Arduino, clicking a button on the Laptop screen makes the Servo Turn the direction selected.  

Comments

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

Safety in the workshop

 An excellent presentation by Dr. Craig Lennox on workshop safety at today's West of Scotland area group meeting. An amusing but nevertheless serious video was followed by a detailed talk on safety and first aid. I am hoping this talk will be made available on the WOSAG website in due course. The take home message is to ensure your workspace is well ventilated, tidy with no trailing cables. Plan what you are going to do using appropriate tools, avoid distractions and don't rush A clear head is essential. Use PPE where necessary for example ear protection, safety glasses and laser glasses. The risks of burns, cuts, electrical shock and inhalation of dust and fumes were all covered in some detail. Automatic External Defibrillator (AED) are now installed in most supermarkets. A short video was shown illustrating just how easy they are to be used. Excellent talk.  Keep safe everyone.

Mimic Points in Visual Basic

For this project, I wanted to create a moving graphic to mimic a set of points moving from one position to the other.  This Blog shows all that is needed. The first Picture shows the 6 lines needed.                                 This Picture shows the Line Properties.    The tracks are shown with 5 lines, though only the middle ones in red are involved.  The other line shown below the others is the one that actually "moves".  It is shown below the other lines for clarity only.  At the start of the Program running, it is positioned over the lower red line. In VB, each Line has properties and these are shown above.  To get a line to appear to move, all that needs to be done is to gradually change the X2 and Y2 co-ordinates of the line. This is the code for the program. The first 6 lines create a pause Function in the program, by using the Computers internal C...