0

我输入了所需的代码,但串行监视器中没有显示 pH 值。我单独尝试了 eC 和 pH 值,没有温度,它起作用了。当我向其中添加温度时,出现了温度和 eC,但缺少 pH 值。该代码没有警告任何错误,但似乎是因为串行监视器缺少一个参数。

#include <DFRobot_PH.h>


//TEMP
// Include the libraries we need
#include <OneWire.h>

// #include <DallasTemperature.h>

int DS18S20_Pin = 7; //DS18S20 Signal pin on digital 2

//Temperature chip i/o
OneWire ds(DS18S20_Pin);  // on digital pin 2

//pH

#define SensorPin 0          // the pH meter Analog output is connected with the Arduino’s Analog
unsigned long int avgValue;  //Store  the average value of the sensor feedback
float b;
int buf[10], temp;

//EC

#include "DFRobot_EC.h"
#include <EEPROM.h>

#define EC_PIN A1
float voltage, ecValue, temperature = 25;
DFRobot_EC ec;

void setup()
{ 

  //PH
  pinMode(13, OUTPUT);
  //RELAY
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);

  //EC
  Serial.begin(115200);
  ec.begin();

  //TEMP

}
void loop()
{

  { //ECSENSOR

    static unsigned long timepoint = millis();
    if (millis() - timepoint > 1000U) //time interval: 1s
    {
      timepoint = millis();
      voltage = analogRead(EC_PIN) / 1024.0 * 5000; // read the voltage
      temperature = getTemp();          // read your temperature sensor to execute temperature compensation
      ecValue =  ec.readEC(voltage, temperature); // convert voltage to EC with temperature compensation
      Serial.print("temperature:");
      Serial.print(temperature, 1);
      Serial.print("^C  EC:");
      Serial.print(ecValue, 2);
      Serial.println("ms/cm");
    }
    ec.calibration(voltage, temperature);         // calibration process by Serail CMD
  }
  //TEMP
  
float temperature = getTemp();
  delay(800); //just here to slow down the output so it is easier to read

}


float getTemp(){
  //returns the temperature from one DS18S20 in DEG Celsius

  byte data[12];
  byte addr[8];

  if ( !ds.search(addr)) {
      //no more sensors on chain, reset search
      ds.reset_search();
      return -1000;
  }

  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return -1000;
  }

  if ( addr[0] != 0x10 && addr[0] != 0x28) {
      Serial.print("Device is not recognized");
      return -1000;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end

  byte present = ds.reset();
  ds.select(addr);
  ds.write(0xBE); // Read Scratchpad


  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
  }

  ds.reset_search();

  byte MSB = data[1];
  byte LSB = data[0];

  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;

  return TemperatureSum;


//PHSENSOR
  for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  { 
    buf[i]=analogRead(SensorPin);
    delay(10);
  }
  for(int i=0;i<9;i++)        //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
    {
      if(buf[i]>buf[j])
      {
        temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  
avgValue = 0;
for (int i = 2; i < 8; i++)               //take the average value of 6 center sample
  avgValue += buf[i];
float phValue = (float)avgValue * 5.0 / 1024 / 6; //convert the analog into millivolt
phValue = 3.5 * phValue;                  //convert the millivolt into pH value
Serial.print("    pH:");
Serial.print(phValue, 2);
Serial.println(" ");
delay(1000);
digitalWrite(13, LOW);
delay(800);
digitalWrite(13, HIGH);


}}
      ...
4

0 回答 0