Friday, May 24, 2013

Arduino sketch to read depth/pressure sensor


/*
//depthSensor1B sketch
//analog pressure sensor attached to analog pin
// added VCC measure directly to get better precision
// 1A works well
// 1B cleaned up code to make functions
// also added a DEBUG for Depth Sensor function that can be used if needed by uncommenting


*/

String sketchName = "depthSensor1B";

int pressureDepthPin = 3; // analog pin 3 connected to pressure sensor

float VCC = 5027.00;  // actual measured voltage of VCC in mV
//int analogPressure = 0;


void setup() {
  Serial.begin(9600);
  Serial.print("Program name: ");
  Serial.println(sketchName);
}

void loop() {
 readDepth();

 // Debug routines (uncomment to use
 // Debug Depth sensor
 debugReadDepth();
 
}

//++++++++++++++++++++++ Depth sensor +++++++++++++++++++++
void readDepth() {
    float depth = calculateDepth ();
    Serial.print("Depth = ");
    Serial.print(depth);
    Serial.println(" m ");
 
  delay(500);
}

// Debug depth sensor
void debugReadDepth () {
    float voltageDepthSensor = measureVoltage ();
    Serial.print("DEBUG: Voltage: ");
    Serial.print(voltageDepthSensor);
    Serial.print(" mV ");
    float pressure = calculatePressure();
    Serial.print(": Pressure = ");
    Serial.print(pressure);
    Serial.print(" kPa ");
    Serial.print(": Depth = ");
    float depth = calculateDepth ();
    Serial.print(depth);
    Serial.println(" m ");
}




//****** Depth sensor reading **************************************************
float measureVoltage ()
{
  int analogPressure=analogRead(pressureDepthPin);
  float voltage = (analogPressure*VCC/1024.000);
  return voltage;
}

float calculatePressure ()
{
   int analogPressure=analogRead(pressureDepthPin);
   //analogPressure/1024 to give volts
   float voltage = (analogPressure*VCC/1024.000);
   float pressureReading=(0.04+(voltage/VCC))*250;
   return pressureReading;
}

float calculateDepth ()
{
   int analogPressure=analogRead(pressureDepthPin);
   //analogPressure/1024 to give volts
   float voltage = (analogPressure*VCC/1024.000);
   float pressureReading=(0.04+(voltage/VCC))*250;
   float depthCalc = (pressureReading/100.00)-1.00;
   return depthCalc;
}

No comments:

Post a Comment