Skip to main content

Visual Basic, Arduino and Points

 Step 3 in this process was to get the Servo to rotate more slowly than before.

This is the modified Arduino Code

// the next block of code sets up a loop in the Program to rotate the Servo 1 step at a time.
//  This way, we get a 'smooth' movement, rather than a sudden one

if (Inpchar=='A') {                         // checks Input character A for Anticlockwise
    delay(300);                                 // a wee pause (for dramatic effect!! )
    for (int i = 0; i <= 135; i++) {    //  a variable i goes 1, 2, 3, 4 . . up to 135
          servoPos = i;                              //  send the new value to the Servo controller
          delay(30);                                   // a very wee pause (30 milliseconds)
//        30 milliseconds * 135 steps means 4,050 milliseconds, so just over 4 seconds

           myServo.write(servoPos);         // move the Servo to the new value of i (1, 2, 3, etc)
    }       //  This is used in Arduino code to finish the Loop and go back to the "for" statement  
             //  and then repeat the loop until it reaches 135.
   Serial.print("H"); delay(2000); Serial.write("h");
         // This finishes off the "if" condition      (  Look for the matching pairs of colours ) to help
             // to understand the Program structure.

So far, there's no need to edit the VB Code.   (Yet) 😂

Here's the full code for the Arduino Program

#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
Servo myServo;        //  Give the Servo a name

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

void loop() {
if (Serial.available())                       // Check if Serial is enabled
    { Inpchar = Serial.read();}           // if it is then read a character
if (Inpchar=='E') {servoPos = 0;}    // if the characer is E then set servoPos variable to a value of 0
myServo.write(servoPos);               // return the Servo to the starting position

// the next block of code sets up a loop to rotate the Servo 1 step at a time
//  so that we get a 'smooth' movement, rather than a sudden one
if (Inpchar=='A') {                         // checks Input character A for Anticlockwise
   delay(300);                                  // a wee pause (for dramatic effect!! )
    for (int i = 0; i <= 135; i++) {    //  a variable i goes 1, 2, 3, 4 . . up to 135
    servoPos = i;                              // give the Servo controller the new value
    delay(30);                                   // a very wee pause
    myServo.write(servoPos);          // move the Servo to the new value of i
    }

   Serial.print("H"); delay(2000); Serial.write("h");   // Sends information back to the VB Program
  }

// Exactly the same logic except  'C' makes the Servo go clockwise
if (Inpchar=='C') {  
    delay(300);
    for (int i = 0; i <= 135; i++) {  
    servoPos = (135 - i);                          //  This is the clever line
                                                               //      as  i  goes    0,  1,   2,   3,   4 . . 135
                                                               //      servoPos goes 135, 134, 133, 132 . . down to 0 
     delay(30);
    myServo.write(servoPos);
    } 

    Serial.print("L");delay(2000); Serial.print("l");  // More info. sent back to the VB Program
  } 

Inpchar = 'X';     // sets Inpchar to a 'Non Functional character 
                           // so that it doesn't try and repeat the same action
}

All that this "upgrade" to the Program does is to make the Servo rotate from one position to another at a slower pace than the sudden change that happened before.  Not much in the grand scheme of things, but on a display layout, connected to a set of points, it will look much better.

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

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