![](/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@0:4494f6ceb276, 2019-04-08 (annotated)
- Committer:
- dnonoo
- Date:
- Mon Apr 08 13:05:19 2019 +0000
- Revision:
- 0:4494f6ceb276
- Child:
- 1:ab2a9277c379
basic pH program on DISCO-1 429ZI; Requires only 5V and ADC in pin; portable to any board
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 | |
dnonoo | 0:4494f6ceb276 | 6 | DigitalOut led_green(LED1); |
dnonoo | 0:4494f6ceb276 | 7 | DigitalOut led_red(LED2); |
dnonoo | 0:4494f6ceb276 | 8 | |
dnonoo | 0:4494f6ceb276 | 9 | InterruptIn user_button(USER_BUTTON); |
dnonoo | 0:4494f6ceb276 | 10 | AnalogIn ph (PB_0); |
dnonoo | 0:4494f6ceb276 | 11 | Serial pc (USBTX, USBRX); |
dnonoo | 0:4494f6ceb276 | 12 | |
dnonoo | 0:4494f6ceb276 | 13 | float buf[10], temp; |
dnonoo | 0:4494f6ceb276 | 14 | float avg; |
dnonoo | 0:4494f6ceb276 | 15 | float phval; |
dnonoo | 0:4494f6ceb276 | 16 | |
dnonoo | 0:4494f6ceb276 | 17 | Map map (0, 1.0f, 0, 14); |
dnonoo | 0:4494f6ceb276 | 18 | |
dnonoo | 0:4494f6ceb276 | 19 | int main() |
dnonoo | 0:4494f6ceb276 | 20 | { |
dnonoo | 0:4494f6ceb276 | 21 | pc.baud(9600); |
dnonoo | 0:4494f6ceb276 | 22 | |
dnonoo | 0:4494f6ceb276 | 23 | while (1) { |
dnonoo | 0:4494f6ceb276 | 24 | |
dnonoo | 0:4494f6ceb276 | 25 | for (int i = 0; i < 10; i++) { //read values from sensor |
dnonoo | 0:4494f6ceb276 | 26 | buf[i] = ph.read(); |
dnonoo | 0:4494f6ceb276 | 27 | //pc.printf("read %.2f\n\r", buf[i]); |
dnonoo | 0:4494f6ceb276 | 28 | wait(0.1); |
dnonoo | 0:4494f6ceb276 | 29 | }// end for 1 |
dnonoo | 0:4494f6ceb276 | 30 | for (int i = 0; i < 9;i++) { // sort values from smallest to lowest |
dnonoo | 0:4494f6ceb276 | 31 | for(int j = i+1; j < 10; j++) { |
dnonoo | 0:4494f6ceb276 | 32 | if (buf[i] > buf[j]) { |
dnonoo | 0:4494f6ceb276 | 33 | temp = buf[i]; // shift data |
dnonoo | 0:4494f6ceb276 | 34 | buf[i] = buf[j]; |
dnonoo | 0:4494f6ceb276 | 35 | buf[j] = temp; |
dnonoo | 0:4494f6ceb276 | 36 | |
dnonoo | 0:4494f6ceb276 | 37 | } // end if 3 |
dnonoo | 0:4494f6ceb276 | 38 | } //end for 2 |
dnonoo | 0:4494f6ceb276 | 39 | }//end for 1 |
dnonoo | 0:4494f6ceb276 | 40 | |
dnonoo | 0:4494f6ceb276 | 41 | avg = 0; |
dnonoo | 0:4494f6ceb276 | 42 | for (int i = 2; i < 8; i++) { // average 6 centre values |
dnonoo | 0:4494f6ceb276 | 43 | avg +=buf[i]; |
dnonoo | 0:4494f6ceb276 | 44 | }// end for 1 |
dnonoo | 0:4494f6ceb276 | 45 | avg = avg/6; |
dnonoo | 0:4494f6ceb276 | 46 | pc.printf("avg = %.2f\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 |