/* # This sample codes is for testing the ORP meter V1.0. # Editor : YouYou # Date : 2013.11.26 # Product: ORP meter # SKU : SEN0165 */ #define VOLTAGE 5.00 //system voltage #define OFFSET -1122 //zero drift voltage #define LED LED_BUILTIN //operating instructions double orpValue; #define ArrayLenth 40 //times of collection #define orpPin 0 //orp meter output,connect to Arduino controller ADC pin int orpArray[ArrayLenth]; int orpArrayIndex=0; double avergearray(int* arr, int number){ int i; int max,min; double avg; long amount=0; if(number<=0){ printf("Error number for the array to avraging!/n"); return 0; } if(number<5){ //less than 5, calculated directly statistics for(i=0;imax){ amount+=max; //arr>max max=arr[i]; }else{ amount+=arr[i]; //min<=arr<=max } }//if }//for avg = (double)amount/(number-2); }//if return avg; } void setup(void) { Serial.begin(9600); pinMode(LED,OUTPUT); analogReadResolution(10); // Sett oppløsningen til AD-konverteren 12 bit } void loop(void) { static unsigned long orpTimer=millis(); //analog sampling interval static unsigned long printTime=millis(); if(millis() >= orpTimer) { orpTimer=millis()+20; orpArray[orpArrayIndex++]=analogRead(orpPin); //read an analog value every 20ms if (orpArrayIndex==ArrayLenth) { orpArrayIndex=0; } orpValue=((30*(double)VOLTAGE*1000)-(75*avergearray(orpArray, ArrayLenth)*VOLTAGE*1000/1024))/75-OFFSET; //convert the analog value to orp according the circuit } if(millis() >= printTime) //Every 800 milliseconds, print a numerical, convert the state of the LED indicator { printTime=millis()+800; Serial.print("ORP: "); Serial.print((int)orpValue); Serial.println("mV"); digitalWrite(LED,1-digitalRead(LED)); } }