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.
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.
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
Private Sub
Basic_AC_Click() ' This is the code which is executed when the ACW button is used
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
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.
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!!
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
Serial.begin(9600); // Enable serial Communication
}
if
(Serial.available()) // Check if Serial port is
enabled.
{ Inpchar = Serial.read();} //
if it is enabled, then read a character.
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
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
Post a Comment