works
Dependencies: BLE_nRF8001 DebounceIn mbed mbed_BLEtry2
Fork of mbed_BLEfinal by
Diff: main.cpp
- Revision:
- 4:980b93c90fb7
- Parent:
- 2:f4cedbb71ef9
- Child:
- 5:7a4a5fbe72ac
diff -r 4ee7b58c5e8e -r 980b93c90fb7 main.cpp --- a/main.cpp Fri Apr 10 02:43:51 2015 +0000 +++ b/main.cpp Tue Apr 14 23:13:16 2015 +0000 @@ -9,6 +9,7 @@ AnalogIn pH_sensor(A0); InterruptIn button(D2); DigitalOut led(LED_RED); +AnalogIn temp_sensor(A1); // The SPI construct, REQN and RDYN IO construct should be modified manually // It depend on the board you are using and the REQN&RDYN configuration on BLE Shield @@ -41,33 +42,50 @@ unsigned char count_on = 0; //for pH measurement -int calib = 0; -int avgReading = 0; +float calib = 0; +float calib2 = 0; +float avgReading = 0; +float avgTempReading = 0; bool calibrationORnot = true; bool ready = false; -int* reading[] = {0,0}; +//int* reading[] = {0,0}; //This loop takes a reading by taking 15 measurements and averaging them when it is called in the loop "measure" -int takeReading() +float takepHReading() { - double totalVal = 0.0; - double pH_sensor_read = 0.0; + float totalVal = 0.0; + float pH_sensor_read = 0.0; int totalReadings = 0; while(totalReadings < 16){ pH_sensor_read = pH_sensor.read(); totalVal = totalVal + pH_sensor_read; totalReadings++; } - int avgSensor = totalVal/15; + float avgSensor = totalVal/totalReadings; return avgSensor; } + + float takeTempReading() +{ + float totalTempVal = 0.0; + float temp_sensor_read = 0.0; + int totalReadings2 = 0; + while(totalReadings2 < 30){ + temp_sensor_read = temp_sensor.read(); + totalTempVal = totalTempVal + temp_sensor_read; + totalReadings2++; + } + float avgTempSensor = totalTempVal/totalReadings2; + return avgTempSensor; + } void calibration() { - //serial.printf("enter calibration loop\n"); +//serial.printf("enter calibration loop\n"); //Loop to take calibration value - calib = takeReading(); + calib = takepHReading(); + calib2 = takeTempReading(); //serial.printf("calib is %d\n", calib); } @@ -75,7 +93,8 @@ { //serial.printf("entered calc\n"); //Take average of readings over 10 seconds - avgReading = takeReading(); + avgReading = takepHReading(); + avgTempReading = takeTempReading(); //serial.printf("took reading\n"); ready = true; } @@ -140,8 +159,11 @@ //serial.printf("yint is %f", yint); //assumes linear relationship between pin readout -> voltage ->pH pH = (0.0209*3300)*(avgReading)-yint; + serial.printf("avgreading = %f & yint = %f F & calib = %f\n\r", avgReading, yint, calib); + + serial.printf("pH = %f\n\r", pH); //conversion to degrees C from sensor output voltage - tempC = (((3000*avgReading/1000) - 1.022129)/-0.0018496); + tempC = (((3000*avgTempReading/1000) - 1.022129)/-0.0018496); tempF = (tempC *9/5) + 32; //print current pH and temp serial.printf("pH = %f & temp = %f F\n\r", pH, tempF);