Monday, June 29, 2015

Salam Otomasi...


Kali ini saya akan menjelaskan bagaimana cara akses data sensor PH keluaran dari df robot. pada dasarnya semua sensor ph umumnya outputnya berupa tegangan analog. sehingga dapat langsung dibaca melalui pin ADC (Analog to Digital Converter) pada fasilitas mikrokontroler. pada percobaan kali ini mikrokontroler yang saya gunakan adalah Arduino.

Berikut adalah listing program untuk proses pembacaan sensor pH pada arduino.

#include <LiquidCrystal.h>
#define SensorPin A0            //pH meter Analog output to Arduino Analog Input 0
#define Offset 0.00            //deviation compensate
#define LED 13
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth  40    //times of collection
int pHArray[ArrayLenth];   //Store the average value of the sensor feedback
int pHArrayIndex=0;  
LiquidCrystal lcd(53, 51, 49, 47, 45, 43);
void setup(void)
{
   
  lcd.begin(16, 2);
  lcd.print("ShipDroid");
  pinMode(LED,OUTPUT);  
  Serial.begin(9600);  
  delay(1000);
  lcd.clear();
}
void loop(void)
{
  digitalWrite(7,LOW);
  static unsigned long samplingTime = millis();
  static unsigned long printTime = millis();
  static float pHValue,voltage;
  if(millis()-samplingTime > samplingInterval)
  {
      pHArray[pHArrayIndex++]=analogRead(SensorPin);
      if(pHArrayIndex==ArrayLenth)pHArrayIndex=0;
      voltage = avergearray(pHArray, ArrayLenth)*5.0/1024;
      pHValue = 3.5*voltage+Offset;
      samplingTime=millis();
  }
  if(millis() - printTime > printInterval)   //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
  {
        lcd.setCursor(0, 0);
        lcd.print("GPS=-7||112");
        lcd.setCursor(0, 1);
        lcd.print("Nilai pH= ");
        lcd.print(pHValue,2);
  Serial.print("Voltage:");
        Serial.print(voltage,2);
        Serial.print("    pH value: ");
Serial.println(pHValue,2);
        digitalWrite(LED,digitalRead(LED)^1);
        printTime=millis();
  }
}
double avergearray(int* arr, int number){
  int i;
  int max,min;
  double avg;
  long amount=0;
  if(number<=0){
    Serial.println("Error number for the array to avraging!/n");
    return 0;
  }
  if(number<5){   //less than 5, calculated directly statistics
    for(i=0;i<number;i++){
      amount+=arr[i];
    }
    avg = amount/number;
    return avg;
  }else{
    if(arr[0]<arr[1]){
      min = arr[0];max=arr[1];
    }
    else{
      min=arr[1];max=arr[0];
    }
    for(i=2;i<number;i++){
      if(arr[i]<min){
        amount+=min;        //arr<min
        min=arr[i];
      }else {
        if(arr[i]>max){
          amount+=max;    //arr>max
          max=arr[i];
        }else{
          amount+=arr[i]; //min<=arr<=max
        }
      }//if
    }//for
    avg = (double)amount/(number-2);
  }//if
  return avg;
}

Jika teman - teman otomasi ingin download source codenya secara langsung dapat diwnload di sini

terimakasih semoga bermanfaat salam otomasi

1 komentar:

  1. mas ada progam dengan bascom avr enggak ? terima kasih

    ReplyDelete