IAN LANG ELECTRONICS

In which, just as the title says, we look at more Visual Basic and Arduino stuff and how they can be made to talk to each other. This little project was going to be a four-hour job. Due to technical disasters (more about that later) on Friday and a complete absence of working brain cells on my part on Saturday it turned into a twenty-three hour job. It's a project requested by a reader and I have to thank him for this idea as it bridges a gap I hadn't seen in the Arduino stuff on here. But first those technical disasters.

 

Visual Basic 2010 would not run in debug mode. I tried it four times and each time it went "Visual Basic is Busy with an Internal Process". I tried a de-reinstall. That didn't work so obviously another de-reinstall isn't going to do any good. But I did it anyway, and sure enough it failed completely. Then I tried a technique which has never been known to work, which is swearing creatively and profusely at the computer. Didn't work. Much head-scratching, pencil-chewing, coffee-and-tea drinking and smoking followed. Six hours later, it occured to me to reset the indexing on the hard disc. So I did. If you do that, you have to restart the computer for it to take effect. So I did. Visual Basic 2010 working again.

 

If you ever get that error message, go to your control panel, open indexing options (it might just say indexing) and click on the "Advanced" button. Then Restore Defaults, then OK, and then restart your computer. Don't threaten to whack it with a very large spanner as it does not respond well to such menacings.

 

The project. It's to get one of the Arduino's analogue inputs wired up to a pot and on request from the PC chuck out the reading of the analogue voltage. It does encompass a range of programming methods and is a good springboard for adapting to your projects. Wiring up your Arduino goes like this:

 

 

Go Back

More Visual Basic I/O Between a PC and an Arduino.

arduinowire

There isn't much to it- just stick one terminal of a pot ( I used a 10k here) to 5V and the other to ground and the wiper to A0 and Bob's your uncle. There's not much to the sketch either:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Let's have a canter through to see what it does. First of all in the setup we just initiate serial communications:

 

void setup(){

 Serial.begin(9600);

}

 

I'm using 9600 Baud because it's what the Arduino likes best; it's a nice compromise between speed and accuracy. We'll need to be able to set that in the VB app though. It's no problem. Let's look at the loop:

 

void loop(){

 

 if (Serial.available()){

 

Well, that's just as it says. If the Arduino gets some serial communication, then all the code in the conditional gets executed. If it doesn't get any serial comms then it doesn't do any of it; lazy Arduino. Here's what happens if we wake it up:

 

   char incoming = Serial.read();

 

That line above looks to see what the incoming data is. If it is a single character (which it will be when we write the VB app) which corresponds to ASCII value 63  (that's a question mark  ?  ) the next line opens a new conditional:

 

   if (incoming ==63){

and the code under that conditional says:

 

 delay(100);Serial.println(analogRead(A0));}}

 }

 

All of which delays for 1/10th of a second, and then prints out the reading from A0 and a new line down to the serial port. I'm going to emphasise the NEW LINE bit because the NEW LINE bit is very important and if you forget the NEW LINE bit you'll spend another umpteen hours head-scratching before you remember that you can code up much more simply in VB by using a NEW LINE bit after the data you want to send and what's more your app will work if you use the NEW LINE bit and won't if you don't use the NEW LINE bit. Don't forget the NEW LINE bit,  it's the ln after Serial.print     (Serial.println means chuck out some data and then a NEW LINE).

 

Suitably emphasised, I think, and you'll see why the NEW LINE bit is important when we get to the VB side. Here it comes with a picture of the two forms:

 

 

 

 

 

void setup(){

 Serial.begin(9600);

}

 

void loop(){

 

 if (Serial.available()){

   char incoming = Serial.read();

   if (incoming ==63){

 delay(100);Serial.println(analogRead(A0));}}

 }

formdesignandrunning

On the left you see the design form which looks a horrendous splodge but on the right is how it looks when running. I made the form 300 by 300 but if you want to make it more aesthetically pleasing alter the size in the properties window like thus:

propwindow propexpanwin

and on the left there you see the size properties and if you click on the + sign to the left it expands as on the right- quite a lot of controls do this. Whilst you're in there, set the maximum box to false and give your form some text for the caption on the top. Mine says Arduino Pot Reader and if you look four down from the size property in the properties window you'll see where that gets typed in. Set the start position to centre screen. Apart from that you shouldn't have to do much else to the properties, but play about with the colours if you like, you'll find it under Backcolor.

 

VB is an event-driven language which means that it doesn't do anything until you click, double click, mouse-hover, mouse-leave, mouse enter or a variety of things on an object. An object is what you put on forms and there's dozens of them. The most obvious ones are buttons, but the first ones we are going to put on are actually components rather than objects. Let's start over the page with the serial port itself.

 

More >