110Views12Replies

Author Options:

arduino not following my else statment Answered

ive got a issue with my arduino moisture sensor readings because my if and else statments.
my else statment will not follow it at all
here is my code..
// 11 sen
// 7 heater
// 4 analog sen

int sen = 11;
int hOn = 7;
int mot = 3;

void setup()
{
  Serial.begin(9600);
  pinMode(hOn, OUTPUT);
  pinMode(sen, OUTPUT);
  pinMode(mot, OUTPUT);
}

void loop()
{
  digitalWrite(mot, HIGH);
  digitalWrite(hOn, HIGH);
  digitalWrite(sen, HIGH);
  analogRead(4);
  delay(5000);
  digitalWrite(sen, LOW);
 
  if (analogRead(4)>=410)
  {
    Serial.print (analogRead(4));
    Serial.print (" Moisture Level Is Bad ");
    digitalWrite(hOn, LOW);
    digitalWrite(mot, LOW);
    delay(120000);
    digitalWrite(mot, HIGH);
    delay(300000);
    digitalWrite(mot, LOW);
    delay(120000);
    digitalWrite(mot, HIGH);
    delay(60000);
  }
  else if (analogRead(4)<=409)
  {
    Serial.print (analogRead(4));
    Serial.print (" All Is Good ");
    digitalWrite(hOn, HIGH);
    digitalWrite(mot, LOW);
    delay(120000);
    digitalWrite(mot, HIGH);
    delay(300000);
    digitalWrite(mot, LOW);
    delay(120000);
    digitalWrite(mot, HIGH);
    delay(60000);
  }
}

// i have tryed just a plain "else" statment and also another plain "if" statment

12 Replies

user
lewisb42 (author)2013-12-08

A couple of problems here:

1) for every check of the value read on pin 4, and even for every Serial.print() debug statement, you re-read pin 4, meaning the value on the pin could change at each point. Better to read it once, save the reading in a variable, and use that variable for your conditionals and debug statements. What you have right now could result in some very unpredictable and hard-to-debug behavior.

2) your else-if clause is clearly exclusive of your if clause. So it should be an else, not an else-if.

Re-worked code:

void loop()
{
  digitalWrite(mot, HIGH);
  digitalWrite(hOn, HIGH);
  digitalWrite(sen, HIGH);
 
  // save your reading in a variable
  int val = analogRead(4);

  delay(5000);
  digitalWrite(sen, LOW);

  // use that variable, and not a re-read of the analog pin, here
  if (val>=410)
  {
    Serial.print (val);
    Serial.print (" Moisture Level Is Bad ");
    digitalWrite(hOn, LOW);
    digitalWrite(mot, LOW);
    delay(120000);
    digitalWrite(mot, HIGH);
    delay(300000);
    digitalWrite(mot, LOW);
    delay(120000);
    digitalWrite(mot, HIGH);
    delay(60000);
  }
  else
  {
    Serial.print (val);
    Serial.print (" All Is Good ");
    digitalWrite(hOn, HIGH);
    digitalWrite(mot, LOW);
    delay(120000);
    digitalWrite(mot, HIGH);
    delay(300000);
    digitalWrite(mot, LOW);
    delay(120000);
    digitalWrite(mot, HIGH);
    delay(60000);
  }
}

Select as Best AnswerUndo Best Answer

user
yaene21 (author)lewisb422013-12-08

It works great with now a slight error the moisture reading value as far as I know from reaserch from the net say0-300 is dry and wet being the higher end but it doesnt make sense if the sensor reads the resistance then shouldnt dry be eg.1000 and wet be 0

Select as Best AnswerUndo Best Answer

user
lewisb42 (author)yaene212013-12-09

The analog inputs don't measure resistance, they measure voltage. I'm assuming the sensor basically acts as a potentiometer where as the resistance increases, the voltage goes down.

Just guessing, but it's more likely that the resistance decreases as the sensor gets wetter. So, assuming that's the case, the wetter the sensor, the lower the resistance, the higher the voltage -- and the higher the reading.

Select as Best AnswerUndo Best Answer

user
yaene21 (author)lewisb422013-12-10

Ok yea that makes sense but when I did a stand alone test with just water we get a low reading of 100-270 and then when its in moist soil we get a high reading
so if it does measure voltage then this would be correct but what I dont understand is every forum and moisture sensor site ive looked at say 0-300 is dry and 1000 being wet so therfor the sensor is saying it needs less volts for dry soil and more for wet soil maybe im investergating this to much and making it more confusing than what it really is

Select as Best AnswerUndo Best Answer

user
yaene21 (author)lewisb422013-12-09

I fixed that error now we have a power problem the programe works fine connected though the usb on the computer but when we use the external power supply it wont run yhe heater function.
We have tested the power and amps that checks out.
everything gas power but its like the board wont send a signal to the heater relay to sswitch it

Select as Best AnswerUndo Best Answer

user
yaene21 (author)yaene212013-12-09

ignore my last comment for some reason im getting differnt values when i am plugged in via the laptop and the external power

Select as Best AnswerUndo Best Answer

user
steveastrouk (author)lewisb422013-12-10

Only thing wrong with that is there is no hysteresis, and if the value is hovering around 410, it will jitter the output.

Select as Best AnswerUndo Best Answer

user
yaene21 (author)lewisb422013-12-08

Thank you il try that and as u can see still very new to this thank you again

Select as Best AnswerUndo Best Answer

user
yaene21 (author)2013-12-05

so that change to the if statment i did didnt work as i just got 396 value from the sensor
and it read " moisture level is bad " why is this when its set to >=410
im absolutely stumped

Select as Best AnswerUndo Best Answer

user
yaene21 (author)2013-12-05

it apears to be working with..
if (analogRead(4)==410||analogRead(4)>411)
and..
else if (analogRead(4)==409||analogRead(4)<408)

Select as Best AnswerUndo Best Answer

user
yaene21 (author)yaene212013-12-05

but still unsure as i need to test it for longer

Select as Best AnswerUndo Best Answer

user
yaene21 (author)2013-12-05

the reading value ive now getting is 391 which by what my code say it should after the 10min delay go to "all is good" and turn off the heaters

Select as Best AnswerUndo Best Answer