if statement verification

greetings,

can someone help me verify this source code. when i try to run the source code it seem only one waterpump is functional which the M1. Im totally new to arduino and coding. your kind help and feedback is very much appreciated. thank you.

const int highppt = 44; //max salinity value
const int lowppt = 7;  //min salinity value
const int highph = 4;  //max pH value
const int lowph = 0;   //min pH value

//Arduino PWM Speed Control:
int E1 = 5;
int M1 = 4;
int E2 = 6;
int M2 = 7;

int h2opump = 10; //water pump pin out 10

void setup()
{
  Serial.begin (9600);
  Serial.println("pH and salinity control experiment");
  Serial.println("      (Salinty)        (pH)  ");

  lcd.begin(16, 2);
  pinMode(M1, OUTPUT);
  pinMode(M2, OUTPUT);
  pinMode(h2opump, OUTPUT);
}
void loop()
{
  
  int phSensor = analogRead(A4); //read ph value
  phValue = 0.01709 * phSensor;

  if (phValue > highph)      // if the analog value > max pH, turn on water pump:
  {
    digitalWrite(h2opump, HIGH);
  }
  else if                    // if the analog value < min pH, turn on pH pump:
  (phValue < lowph)
  {
    digitalWrite(M2, HIGH);
    analogWrite(E2, 100);
  }
  else                       // else do nothing
  {
    analogWrite(E2, 0);
    digitalWrite(h2opump, LOW);
  }
  
  int salinitySensor = analogRead(A5);  //read the salinity value
  pptValue = salinitySensor * 0.0064;

  if (pptValue > highppt)     // if the analog value > max salinity, turn on water pump:
  {
    digitalWrite(h2opump, HIGH);
  }
  else if                    // if the analog value < min salinity, turn on salinity pump:
  (pptValue < lowppt)
  {
    digitalWrite(M1, HIGH);
    analogWrite(E1, 100);
  }
  else                       // else do nothing
  {
    analogWrite(E1, 0);
    digitalWrite(h2opump, LOW);
  }

probably want to post this over in programming, not project guidance.

looks like

const int lowph = 0; //min pH value

else if // if the analog value < min pH, turn on pH pump:
(phValue < lowph)
{
digitalWrite(M2, HIGH);

====================================================

just how does phValue ever get below 0 ?

as another note, do you every want to turn M1 or M2 off after you turn them on ?

hi dave-in-nj,

thx for the feedback, the min ph value supposed to be 5, i change it to zero just to see how the feedback from arduino.

yes i want the motor to switch off after it reach the range of desired ph and salinty.

Please do not make a second Thread about this project. If you feel like moving to the Programming section (which I don't think is necessary) please ask the moderator to move your Thread.

Your names E1 E2 M1 M2 are meaningless. It would be much easier to make sense of your code (and to know if there is a key element missing) if you give them meaningful names like you gave to h2oPump.

In the code you posted you have not declared the variable phValue. The type of that variable is important. The code won't work if it is an int. It would need to be a float. And it would probably be better to make the comparison constants floats also - at least it would be less confusing.

You have this line

 else                       // else do nothing

and then it is followed by some code which makes the comment meaningless. It does NOT "do nothing". It seems to switch something off.

...R

Hi, Looking at your fritzy, you only have three wires to the relay, have you got the four connections in real life.

Please when you want to show files, please use REPLY not QUICK REPLY, and attach using the ATTACHMENT facility.

It is sooo much easier.

Tom.... :)

hi Robin2,

thx for the valuable lessons. im totally new with arduino.. the source code is some combination from many source code that i encounter with.. just learning through trial n error..

hi TomGeorge,

thx for the feedback.

TomGeorge: Hi, Looking at your fritzy, you only have three wires to the relay, have you got the four connections in real life.

there are four wires actually, 1 is from the motor connected to Normally close pin.

TomGeorge: Hi, Looking at your fritzy, you only have three wires to the relay, have you got the four connections in real life.

Please when you want to show files, please use REPLY not QUICK REPLY, and attach using the ATTACHMENT facility.

It is sooo much easier.

Tom....

it is good practice to use a diode on a relay to direct the back EMF to ground. better yet, use a transistor to handle the power of the coil of the relay. it may be that the power needed will exceed the power available from a pin and will burn out the pin on the Arduino.

Robin2: In the code you posted you have not declared the variable phValue. The type of that variable is important. The code won't work if it is an int. It would need to be a float. And it would probably be better to make the comparison constants floats also - at least it would be less confusing.

I am far from a good programmer and ask this for my own education. this line : phValue = 0.01709 * phSensor;

I though that this line does declare phValue, and that by default is an integer ? obviously, it does not do what the writer intended unless he meant to use it as a limit. it seems that as an integer, the value of phSenor will need to be over about 30 so that the result will be about 0.051 and round up to an integer. the maximum, if we use 1024 at the input will be 18 as 1024*0.01709 = 17.5001

dave-in-nj: I though that this line does declare phValue, and that by default is an integer ?

It won't compile in a test I tried. If it did, how would the compiler distinguish local from global variables?

...R

hi guys,

somehow i manage to run my project with this

#include<LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 9, 8, 3, 2);

float pptValue = 0;
float phValue = 0;

//Arduino PWM Speed Control:2A dual motor shield L298
const int pptpumpspeed = 5;    //pump speed control
const int phpumpspeed = 6;     //pump speed control

int phpump = 7;     //pump pin out 7
int h2opump = 13;   //water pump pin out 13
int pptpump = 4;    //pump pin out 4

float phpin =  A4;   //ph sensor input pin
float pptpin = A5;   //salinity sensor input pin


void setup()
{
  Serial.begin (9600);
  Serial.println("pH and salinity control experiment");
  Serial.println("      (Salinty)        (pH)  ");

  lcd.begin(16, 2);
  pinMode(phpin, INPUT);
  pinMode(pptpin, INPUT);
  pinMode(pptpump, OUTPUT);
  pinMode(phpump, OUTPUT);
  pinMode(h2opump, OUTPUT);
  digitalWrite(pptpump, LOW);
  digitalWrite(phpump, LOW);
  digitalWrite(h2opump, LOW);
}

void loop()
{
  digitalWrite(h2opump, LOW);
  digitalWrite(pptpump, LOW);
  digitalWrite(phpump, LOW);
  
  int phSensor = analogRead(phpin); //read ph value
  phValue = 0.01709 * phSensor;

  if (phValue > 8.5)          // if the analog value > max pH, turn on water pump
  {
    digitalWrite(h2opump, HIGH);
  }
  else if                    // if the analog value < min pH, turn on pH pump
  (phValue < 7)
  {
    digitalWrite(phpump, HIGH);
    analogWrite(phpumpspeed, 100);
  }
  else                       // else switch motor off
  {
    analogWrite(phpumpspeed, 0);
    digitalWrite(h2opump, LOW);
  }

  int salinitySensor = analogRead(pptpin);  //read the salinity value
  pptValue = salinitySensor * 0.0064;

  if (pptValue > 25)         // if the analog value > max salinity, turn on water pump
  {
    digitalWrite(h2opump, HIGH);
  }
  else if (pptValue < 15 )   // if the analog value < min salinity, turn on salinity pump
  
  {
    digitalWrite(pptpump, HIGH);
    analogWrite(pptpumpspeed, 100);
  }
  else                       // else switch motor off
  {
    analogWrite(pptpumpspeed, 0);
    digitalWrite(h2opump, LOW);
  }

  Serial.print("\t");
  Serial.print(pptValue);
  Serial.print("\t");
  Serial.print("       ");
  Serial.print(phValue);
  Serial.println("\t");
  delay(1000);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("ppt:");
  lcd.setCursor(5, 0);
  lcd.print(pptValue);
  lcd.setCursor(0, 1);
  lcd.print("pH:");
  lcd.setCursor(4, 1);
  lcd.print(phValue);
  delay(1000);
}

but the problem is during the experiment with a small tank around 25 litre of water, the ph sensor show 0 value… why is that? im using dfrobot ph meter.

This code

  else                       // else switch motor off
  {
    analogWrite(phpumpspeed, 0);
    digitalWrite(h2opump, LOW);
  }

follows on from code that switches on either a h2opump or a phpump. However it only switches off the h20pump.

After this code

  int phSensor = analogRead(phpin); //read ph value
  phValue = 0.01709 * phSensor;

print the value in phSensor so you can see the value before the maths.

If you are having trouble getting values from a sensor just write a short program that does nothing but get values and print them to the Serial Monitor.

...R

What is the pH of your tank? Is this your sensor? http://dfrobot.com/wiki/index.php/PH_meter%28SKU:_SEN0161%29

What have you done to deal with the negative mv reading at pH values above 7.0? If your pH is above 7, you will be reading 0 based on the analog read. Does the interface board between the probe and the arduino shift the voltages to positive? Is there amplification?

EDIT --Given the range of voltages you expect to see with moderate pH values, I think that you will be better off using the internal 1.1 volt analog reference and modifying the formula for pH from mv. http://www.arduino.cc/en/Reference/AnalogReference

Robin2:
It won’t compile in a test I tried.
If it did, how would the compiler distinguish local from global variables?

…R

guess that outside of the initialization space, the only other place to declare a variable is inside of a for loop.