Visual Basic, Firmata and arduino

Hello,

Can someone explain to me the following example and how to modify it to controll a servo :slight_smile:

#include <Firmata.h>

byte analogPin;

void analogWriteCallback(byte pin, int value)
{
    pinMode(pin,OUTPUT);
    analogWrite(pin, value);
}

void setup()
{
    Firmata.setFirmwareVersion(0, 1);
    Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
    Firmata.begin();
}

void loop()
{
    while(Firmata.available()) {
        Firmata.processInput();
    }
    for(analogPin = 0; analogPin < TOTAL_ANALOG_PINS; analogPin++) {
        Firmata.sendAnalog(analogPin, analogRead(analogPin)); 
    }
}

Can someone explain to me the following example and how to modify it to controll a servo

Yes and maybe.

Why do you think you need to use Firmata if all you are doing is controlling a servo?

I'm still experimenting with servos and controlling them with a mouse I have already made a program that gets cursors position and "translates" the numbers to ones arduino can use to set the servo's angle.

That doesn't explain why you need to use Firmata. You can send serial data directly to the Arduino, and write your own sketch to read, parse, and use that data.

If you must use Firmata, use the ServoFirmata sketch on the Arduino, and the sendAnalog function to send the servo position to the servo pin.

The program that get's the cursor's positon is made i Visual Basic so i want to use Firmata to send the positons to arduino...

Don't the servos use Digital PWM?

Don't the servos use Digital PWM?

They do. There is a sample sketch provided with the Arduino Firmata library that does handle servo control. Try loading that one on the Arduino.

Ok, Thanks!

So, I've uploaded the sketch to my arduino (UNO) and connected it to Pin 9.

Made a new "Form" in visual basic added FirmataVB object to the form and Digital Pin Control Object.

In the FirmataVB proporties i set the com port to COM16 (The one my arduino uses) and the baud rate to 57600 (The one used in the servo example)

Under Digital Pin Control proporties i set the PinMode to "OUTPUT_PWM" and the PinNumber to 9 (I have the servo connected to pin 9)

The port has automaticly set to 1 I tried to move the slider in the program and nothing happend.

Can you give me a quick tutorial on how to make a basic setup to control one servo at the moment?

Couldn't find anything usefull on FirmataVB Analog Pin User Control

I hope I'm not annoying :slight_smile:

I also tried to use pySerial to control the servo but it "crashed and burned"

Is there any other way to send a simple number that ranges from 0 to 179 to the arduino in visual basic?

Is there any other way to send a simple number that ranges from 0 to 179 to the arduino in visual basic?

You can use the MSCOMM control to talk to the serial port. Once you can do that, you can send it anything you want.

Then, you write a sketch for the Arduino that reads the serial data, stores it in a character array (NULL terminated, of course), and then parses the data when the end-of-packet marker you wrote is received.

Finaly some sucess!

I have managed to light up an LED with my little program using the Serial Port control in Visual Basic

Can you tell me why it sends a number 72 instead of a string HIGH? :slight_smile:

72 is the ASCII code for the letter H.

Okidoks...

I'll see if I'll come up with something to convert ASCII to something arduino can send to servos

note: Didn't research anything yet...

Alright...

So I think I should do like this:

1.In visual basic I should send the number in "Parts" eg if i have a number 99 it would firts send a 0 then 9 and then 9 which should translate in 48 57 57.

  1. those three numbers I would temporarly store in EEPROM.

  2. Then I would use if statments to convert from ASCII to numbers and then store the three again in EEPROM

  3. Again I would "grab" the numbers from EEPROM and "assemble" the numbers back to 99.

NOTE1: I don't know if there are already any implemented functions that let me bypess the process above...

There is no reason to store the data in EEPROM. That's what variables are for.

If you have 99 in "Parts" (whatever that is), or any number that will not exceed 255, you could send the value as a byte, rather than as a string.

Then, the Arduino needs to do nothing more than read the number.

If the number could exceed 255, then sending it as a string is the next easiest thing to do. The problem with that is that you don't know what characters in the string go together to form a number. You need to add start and end of packet markers, around the number, like <582>.

Then, the Arduino can read serial data, ignoring it until it sees a start of packet marker (<). It then reads everything, in a loop. If the character read is not a >, then it gets stored in an array. If it IS a >, then, break out of the loop and process the string.

Search the forum for "started && ended" for an example of how to read serial data with start and end markers, and store the data between them.

Once you have the characters that represent a number, with a NULL terminator, you can use atoi to convert the string to an integer.