1

im using ph meter DFROBOT v1.0 at i compiled the code from the DFROBOT web this is the code

*
 # This sample code is used to test the pH meter V1.0.
 # Editor : YouYou
 # Ver    : 1.0
 # Product: analog pH meter
 # SKU    : SEN0161
*/
#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;
void setup(void)
{
  pinMode(LED,OUTPUT);
  Serial.begin(9600);
  Serial.println("pH meter experiment!");    //Test the serial monitor
}
void loop(void)
{
  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
  {
    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;
}

and its work, untill i want to combine thats code to my code, the ph value = 0.00 im just copy paste the code from the website in to my code and its not working what should i do

void loop()
{
  //-------------Read Function sensor----------- 
//  PPM---- 
    gravityTds.setTemperature(temperature);  // set the temperature and execute temperature compensation
    gravityTds.update();  //sample and calculate 
    
    tdsValue = gravityTds.getTdsValue();
    tdsValue=(tdsValue *2)/1000;// then get EC the value
    Serial.print(tdsValue,5);
    Serial.println("ppm");

    hasilec = (ppmcensor - inputec );
    Serial.print("Eror EC : ");
    Serial.println(hasilec,6);
    
    
// ph-----
  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();
  }
  Serial.print("Nilai pH :");
  Serial.println(voltage,2);
  
//  ph = (phcensor - inputph);
  
//  hasilec = random(-200, 0) / 100.0;
//  ph = random(-200, 200) / 100.0;
  ph = 130/100.0;


  
  
  unsigned long now = millis();
  
  SerialMon.print("EROOR pH    ");
  SerialMon.print(ph);
  SerialMon.print("  ");
  SerialMon.print("ERORR EC ");
  SerialMon.print(hasilec);
  SerialMon.println("");
  SerialMon.print(ph);  Serial.print(" ");
  SerialMon.print(hasilec);   Serial.print(" ");
  SerialMon.println(" ");
    
  fuzzy->setInput(2, hasilec);
  fuzzy->setInput(1, ph);  
  fuzzy->fuzzify();

  float pompa01 = fuzzy->defuzzify(1);//pHDOWN
  float pompa02 = fuzzy->defuzzify(2);//pHUP
  float pompa03 = fuzzy->defuzzify(3);//ABMIX
  
  SerialMon.println("Result= ");
  SerialMon.println("");
  SerialMon.print(" Pompa1 pHDOWN: ");
  SerialMon.println(pompa01);
  SerialMon.print(" pompa2 phUP  : ");
  SerialMon.println(pompa02);
  SerialMon.print(" pompa3 ABMIX : ");
  SerialMon.println(pompa03);
  SerialMon.println("");
  SerialMon.println("");
  SerialMon.println("");
  
  delay (2000);
  
  long int pompa1 = pompa01*1000;
  long int pompa2 = pompa02*1000;
  long int pompa3 = pompa03*1000;
  
  pump_periods[0] = pompa1; //PHdown
  pump_periods[1] = pompa2; //PHup
  pump_periods[2] = pompa3; //ABMIX
  
//  for (int i = 0; i < jml_pump; i++) {
//      if (now - pump_last_actuations[i] >= pump_periods[i]) {
//          pump_states[i] = (pump_states[i] == LOW) ? HIGH : LOW;

//          digitalWrite(pump_pins[i], pump_states[i]);
//          pump_last_actuations[i] = now;
//      }     
//  }

  for (int i = 0; i < jml_pump; i++) { 
  digitalWrite(pump_pins[i], LOW);     // Start pump i
  delay(pump_periods[i]);            // nyala waktu dalam pompa[i] ms
  digitalWrite(pump_pins[i], HIGH);     // Stop pump i
  }
  delay(100000);   
}
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;
}
4
  • What exactly do you mean by copy&pasting the code into your code? You cannot simply do that, because you can only have 1 void loop() function and 1 void setup() function
    – chrisl
    Jan 20 at 8:01
  • the first code its from wiki.dfrobot.com/… and i add that code to my code
    – daffa faiz
    Jan 20 at 8:03
  • As I wrote, you cannot simply do that. Combining two codes is more complex than that
    – chrisl
    Jan 20 at 8:10
  • yep thx for helping me
    – daffa faiz
    Jan 22 at 8:51