VB Interface for Arduino Tutorial Series Part 1: Arduino Code

This is part 1 of the Creating a Visual Basic Interface for Arduino Tutorial.

In the first part of the tutorial, we’re going to focus on the Arduino part of the project. If you haven’t already downloaded the code, you can grab it at GitHub.

Understanding the Process

First, let’s take a moment to understand what we’re trying to do in the Arduino code. The Arduino code in this project is only receiving data; We’re not bothering to send anything back. This makes it pretty simple as all we have to do is:

  1. Wait for serial buffer to fill up with data from the VB program, then put the data into a string variable.
  2. Check if we got the right amount of data from the VB program.
  3. Split up the data into two parts: One for selecting the pin of the servo motor, the other for setting the servo position/speed.
  4. Convert the two parts into integers so we can properly use the values with the servo library.

That’s it! Let’s take a look at the code.

Code Walkthrough

#include <Servo.h>
Servo userservo;

This code sets up the servo library. “userservo” is what we use to identify the servo pin we’re using.

String receivedData;
String pinSelect, servoSpeed;

These strings are our pieces of data. “receivedData” is the full string of data we get from the VB program. “pinSelect” and “servoSpeed” are the two pieces of data we end up when we split “receivedData”.

void setup(){
Serial.begin(9600);
}

We start a serial connection at 9600 baud as soon as the Arduino is powered on.

void loop()
{
while(Serial.available()) {

delay(3);
char c = Serial.read();
Serial.println(c);
receivedData += c;
}

We’re now into the main looping program. The code in the while loop executes when there is data in the buffer. We start reading one character at a time from the serial buffer and adding it to the end of the “receivedData” string. The code in the while loop loops until the serial buffer is empty, so by the time we’re out of the while loop, “receivedData” should have the full string that was sent from the VB program.

if(receivedData.length() != 5){
receivedData = “”;
}

The next step is to check if we have the correct amount of data from the VB program. We’re expecting 5 characters in the string, so if there isn’t 5 characters, we’ll just throw out whatever we did get.

else{

pinSelect = receivedData.substring(0, 2);
int pinSelectInt = pinSelect.toInt();
userservo.attach(pinSelectInt);

If we did get the correct amount of data, we’ll split it up into two pieces of data. The first piece selects the pin the servo motor is connected to. The substring() function returns a piece of the string. The first parameter is the position of the first character we want, and the second parameter is the position after the last character we want. Note that the position of the first character in a string is 0. We want the first two characters of the string, so the arguments are 0 and 2.

The next line converts the new string into an integer, which is the data type that the servo library works with.

The following line attaches the selected pin to “userservo”. We need this to address our servo when we set the position/speed.

servoSpeed = receivedData.substring(2, 6);
int servoSpeedInt = servoSpeed.toInt();

receivedData = “”;

Repeat the process again for the second piece of data, which is the servo speed. The parameters for substring() this time is 2 and 6 because we want characters in positions 2 to 5 of the string.

userservo.write(servoSpeedInt);

The last line sets the servo position/speed to whatever the VB program sent.

}
}

And that’s the end of our program loop!

Coming up next: VB Form

In the next part of the tutorial, we’re going to design the Visual Basic form in Visual Studio. Stay tuned!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s