bangnaga

PH Meter & Kekeruhan Air

Aug 14th, 2016 (edited)
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. /*
  2.  # This sample code is used to test the pH meter V1.0.
  3.  # Editor : YouYou
  4.  # Ver    : 1.0
  5.  # Product: analog pH meter
  6.  # SKU    : SEN0161
  7. */
  8. #define SensorPin A0            //pH meter Analog output to Arduino Analog Input 0
  9. #define LDR_Pin A1            //pH meter Analog output to Arduino Analog Input 0
  10.  
  11.  
  12. #define Offset 0.00            //deviation compensate
  13. #define LED 13
  14. #define samplingInterval 20
  15. #define printInterval 800
  16. #define ArrayLenth  40    //times of collection
  17. int pHArray[ArrayLenth];   //Store the average value of the sensor feedback
  18. int pHArrayIndex=0;    
  19. void setup(void)
  20. {
  21.   pinMode(LED,OUTPUT);  
  22.   Serial.begin(9600);  
  23. }
  24. void loop(void)
  25. {
  26.   static unsigned long samplingTime = millis();
  27.   static unsigned long printTime = millis();
  28.   static float pHValue,voltage;
  29.   if(millis()-samplingTime > samplingInterval)
  30.   {
  31.       pHArray[pHArrayIndex++]=analogRead(SensorPin);
  32.       if(pHArrayIndex==ArrayLenth)pHArrayIndex=0;
  33.       voltage = avergearray(pHArray, ArrayLenth)*5.0/1024;
  34.       pHValue = 3.5*voltage+Offset;
  35.       samplingTime=millis();
  36.   }
  37.   if(millis() - printTime > printInterval)   //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
  38.   {
  39.     int LDRReading = analogRead(LDR_Pin);
  40.  
  41.       Serial.print("*Volt_pH: ");
  42.       Serial.print(voltage,2);
  43.       Serial.print("*  Nilai_pH: ");
  44.       Serial.print(pHValue,2);
  45.       Serial.print("*  Analog_Kekeruhan: ");
  46.       Serial.print(LDRReading);
  47.       Serial.println("*");
  48.         digitalWrite(LED,digitalRead(LED)^1);
  49.         printTime=millis();
  50.   }
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. }
  59. double avergearray(int* arr, int number){
  60.   int i;
  61.   int max,min;
  62.   double avg;
  63.   long amount=0;
  64.   if(number<=0){
  65.     //Serial.println("Error number for the array to avraging!/n");
  66.     return 0;
  67.   }
  68.   if(number<5){   //less than 5, calculated directly statistics
  69.     for(i=0;i<number;i++){
  70.       amount+=arr[i];
  71.     }
  72.     avg = amount/number;
  73.     return avg;
  74.   }else{
  75.     if(arr[0]<arr[1]){
  76.       min = arr[0];max=arr[1];
  77.     }
  78.     else{
  79.       min=arr[1];max=arr[0];
  80.     }
  81.     for(i=2;i<number;i++){
  82.       if(arr[i]<min){
  83.         amount+=min;        //arr<min
  84.         min=arr[i];
  85.       }else {
  86.         if(arr[i]>max){
  87.           amount+=max;    //arr>max
  88.           max=arr[i];
  89.         }else{
  90.           amount+=arr[i]; //min<=arr<=max
  91.         }
  92.       }//if
  93.     }//for
  94.     avg = (double)amount/(number-2);
  95.   }//if
  96.   return avg;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment