1.Flowhart
Coding
|
Pen jelasan
|
#define SensorPin A0
#include <Servo.h>
#define Offset -3.00
#define LED 13
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth 40
|
//pH meter Analog output to
Arduino Analog Input 0
//deviation compensate
//times of collection
|
Servo myservo;
Servo myservo2;
|
//inisialisasi untuk variabel
motor servo 1 dan 2
|
int iOldPos, iNewPos = 0;
int pos = 0;
int pHArray[ArrayLenth];
int pHArrayIndex = 0;
long previousMillis = 0;
const int interval = 20;
long intervalmuter = 1000;
long intervalmuter2 = 1200;
|
//Store the average value of the
sensor feedback
|
void setup(void)
{
pinMode(LED, OUTPUT);
Serial.begin(9600);
Serial.println("pH meter experiment!"); myservo.attach(9);
myservo2.attach(10);
}
|
//Test the serial monitor
|
void loop(void)
{
static unsigned long samplingTime = millis();
static unsigned long printTime = millis();
static unsigned long muterTime = millis();
static unsigned long muterTime2 = 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 = 2.8 * voltage + Offset;
samplingTime = millis();
}
|
|
if (millis() - printTime > printInterval) //Every 800 milliseconds,
print a numerical, convert the state of the LED indicator
{
Serial.print("Voltage:");
Serial.print(voltage, 2);
Serial.print(" pH value: ");
Serial.println(pHValue, 2);
digitalWrite(LED, digitalRead(LED) ^ 1);
printTime = millis();
}
|
|
if(pHValue > 8.10 && millis() - muterTime >
intervalmuter ){
pos = 90;
{
myservo.write(pos);
} muterTime = millis();
|
// start dari 0 derajar sampai
180 derajat
// pada posisi 1 derajat
// memberitahu servo untuk pergi ke posisi 'pos'
// tunggu 15ms untuk
pencapaian posisi servo
|
}if(pHValue < 8.10 &&
millis() - muterTime > intervalmuter){
pos = -90;
{
myservo.write(pos);
} muterTime = millis();
}
|
// start dari 180 derajat ke 0
derajat
// memberitahu servo untuk pergi
ke posisi 'pos'
// tunggu 15ms untuk pencapaian
posisi servo
|
if(pHValue < 6.90 &&
millis() - muterTime2 > intervalmuter2 ){
pos = 90;
{
myservo2.write(pos);
} muterTime2 = millis();
|
// start dari 0 derajar sampai
180 derajat
// pada posisi 1 derajat
// memberitahu servo untuk pergi
ke posisi 'pos'
// tunggu 15ms untuk
pencapaian posisi servo
|
}if(pHValue > 6.90 &&
millis() - muterTime2 > intervalmuter2){
pos = -90;
{
myservo2.write(pos);
} muterTime2 = millis();
}
}
|
// start dari 180 derajat ke 0
derajat
// memberitahu servo untuk pergi
ke posisi 'pos'
// tunggu 15ms untuk
pencapaian posisi servo
|
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) {
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;
min = arr[i];
} else {
if (arr[i] > max) {
amount += max;
max = arr[i];
} else {
amount += arr[i];
}
}//if
}//for
avg = (double)amount / (number-2);
}//if
return avg;
}
|
//less than 5, calculated
directly statistics
//arr<min
//arr>max
//arr<min
//min<=arr<=max
|
Tidak ada komentar:
Posting Komentar