what's wrong about this simple sketch?

Hello Community,
I am trying to create a sketch that is able to read PH and EC values( using a specific device modelA1005, separate 12V plug)and to control them two ,through a 5V module 2relays, for 2 peristaltic pumps ( EC+ to increase EC; pH- to decrease pH value). Besides I desire use an Lcd(20x4)I2C just to report serial monitor:
1)EC>(value)
2)pH>(value)
3)EC+=(relay1 on or off)
4)pH-=(relay2 on or off)

What is wrong about the sketch I attach here?

ps:about lcd , I hope to find some basic lcd i2c library to add, because I just need serial monitor.

Thanks
sketch:

/*

*/

// These constants won’t change. They’re used to give names
// to the pins used:
const int analogInPin = A1; // EC reading
const int analogOutPin = 7; // Analog output pin that the LED is attached to
const int analogInPin1 = A2; // ph reading
const int analogOutPin1 = 8; //analog out
int Relay1 = 10;
int Relay2 = 11;
int sensorValue = 0; // value read from the sensor
int sensorValue1 = 0;
int EC = 0; // value output to the PWM (analog out)
int pH = 0;

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);

}

void loop() {
Serial.println("-------------------");
pinMode(Relay1,OUTPUT);
pinMode(Relay2,OUTPUT);
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
EC= map(sensorValue, 0, 1023, 0, 5000);
// change the analog out value:
analogWrite(analogOutPin, EC);

// read the analog in value:
sensorValue1 = analogRead(analogInPin1);
// map it to the range of the analog out:
pH = map(sensorValue1, 0, 1023, 0, 14);
// change the analog out value:
analogWrite(analogOutPin1, pH);
if (EC < 1000) {
digitalWrite(Relay1, LOW);Serial.println(“EC+=on”);
}else {
digitalWrite (Relay1, HIGH);Serial.println(“EC+=off”);
}
if ( pH > 6,3) {
digitalWrite (Relay2,LOW);Serial.println(“pH-=on”);

}else { digitalWrite (Relay2,HIGH);Serial.println(“pH-=off”);

}
}

// print the results to the serial monitor:

Serial.print("EC > ");
Serial.println(analogRead(1)* 5.00 / 1024, 2);
// print the results to the serial monitor:
Serial.print("pH > ");
Serial.println(analogRead(2)* 14.00 / 1024, 1); note(error when I verify: serial not type)

// wait 10 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(500);
}

diegodice1976:
What is wrong about the sketch I attach here?

uh.... which sketch???

post the code rather if you can; easiest for everyone to view! :slight_smile:

What is wrong about the sketch

What does it do ?
What should it do ?

Why did you not read and follow the advice in Read this before posting a programming question at the top of teh page ?

thanks for posting the code (though it was not in the correct way…)

there was a trailing ‘}’ in you code that was causing the error:

this code now compiles:

const int analogInPin = A1; // EC reading
const int analogOutPin = 7; // Analog output pin that the LED is attached to
const int analogInPin1 = A2; // ph reading
const int analogOutPin1 = 8; //analog out
int Relay1 = 10;
int Relay2 = 11;
int sensorValue = 0; // value read from the sensor
int sensorValue1 = 0;
float EC = 0;
float pH = 0;

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);

}

void loop() {
  Serial.println("-------------------");

  // read the analog in value:
  analogRead(analogInPin);
  sensorValue = analogRead(analogInPin);

  //calculate EC value
  EC = sensorValue * 5.00 / 1024;

  // map it to the range of the analog out:
  analogWrite(analogOutPin, map(EC, 0, 5, 0, 255));

  // read the analog in value:
  analogRead(analogInPin1);
  sensorValue1 = analogRead(analogInPin1);

  //Calculate pH value
  pH = sensorValue1 * 14.00 / 1024;

  // map it to the range of the analog out:
  analogWrite(analogOutPin1, map(pH, 0, 14, 0, 255));

  if (EC < 1.0) {
    digitalWrite(Relay1, LOW); Serial.println("EC+=on");
  }
  else {
    digitalWrite (Relay1, HIGH); Serial.println("EC+=off");
  }

  if ( pH > 6.3) {
    digitalWrite (Relay2, LOW); Serial.println("pH-=on");
  }
  else {
    digitalWrite (Relay2, HIGH); Serial.println("pH-=off");
  }

  // print the results to the serial monitor:

  Serial.print("EC > ");
  Serial.println(EC, 2);
  // print the results to the serial monitor:
  Serial.print("pH > ");
  Serial.println(pH, 1);


  // wait 500 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(500);
}

I do have a comment about this portion of your code:
// read the analog in value:
sensorValue1 = analogRead(analogInPin1);
// map it to the range of the analog out:
pH = map(sensorValue1, 0, 1023, 0, 14);
// change the analog out value:
analogWrite(analogOutPin1, pH);

the ‘range’ of analogWrite is 0-255.

hope that helps…

EDIT: code updated following OP comments

What's wrong is you have a load of code that isn't in any function. Perhaps you ended loop() too early?

And 'if ( pH > 6, 3)' almost certainly won't do what you think it will. Check the syntax.

Steve

I am sorry I didn't use code. please forgive me: this is my first topic and I tryid to read everything before asking. unfortunatly I am a real beginner and I had to take the courage to ask

Thanks Shezaad, you were useful!
I verify and it is correct now!
do you thing both relays will work under those conditions?
I alredy tried EC+ relay(on when ec is less than 1000) ,and it works perfectly.
but Steve sayd to me that probably using this sketch ,pH- relay(on when ph is up to 6.3) cannot work…
anyway I am gonna to try
Thanks, Thanks

diegodice1976:
Steve sayd to me that probably using this sketch ,pH- relay(on when ph is up to 6.3) cannot work...

Actually, he did not say it cannot work, he said it might not do what you think it does, and to check the syntax.

A relay will be VERY unhappy being controlled by the PWM output.....

Regards,
Ray L.

There is a real difference between 6.3 which you just said and 6,3 which is what was in your original code. It looks like sherzaad changed it for you without even mentioning it.

Steve

Hello Steve,
when I use 6.3 it never start, regardless of the pH value;
if I use 6,3 the relay is always 'on', regardless of the pH value as well.
sorry but I am too ignorant to see the error.
Thanks
ps: EC relay works perfectly...so the problem about pH is the way to write the value, but I don't get it
Diego

'pH' is an integer (int data type). It's silly to compare it to 6.3, since an integer could only be 6 or 7.

Put a Serial.print() in to tell you exactly what the value of pH is after the map(). Otherwise you're just guessing.

And maybe try it with if (pH > 6) to see what that does.

Steve

Relays don’t mind being driven with PWM.
it’s fairly common to,pull-in with 100%, then back off to say 30% for hold-in after a few tens of milliseconds to reduce overall power consumption

...but I am using 10 and 11 like digital(I/0), not like PWM. And Anyway I use only usb 5V plug to give power to Arduino. The relays are 5V as well.I use 12V in an exsternal circuit passing by the relays.
I suppose it was a problem about syntax: now it looks correct and working.
Thanks Everybody