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

Sunday's West of Scotland Area Group Zoom meeting

 Another very well attended Zoom meeting with 34 participants. Davy showed us the commercially available automatic train stopping unit available at £12.53 from Brimar and compared it with the almost identical PMP 15 kit from the MERG Kitlocker at  only £2.65.  With these kind of savings, MERG membership is worth every penny. Chic Thomson treated us to a very entertaining presentation on simulation software for Electronics. INKSCAPE  is a free open  source drawing package. There are many Arduino Simulators available but Arduino Simulator 1.5.1 is an excellent open source offering.  For those interested in PICs simulators are available within the JAL compiler (Just Another Language) and MPLAB Sim is available within MPLAB but seldom advertised. Electronics Workshop 5.1 is a program no longer being developed but a useful tool for investigating any electrical or electronic circuit. Apparently you can Google it but there are a few insecure sites so be careful. F...

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