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

Committer:
sbouber1
Date:
Fri Jun 10 12:38:54 2016 +0000
Revision:
8:24b057cbcb53
Parent:
6:067e999b9c6e
Started threading stuff

Who changed what in which revision?

UserRevisionLine numberNew contents of line
6366295 0:dab140a197e0 1 #include "SalinityController.h"
6366295 0:dab140a197e0 2
6366295 0:dab140a197e0 3 AnalogIn salinity_sensor(p19);
6366295 0:dab140a197e0 4
6366295 0:dab140a197e0 5 SalinityController::SalinityController()
6366295 0:dab140a197e0 6 {
6366295 0:dab140a197e0 7 printf("Initiate Salinity Controller\r\n");
6366295 0:dab140a197e0 8 }
6366295 0:dab140a197e0 9
6366295 0:dab140a197e0 10 // Function reads sensor values and averages N sensor values
6366295 0:dab140a197e0 11 float SalinityController::readSensor()
6366295 0:dab140a197e0 12 {
6366295 0:dab140a197e0 13 float analogin_value = 0;
6366295 0:dab140a197e0 14 float ppt = 0;
6366295 0:dab140a197e0 15
sbouber1 8:24b057cbcb53 16 Timer t;
sbouber1 8:24b057cbcb53 17
sbouber1 8:24b057cbcb53 18 t.start();
sbouber1 8:24b057cbcb53 19
6366295 0:dab140a197e0 20 // Read 0-1.0 value
6366295 0:dab140a197e0 21 for(int i = 0; i < N; i++)
6366295 0:dab140a197e0 22 {
6366295 0:dab140a197e0 23 analogin_value += salinity_sensor.read();
6366295 0:dab140a197e0 24 }
sbouber1 8:24b057cbcb53 25
sbouber1 8:24b057cbcb53 26 t.stop();
sbouber1 8:24b057cbcb53 27
sbouber1 8:24b057cbcb53 28 printf("We did %d averages of salinity in %d ms\n", N, t.read_ms());
sbouber1 8:24b057cbcb53 29
sbouber1 8:24b057cbcb53 30 // Average N sensor values
6366295 0:dab140a197e0 31 analogin_value /= (float) N;
6366295 0:dab140a197e0 32
6366295 0:dab140a197e0 33 ppt = analoginToPPT(analogin_value);
6366295 0:dab140a197e0 34
6366295 0:dab140a197e0 35 return ppt;
6366295 0:dab140a197e0 36 }
6366295 0:dab140a197e0 37
6366295 0:dab140a197e0 38 // Function converts average sensor value to ppt
6366295 0:dab140a197e0 39 float SalinityController::analoginToPPT(float analogin_value)
6366295 0:dab140a197e0 40 {
6366295 0:dab140a197e0 41 float voltage = 0;
6366295 0:dab140a197e0 42 float ppt = 0;
6366295 0:dab140a197e0 43
6366295 0:dab140a197e0 44 // Convert average sensor value to value with range 0-5.0v
6366295 0:dab140a197e0 45 voltage = analogin_value * 3.3f * (5.0f/3.0f);
6366295 0:dab140a197e0 46
joran 6:067e999b9c6e 47 // printf("Sensor Voltage: %f \t", voltage);
6366295 1:eb527bc93b62 48
6366295 0:dab140a197e0 49 // Convert voltage to ppt
6366295 4:4011a1562a77 50 // ppt = voltage * 16.3;
6366295 4:4011a1562a77 51 ppt = voltage * 15.1747217178 - 2.89491343498;
6366295 0:dab140a197e0 52
6366295 0:dab140a197e0 53 return ppt;
6366295 0:dab140a197e0 54 }
6366295 0:dab140a197e0 55
joran 6:067e999b9c6e 56 float SalinityController::getPPT()
joran 6:067e999b9c6e 57 {
joran 6:067e999b9c6e 58 float ppt = 0;
joran 6:067e999b9c6e 59
joran 6:067e999b9c6e 60 ppt = readSensor();
joran 6:067e999b9c6e 61
joran 6:067e999b9c6e 62 return ppt;
joran 6:067e999b9c6e 63 }
joran 6:067e999b9c6e 64
joran 6:067e999b9c6e 65 float SalinityController::getVoltage()
joran 6:067e999b9c6e 66 {
joran 6:067e999b9c6e 67 float voltage = 0;
joran 6:067e999b9c6e 68
joran 6:067e999b9c6e 69 float analogin_value = 0;
joran 6:067e999b9c6e 70 float ppt = 0;
joran 6:067e999b9c6e 71
joran 6:067e999b9c6e 72 // Read 0-1.0 value
joran 6:067e999b9c6e 73 for(int i = 0; i < N; i++)
joran 6:067e999b9c6e 74 {
joran 6:067e999b9c6e 75 analogin_value += salinity_sensor.read();
joran 6:067e999b9c6e 76 }
joran 6:067e999b9c6e 77 // Average 10 sensor values
joran 6:067e999b9c6e 78 analogin_value /= (float) N;
joran 6:067e999b9c6e 79 voltage = analogin_value * 3.3f * (5.0f/3.0f);
joran 6:067e999b9c6e 80
joran 6:067e999b9c6e 81 return voltage;
joran 6:067e999b9c6e 82 }
joran 6:067e999b9c6e 83
6366295 0:dab140a197e0 84 // Function prints salinity sensor value in ppt
6366295 0:dab140a197e0 85 void SalinityController::displayPPT()
6366295 0:dab140a197e0 86 {
6366295 0:dab140a197e0 87 float ppt = 0;
6366295 0:dab140a197e0 88
6366295 0:dab140a197e0 89 ppt = readSensor();
6366295 0:dab140a197e0 90
6366295 0:dab140a197e0 91 printf("Salinity: %f ppt\t", ppt);
6366295 0:dab140a197e0 92 }