![](/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
main.cpp@1:ab2a9277c379, 2019-04-10 (annotated)
- Committer:
- dnonoo
- Date:
- Wed Apr 10 19:39:48 2019 +0000
- Revision:
- 1:ab2a9277c379
- Parent:
- 0:4494f6ceb276
yes;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dnonoo | 0:4494f6ceb276 | 1 | #include "mbed.h" |
dnonoo | 0:4494f6ceb276 | 2 | #include "Map.hpp" |
dnonoo | 0:4494f6ceb276 | 3 | |
dnonoo | 0:4494f6ceb276 | 4 | |
dnonoo | 0:4494f6ceb276 | 5 | DigitalOut led_green(LED1); |
dnonoo | 0:4494f6ceb276 | 6 | DigitalOut led_red(LED2); |
dnonoo | 0:4494f6ceb276 | 7 | |
dnonoo | 0:4494f6ceb276 | 8 | InterruptIn user_button(USER_BUTTON); |
dnonoo | 0:4494f6ceb276 | 9 | AnalogIn ph (PB_0); |
dnonoo | 0:4494f6ceb276 | 10 | Serial pc (USBTX, USBRX); |
dnonoo | 0:4494f6ceb276 | 11 | |
dnonoo | 0:4494f6ceb276 | 12 | float buf[10], temp; |
dnonoo | 0:4494f6ceb276 | 13 | float avg; |
dnonoo | 0:4494f6ceb276 | 14 | float phval; |
dnonoo | 0:4494f6ceb276 | 15 | |
dnonoo | 1:ab2a9277c379 | 16 | Map map (0, 1.0f, 0, 14.0f); |
dnonoo | 0:4494f6ceb276 | 17 | |
dnonoo | 0:4494f6ceb276 | 18 | int main() |
dnonoo | 0:4494f6ceb276 | 19 | { |
dnonoo | 0:4494f6ceb276 | 20 | pc.baud(9600); |
dnonoo | 0:4494f6ceb276 | 21 | |
dnonoo | 0:4494f6ceb276 | 22 | while (1) { |
dnonoo | 0:4494f6ceb276 | 23 | |
dnonoo | 0:4494f6ceb276 | 24 | for (int i = 0; i < 10; i++) { //read values from sensor |
dnonoo | 0:4494f6ceb276 | 25 | buf[i] = ph.read(); |
dnonoo | 0:4494f6ceb276 | 26 | //pc.printf("read %.2f\n\r", buf[i]); |
dnonoo | 0:4494f6ceb276 | 27 | wait(0.1); |
dnonoo | 0:4494f6ceb276 | 28 | }// end for 1 |
dnonoo | 0:4494f6ceb276 | 29 | for (int i = 0; i < 9;i++) { // sort values from smallest to lowest |
dnonoo | 0:4494f6ceb276 | 30 | for(int j = i+1; j < 10; j++) { |
dnonoo | 0:4494f6ceb276 | 31 | if (buf[i] > buf[j]) { |
dnonoo | 0:4494f6ceb276 | 32 | temp = buf[i]; // shift data |
dnonoo | 0:4494f6ceb276 | 33 | buf[i] = buf[j]; |
dnonoo | 0:4494f6ceb276 | 34 | buf[j] = temp; |
dnonoo | 0:4494f6ceb276 | 35 | |
dnonoo | 0:4494f6ceb276 | 36 | } // end if 3 |
dnonoo | 0:4494f6ceb276 | 37 | } //end for 2 |
dnonoo | 0:4494f6ceb276 | 38 | }//end for 1 |
dnonoo | 0:4494f6ceb276 | 39 | |
dnonoo | 0:4494f6ceb276 | 40 | avg = 0; |
dnonoo | 0:4494f6ceb276 | 41 | for (int i = 2; i < 8; i++) { // average 6 centre values |
dnonoo | 0:4494f6ceb276 | 42 | avg +=buf[i]; |
dnonoo | 0:4494f6ceb276 | 43 | }// end for 1 |
dnonoo | 0:4494f6ceb276 | 44 | avg = avg/6; |
dnonoo | 1:ab2a9277c379 | 45 | avg = avg - 0.15f; |
dnonoo | 1:ab2a9277c379 | 46 | pc.printf("avg = %.6f\n\r", avg); |
dnonoo | 0:4494f6ceb276 | 47 | phval = map.Calculate(avg); |
dnonoo | 0:4494f6ceb276 | 48 | pc.printf("pH = %.2f\n\r", phval); |
dnonoo | 0:4494f6ceb276 | 49 | }// end while 1 |
dnonoo | 0:4494f6ceb276 | 50 | }// end main |