Arduino and Pressure Sensing.

I'm currently working on using the Arduino as an ADC for an autonomous sub that will be used at this years AUVSI competition. My blog details the basic principles behind using the Arduino as an ADC. Currently I'm working on testing code related to communicating with the Arduino in C over serial from windows it should be posted by the end of the day or at latest Friday. I hope this helps anyone working on similar projects. (Pictures also coming soon.)

http://thecodebenders.tumblr.com/

As promised the code has been updated, there's now the serial code on there. Taking pictures tonight should be able to get them posted depending on just how tired I am.

C-Serial Code:

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#define n 1024

//Globals
HANDLE hSerial;                                            //Handle for serial port
FILE *fd;                                                //File Descriptor of file to write to serial handle

//Prototypes
int open_port();                                        //Opens serial port for reading and writing
int read();                                                //Reads information from serial port



int main () {

    int read()

    return 0;

}

int read()
{

    DWORD dwBytesRead = 0;
    printf(“\nAttempring to read from serial port?”);

    char szBuff[n + 1] = {0};
    int intBuff;
    int buffSum
    float floatBuff;

    while(1)
    {

        ReadFile(hSerial, szBuff, 1, &dwBytesRead, NULL); //get a char from the serial line
        if(szBuff[0]==”\n”) //if it's “\n” we've hit the beginning of a number start pulling it.
        {

            ReadFile(hSerial, szBuff, 4, &dwBytesRead, NULL); //pull all 4 chars
            for(int i=1,i<=4,i++){

                intBuff = atoi(szBuff[i]); //format the chars into ints
                buffSum += intBuff*10^(i-1); //make a big ole int out of the 4 values
                floatBuff = buffSum/1000 //make it a float

                setValue(char* table, char* depth, float floatBuff); //send the depth to the mySQL DB


            }

        }

        szBuff = {0};
        break;

    }
    return 1;

}


int open_port()
{

    //Tell user that port-opening has been initiated
    printf(“Opening Serial Port?\n”);

    //Announce current task
    printf(“Attaching handle?”);

    //Attempt to attach a hande to serial port
    hSerial = CreateFile(L”COM3”,

            GENERIC_READ | GENERIC_WRITE,
            0,
            0,
            OPEN_EXISTING,
            FILE_ATTRIBUTE_NORMAL,
            0);

    if(hSerial==INVALID_HANDLE_VALUE)
    {

        if(GetLastError()==ERROR_FILE_NOT_FOUND){

            //Print Error if neccessary
            printf(“ERROR: Handle was not attached.  Reason: COM3 not available.\n”);
            return 0;

        }
        //Print Error if neccessary
        printf(“\n\nWelp?that didn't work.  Explaination: it blew up?pretty much?”);
        return 0;

    }
    else printf(“done!”);                                //Successfully Attached handle to serial port

    //Announce current task
    printf(“\nSetting Parameters?”);

    //Set serial port parameters

    DCB dcbSerialParams = {0};
    //dcbSerial.DCBlength=sizeof(dcbSerialParams);
    if (!GetCommState(hSerial, &dcbSerialParams)) {

        printf(“failed!”);
        return 0;

    }
    dcbSerialParams.BaudRate=CBR_9600;
    dcbSerialParams.ByteSize=8;
    dcbSerialParams.StopBits=ONESTOPBIT;
    dcbSerialParams.Parity=NOPARITY;
    if(!SetCommState(hSerial, &dcbSerialParams)){

        printf(“\n\nALERT: Serial port failed!”);

    }
    else printf(“done!”);                        //Serial port has been successfully configured
    return 1;

}

Arduino ADC Code:

void setup() {

    Serial.begin(9600);  // set up Serial library at 9600kbps
    Serial.println(“Pressure Sensor Readout”);  //for serial make it look pretty purposes, can be commended out.
    pressure_offset = OffSet();  // builds the pressure offset from an average of 40 inputs (below main loop)

}


void loop()

    double pressure;
    double depth;
    double altimeter;
    double AnalogIn;

    //takes in the pressure and offsets.
    AnalogIn = analogRead(pressure_in)-pressure_offset;
    //Depth calculating algothim, 2.31 PSI per foot of water, 32.76 points analog in per PSI.
    depth = 2.31*AnalogIn/32.76;
    //Arduino doesn't support float printing, see algorithm below.
    printFloat(depth);
    //choke down the massive ammounts of serial data spamming.
    delay(500);
    Serial.print(“\n”);

}

double OffSet(){

    int i;  //iteration counter
    double sum; // where we put the value to be returned
    for(i=0; i<40; i++){

        sum += analogRead(pressure_in); //does the running sum for 40 values

    }
    return sum/40.0; // return the average

}


void PrintFloat( float f){

    Serial.print((int)f); //print the non decimal portion
    Serial.print(“.”); //print the decimal
    int tmp = (f - (int)f) * 100;  //print 2 decimals of the fractional part
    Serial.println( abs(tmp) );

}