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

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