arduino²âÊÔ
//#include <LiquidCrystal.h>#include <EEPROM.h>
//The time between each EEPROM write function call in ms
#define SAMPLE_TIME 2000
const int analogInPin = A0;
int sensorValue = 0;
int sensorValue1 = 0;
const int turbPin=A1;
unsigned long int avgValue;
float b;
int buf,temp;
float min = 1000000000000;
float max = 0;
boolean varSet = false;
float volt;
float ntu;
void setup(void) {
Serial.begin(9600);
Serial.print(" pH, ");
Serial.print("Max, ");
Serial.print("Min, ");
Serial.print("Avg, ");
Serial.println("Turbidity");
}
void loop(void) {
for(int i=0;i<10;i++)
{
buf=analogRead(analogInPin);
delay(100);
}
for(int i=0;i<9;i++)
{
for(int j=i+1;j<10;j++)
{
if(buf>buf)
{
temp=buf;
buf=buf;
buf=temp;
}
}
}
avgValue=0;
float pHAverage = 0;
for(int i=2;i<8;i++)
avgValue+=buf;
float pHVol=(float)avgValue*5.0/1024/6;
float pHValue = -5.70 * pHVol + 24.34;
pHAverage += pHValue;
if(min > pHValue){
min = pHValue;
}
if(max < pHValue){
max = pHValue;
}
/*Serial.print("pH ");
Serial.print("Maximum pH");
Serial.print("Minimum pH");
Serial.println("Average pH");*/
Serial.print(pHValue);
Serial.print(", ");
//print max and min pH levels overall
//Serial.print("Highest pH = ");
Serial.print(max);
Serial.print(", ");
//Serial.print("Lowest pH = ");
Serial.print(min);
Serial.print(", ");
//prints the average pH
//Serial.print("Average pH: ");
Serial.print(pHAverage);
Serial.print(", ");
//message to interpret pH level
//if(pHAverage > 4 && pHAverage < 9) {
//Serial.println("Water levels are normal!");}
//else if (pHAverage <= 4) {
//Serial.println("Water is too acidic!");
//}
//else if (pHAverage >= 9) {
//Serial.println("Water is too basic!");
//}
delay(3000);
float sensorValue1 = analogRead(turbPin);
/*volt = 0;
ntu = 0;
for(int i=0; i<800; i++)
{
volt += ((float)analogRead(turbPin)/1023)*5;
}
volt = volt/800;
volt = round_to_dp(volt,1);
volt = 4;
if(volt < 2.5){
ntu = 3000;
}else{
ntu = -1120.4*square(volt)+5742.3*volt-4353.8;
}*/
//Serial.print("Turbidity: ");
Serial.println(sensorValue1 / 500);
//Serial.println();
delay (3000);
}//end loop
float round_to_dp( float in_value, int decimal_place )
{
float multiplier = powf( 10.0f, decimal_place );
in_value = roundf( in_value * multiplier ) / multiplier;
return in_value;
}
Ò³:
[1]