![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Basic pH read program for HAOSHI H-101 pH probe Still needs calibrating requires only 5V and ADC pin
Diff: main.cpp
- Revision:
- 0:4494f6ceb276
- Child:
- 1:ab2a9277c379
diff -r 000000000000 -r 4494f6ceb276 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Apr 08 13:05:19 2019 +0000 @@ -0,0 +1,50 @@ +#include "mbed.h" +#include "Map.hpp" + +# + +DigitalOut led_green(LED1); +DigitalOut led_red(LED2); + +InterruptIn user_button(USER_BUTTON); +AnalogIn ph (PB_0); +Serial pc (USBTX, USBRX); + +float buf[10], temp; +float avg; +float phval; + +Map map (0, 1.0f, 0, 14); + +int main() +{ + pc.baud(9600); + + while (1) { + + for (int i = 0; i < 10; i++) { //read values from sensor + buf[i] = ph.read(); + //pc.printf("read %.2f\n\r", buf[i]); + wait(0.1); + }// end for 1 + for (int i = 0; i < 9;i++) { // sort values from smallest to lowest + for(int j = i+1; j < 10; j++) { + if (buf[i] > buf[j]) { + temp = buf[i]; // shift data + buf[i] = buf[j]; + buf[j] = temp; + + } // end if 3 + } //end for 2 + }//end for 1 + + avg = 0; + for (int i = 2; i < 8; i++) { // average 6 centre values + avg +=buf[i]; + }// end for 1 + avg = avg/6; + pc.printf("avg = %.2f\n\r", avg); + phval = map.Calculate(avg); + pc.printf("pH = %.2f\n\r", phval); + }// end while 1 +}// end main