I am working on a new software extension for a CAD package using Arduino. I would like to use the Standard Firmata to send/receive Serial data back and forth. My software is written in VB.NET and so far I can successfully send and receive serial data. Right now I have written a rudimentary Arduino program (like a crude custom firmata) to send sensor values out, and I am receiving CSV strings and parsing them successfully to control LED and servos. Nevertheless, I see the value of using the Standard Firmata. Is there someone out there that could assist me in understanding what serial values the Standard Firmata needs to receive (how many numbers, what order, etc) and also what SF sends out, etc. I imagine users like Massimo or eighthave would have a lot to offer this discussion. I have done a lot of research and hope some is willing to help. Right now I am using Standard Firmata V2.1 beta7. Thanks, hal9000
Just as a follow-up to my previous post - here is the Arduino code (my crude version of a custom Firmata) that I have written to send and receive serial data back and forth between Arduino and my VB.net code. I am using the Duemilanove so this code was written to write 6 Analog In values and 3 Digital In values. It also reads 6 PWM servo position values, and 3 digital out values. As you can see it would be much better to use the Standard Firmata though. I am trying to figure out whether it is best to use a "#include <Firmata.h>" similar to the example http://arduino.cc/en/Reference/Firmata, or simply upload the Standard Firmata to my board, then figure out how to communicate with it using serial /VB.net.
Thanks for any hints/assistance you may have!
#include <Servo.h>
#include <Wire.h>
#define BAUDRATE 115200
#define BUFFSIZE 1024 // buffer one command at a time, 12 bytes is longer than the max length
char buffer[BUFFSIZE]; // this is the double buffer
uint8_t bufferidx = 0; // a type of unsigned integer of length 8,16, or 32 bits
uint32_t DPin13, DPin12, PWM11, PWM10, PWM9, DPin8, PWM6, PWM5, PWM3;
/*==============================================================================
* GLOBAL VARIABLES
*============================================================================*/
char *parseptr;
char buffidx;
int APin0 = 0; //declare all Analog In pins
int APin1 = 0;
int APin2 = 0;
int APin3 = 0;
int APin4 = 0;
int APin5 = 0;
int DPin2 = 0; //declare all Digital In pins
int DPin4 = 0;
int DPin7 = 0;
Servo Servo1; // declare all Servo (PWM out pins)
Servo Servo2;
Servo Servo3;
Servo Servo4;
Servo Servo5;
Servo Servo6;
int DinPin8 = 0; //declare all Digital Out pins
int DinPin12 = 0;
int DinPin13 = 0;
/*==============================================================================
* SETUP()
*============================================================================*/
void setup()
{ Serial.begin(BAUDRATE); // Start serial communication at 9600 bps
for (int AinPin = 0; AinPin <= 5; AinPin++) // sets Analog In pin mode to INPUT
pinMode(AinPin, INPUT);
pinMode(2, INPUT); // sets Digital In pin mode to INPUT
pinMode(4, INPUT);
pinMode(7, INPUT);
Servo1.attach(3); // attach all Servos to specific PWM pins
Servo2.attach(5);
Servo3.attach(6);
Servo4.attach(9);
Servo5.attach(10);
Servo6.attach(11);
pinMode(8, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT); // sets Digital Out pin mode to OUTPUT
}
/*==============================================================================
* LOOP()
*============================================================================*/
void loop()
{
{
/// READ FROM ARDUINO
APin0 = analogRead(0); // Read analog input pin
APin1 = analogRead(1); // Read analog input pin
APin2 = analogRead(2); // Read analog input pin
APin3 = analogRead(3); // Read analog input pin
APin4 = analogRead(4); // Read analog input pin
APin5 = analogRead(5); // Read analog input pin
DPin2 = digitalRead(2); // Read digital input pin
DPin4 = digitalRead(4); // Read digital input pin
DPin7 = digitalRead(7); // Read digital input pin
/// SERIAL WRITE FUNCTIONS
// Sending Sensor Data (comma seperated) to Serial / GH
Serial.print(APin0);
Serial.print(","); // Send the value and a comma
Serial.print(APin1);
Serial.print(","); // Send the value and a comma
Serial.print(APin2);
Serial.print(","); // Send the value and a comma
Serial.print(APin3);
Serial.print(","); // Send the value and a comma
Serial.print(APin4);
Serial.print(","); // Send the value and a comma
Serial.print(APin5);
Serial.print(","); // Send the value and a comma
Serial.print(DPin2);
Serial.print(","); // Send the value and a comma
Serial.print(DPin4);
Serial.print(","); // Send the value and a comma
Serial.println(DPin7);
delay(20); // Wait 100ms for next reading
}
char c; // holds one character from the serial port
if (Serial.available()) {
c = Serial.read(); // read one character
buffer[bufferidx] = c; // add to buffer
Serial.print(c);//write raw data to serial for testing
if (c == '\n') {
buffer[bufferidx+1] = 0; // terminate it
Serial.println(" eol"); // debug marker "end of line"
parseptr = buffer; // offload the buffer into temp variable
//********************************************************
DPin13 = parsedecimal(parseptr); // parse the first number
parseptr = strchr(parseptr, ',')+1; // move past the ","
DPin12 = parsedecimal(parseptr); // parse the second number
parseptr = strchr(parseptr, ',')+1; // move past the ","
PWM11 = parsedecimal(parseptr); // parse the third number
parseptr = strchr(parseptr, ',')+1; // move past the ","
PWM10 = parsedecimal(parseptr); // parse the fourth number
parseptr = strchr(parseptr, ',')+1; // move past the ","
PWM9 = parsedecimal(parseptr); // parse the fifth number
parseptr = strchr(parseptr, ',')+1; // move past the ","
DPin8 = parsedecimal(parseptr); // parse the sixth number
parseptr = strchr(parseptr, ',')+1; // move past the ","
PWM6 = parsedecimal(parseptr); // parse the seventh number
parseptr = strchr(parseptr, ',')+1; // move past the ","
PWM5 = parsedecimal(parseptr); // parse the eighth number
parseptr = strchr(parseptr, ',')+1; // move past the ","
PWM3 = parsedecimal(parseptr); // parse the ninth number
//********************************************************
Servo1.write(PWM11); // tell servo to go to position in variable 'servo1Degrees'
Servo2.write(PWM10); // tell servo to go to position in variable 'servo2Degrees'
Servo3.write(PWM9); // tell servo to go to position in variable 'servo3Degrees'
Servo4.write(PWM6); // tell servo to go to position in variable 'servo4Degrees'
Servo5.write(PWM5); // tell servo to go to position in variable 'servo5Degrees'
Servo6.write(PWM3); // tell servo to go to position in variable 'servo6Degrees'
//********************************************************
if (DPin8 == 1) { // if it is equal to 1
digitalWrite(8, HIGH); // then turn LED on
}
else {
digitalWrite(8, LOW); // else turn LED off
}
if (DPin12 == 1) { // if it is equal to 1
digitalWrite(12, HIGH); // then turn LED on
}
else {
digitalWrite(12, LOW); // else turn LED off
}
if (DPin13 == 1) { // if it is equal to 1
digitalWrite(13, HIGH); // then turn LED on
}
else {
digitalWrite(13, LOW); // else turn LED off
}
//********************************************************
bufferidx = 0; // reset the buffer for the next read
return; //return so that we don't trigger the index increment below
}
// didn't get newline, need to read more from the buffer
bufferidx++; // increment the index for the next character
if (bufferidx == BUFFSIZE-1) { //if we get to the end of the buffer reset for safety
Serial.print('!', BYTE);
bufferidx = 0;
}
}
}
uint32_t parsedecimal(char *str)
{
uint32_t d = 0;
while (str[0] != 0) {
if ((str[0] > '9') || (str[0] < '0'))
return d;
d *= 10;
d += str[0] - '0';
str++;
}
return d;
}
Did you see this:
Looks something like what you are trying to (re)invent.