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

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