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@4:4011a1562a77, 2016-06-08 (annotated)
- Committer:
- 6366295
- Date:
- Wed Jun 08 13:16:32 2016 +0000
- Revision:
- 4:4011a1562a77
- Parent:
- 1:eb527bc93b62
- Child:
- 6:067e999b9c6e
Changed ppt formula
Who changed what in which revision?
User | Revision | Line number | New 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 | |
6366295 | 0:dab140a197e0 | 16 | // Read 0-1.0 value |
6366295 | 0:dab140a197e0 | 17 | for(int i = 0; i < N; i++) |
6366295 | 0:dab140a197e0 | 18 | { |
6366295 | 0:dab140a197e0 | 19 | analogin_value += salinity_sensor.read(); |
6366295 | 0:dab140a197e0 | 20 | } |
6366295 | 0:dab140a197e0 | 21 | // Average 10 sensor values |
6366295 | 0:dab140a197e0 | 22 | analogin_value /= (float) N; |
6366295 | 0:dab140a197e0 | 23 | |
6366295 | 0:dab140a197e0 | 24 | ppt = analoginToPPT(analogin_value); |
6366295 | 0:dab140a197e0 | 25 | |
6366295 | 0:dab140a197e0 | 26 | return ppt; |
6366295 | 0:dab140a197e0 | 27 | } |
6366295 | 0:dab140a197e0 | 28 | |
6366295 | 0:dab140a197e0 | 29 | // Function converts average sensor value to ppt |
6366295 | 0:dab140a197e0 | 30 | float SalinityController::analoginToPPT(float analogin_value) |
6366295 | 0:dab140a197e0 | 31 | { |
6366295 | 0:dab140a197e0 | 32 | float voltage = 0; |
6366295 | 0:dab140a197e0 | 33 | float ppt = 0; |
6366295 | 0:dab140a197e0 | 34 | |
6366295 | 0:dab140a197e0 | 35 | // Convert average sensor value to value with range 0-5.0v |
6366295 | 0:dab140a197e0 | 36 | voltage = analogin_value * 3.3f * (5.0f/3.0f); |
6366295 | 0:dab140a197e0 | 37 | |
6366295 | 1:eb527bc93b62 | 38 | printf("Sensor Voltage: %f \t", voltage); |
6366295 | 1:eb527bc93b62 | 39 | |
6366295 | 0:dab140a197e0 | 40 | // Convert voltage to ppt |
6366295 | 4:4011a1562a77 | 41 | // ppt = voltage * 16.3; |
6366295 | 4:4011a1562a77 | 42 | ppt = voltage * 15.1747217178 - 2.89491343498; |
6366295 | 0:dab140a197e0 | 43 | |
6366295 | 0:dab140a197e0 | 44 | return ppt; |
6366295 | 0:dab140a197e0 | 45 | } |
6366295 | 0:dab140a197e0 | 46 | |
6366295 | 0:dab140a197e0 | 47 | // Function prints salinity sensor value in ppt |
6366295 | 0:dab140a197e0 | 48 | void SalinityController::displayPPT() |
6366295 | 0:dab140a197e0 | 49 | { |
6366295 | 0:dab140a197e0 | 50 | float ppt = 0; |
6366295 | 0:dab140a197e0 | 51 | |
6366295 | 0:dab140a197e0 | 52 | ppt = readSensor(); |
6366295 | 0:dab140a197e0 | 53 | |
6366295 | 0:dab140a197e0 | 54 | printf("Salinity: %f ppt\t", ppt); |
6366295 | 0:dab140a197e0 | 55 | } |