Program for the water play project for the course Software Testing Practical 2016 given at the VU University

Dependencies:   mbed DRV88255 TextLCD Ping mbed-rtos

SalinityController.cpp

Committer:
sbouber1
Date:
2016-06-10
Revision:
8:24b057cbcb53
Parent:
6:067e999b9c6e

File content as of revision 8:24b057cbcb53:

#include "SalinityController.h"

AnalogIn salinity_sensor(p19);

SalinityController::SalinityController()
{
    printf("Initiate Salinity Controller\r\n");
}

// Function reads sensor values and averages N sensor values
float SalinityController::readSensor()
{
    float analogin_value = 0;
    float ppt = 0;
    
    Timer t;
    
    t.start();
    
    // Read 0-1.0 value
    for(int i = 0; i < N; i++)
    {
        analogin_value += salinity_sensor.read();
    }
    
    t.stop();
    
    printf("We did %d averages of salinity in %d ms\n", N, t.read_ms());
    
    // Average N sensor values
    analogin_value /= (float) N;
    
    ppt = analoginToPPT(analogin_value);
    
    return ppt;
}

// Function converts average sensor value to ppt
float SalinityController::analoginToPPT(float analogin_value)
{
    float voltage = 0;
    float ppt = 0;
    
    // Convert average sensor value to value with range 0-5.0v
    voltage = analogin_value * 3.3f * (5.0f/3.0f);
    
  //  printf("Sensor Voltage: %f \t", voltage);
    
    // Convert voltage to ppt
//    ppt = voltage * 16.3;
    ppt = voltage * 15.1747217178 - 2.89491343498;
    
    return ppt;
}

float SalinityController::getPPT()
{
    float ppt = 0;
    
    ppt = readSensor();
    
    return ppt;
}

float SalinityController::getVoltage()
{
    float voltage = 0;
    
    float analogin_value = 0;
    float ppt = 0;
    
    // Read 0-1.0 value
    for(int i = 0; i < N; i++)
    {
        analogin_value += salinity_sensor.read();
    }
    // Average 10 sensor values
    analogin_value /= (float) N;    
    voltage = analogin_value * 3.3f * (5.0f/3.0f);
    
    return voltage;
}

// Function prints salinity sensor value in ppt
void SalinityController::displayPPT()
{
    float ppt = 0;
    
    ppt = readSensor();
    
    printf("Salinity: %f ppt\t", ppt);
}