Last stage debugging - Help !![SolveD]

This section (and others like it) could be improved/shortened:

  if (pump_Status == true)
    {
      Serial.print(" Pump Status : ACTIVE ");
      Serial.println();
    }

  else if (pump_Status == false)
    {
      Serial.print(" Pump Staus : INACTIVE ");
      Serial.println();
    }

If you're testing a boolean, you can simply use

if (pump_Status)

Since it's a boolean, if it's not true, no need to test if it's false.

You have some very repetetive code there. Consider adding a function that takes a boolean and a string. Let the function print the string, test the boolean, and println "active" or "inactive" depending on its value. Then use if for all that conditional printing you're doing.

Amongst other things -

Serial.print(" Pump Status : ");    Serial.println((pump_Status ? "ACTIVE " : "INACTIVE "));
Serial.print(" Light Status : ");   Serial.println((LightStatus ? "ACTIVE " : "INACTIVE "));
Serial.print(" Fan Status : ");     Serial.println((FanStatus ? "ACTIVE " : "INACTIVE "));

hello!

Thanks for the suggestions..

Ok i did get it, i will work to shortened and remove some repetitive codes further up, but will have to do some more reading what i see :%

The serial print for now is just to test if code is running properly, however i will be using LCD.print to display all these stuffs which is in serial.print for now.

Serial.print(" Pump Status : ");    Serial.println((pump_Status ? "ACTIVE " : "INACTIVE "));

Serial.print(" Light Status : ");  Serial.println((LightStatus ? "ACTIVE " : "INACTIVE "));
Serial.print(" Fan Status : ");    Serial.println((FanStatus ? "ACTIVE " : "INACTIVE "));

One question here, how will the code know that if pump_Status is 'true' to display ACTIVE or INACTIVE . .

thanks
taz . .

'false' is always zero, not false is alway not 'zero' yet not 'exactly' 'true' while 'true' is only 'true'.

To answer the question read -

http://www.cplusplus.com/articles/1AUq5Di1/

You have some very repetetive code there. Consider adding a function that takes a boolean and a string. Let the function print the string, test the boolean, and println "active" or "inactive" depending on its value. Then use if for all that conditional printing you're doing.

Thats seems interesting, i will try to do this . . any help or source where i can start to grasp a good comprehension about the subject. I think i will be starting with Arduino Reference for string in the meanwhile . .

Thanks . .

'false' is always zero, not false is alway not 'zero' yet not 'exactly' 'true' while 'true' is only 'true'.

Seems confusing, but i shall read it for a proper understanding, i shall keep things posted here, and in time of trouble, i shall ask for a push from time to time . . :wink:

I am actually editing my code for implementation with a 16X2 lcd, and display all my serial print to the latter.

Thanks for your precious time and support . .

See ya ..

taz ...

What I'm suggesting is that the sections like this:

  if (pump_Status == true)
    {
      Serial.print(" Pump Status : ACTIVE ");
      Serial.println();
    }

  else if (pump_Status == false)
    {
      Serial.print(" Pump Staus : INACTIVE ");
      Serial.println();
    }

Could be replaced by a call like this:

DisplayStatus("Pump",pump_Status);

Where the DisplayStatus Function is:

void DisplayStatus (char* Title,boolean Status)
{
Serial.print(Title);
if(Status)
  Serial.println(" ACTIVE);
else
  Serial.println(" INACTIVE");
}

It's simple really. Conditional tests such as used in 'if', 'while', 'do while', and several others, evaluate to zero as false and not zero as not false.

The 'bool' values 'false' and 'true' have specific values - 'false' is always zero while 'true' may be any non-zero value depending upon the language version.

Here is an easy to read article concerning conditionals in C++

http://www.cplusplus.com/forum/articles/3483/

Hello,

Im back again after some workings on the code.

I added up from the previous revision, an lcd 16x2, with some new lines in the code, but here im stuck with some bugs in the code.

My temperature part of the code is not giving me values. I suspected some wiring went wrong, however, when i test the temperature code alone, it works as nice as it should be. . .

With this new revision i added the lcd, and some menu to be displayed like, intro menu, soile percentage menu and LDR Raw reading. . I went through a pushbutton to swap between menus.

Here also without pressing my button, the lcd is changing menu.

Find herewith attached my code. Any suggestions where i can tackle for this bug.

Thanks in advance . .

GreenHouse_Rev_F_03.ino (10.3 KB)

I'm curious why this continues to linger in your code -

if (FanStatus == true);{
FanControlOFF ();
}

Oops was busy adding new lines and forgot about modification.

A small recap please, :slight_smile: . .

I need not to test for a boolean if it is true or not right ?

intead of above i should i be using something likewise :

lloyddean:
And didn't you mean -

    if ( ! FanStatus )

{
        FanControlON();
    }
    else
    {
        FanControlOFF();
    }

Im getting to it, now :sweat_smile: .. thanks again . .

We'll keep questioning the code, you make the modifications and repost them.

Hello,

I have made modifications of the code and compile all what i need to run the Greenhouse project up to here.

I shall resume whats in there quickly:

  1. Monitoring soil moisture - - Setting pump to active upon a threshold value.

  2. Monitoring temperature -- Setting Fan active upon threshold value.

  3. Monitoring light intensity -- Setting Grow Light to active upon threshold value

  4. Display Soil moisture percentage on 16 x 2 display

  5. Display raw analog of photo reading and ambient light condition value on LCD

  6. Display Temperature reading on LCD

  7. Display Output status if active or inactive on LCD

  8. One pushbutton to change in between menus of the LCD.


Attached herewith the code, which i compiled and shortened at my best try . . :relaxed:

However, i running some bugs as follows:

  1. My temperature part of the code is not working, returning me a value of 0 both on LCD and serial port. Neither my FAN being activated.

  2. WIhtout pressing the pushbutton, my Lcd menu is changing automatically, which i do not want.

I have tested all parts of my hardware till now, they seem to be ok, as i did run the temperature code part alone and it worked nicely.

May be i have an issue with my switch button, i do not know if i can enable INPUT_PULLUP on pin 1 of the uno. . . as in almost all e.g. they are using pin2.

Thanks for any suggestions, i shall appreciate it as much, as im learning. :slight_smile:

Regards

taz . . .

GreenHouse_Rev_F_04.ino (10.7 KB)

This:

    itemIdx = (itemIdx + 1)% N_items;

will give you values from 0 to 4. You need a 5 to make TempRead true, which will therefore never happen. Tempread controls whether you read the temp, so you never do. What's the purpose of funcActive, ReadPhoto and TempRead?

Helo Bill,

You got the pin out of the forest . . :D, it seems, that i did add new case number and forget to update the value for itmIdx & N_items;

funcActive purpose is to make the function "pumpControlON()" only after reading percentage moisture value, as my threshold value to run my pump rely on this value.
Why, i used this state, because since only after about 3000UL will i get my percentage reading,whereby the loop speed is in ms.

ReadPhoto & TempRead are used to take reading from analog pins within a time interval, so as to prevent noise occured when taking reading on several analog pins at almost same time.

Thanks for your treat bill, my sketch is working nice. :slight_smile:

However,debugging continues :), my display is still swapping menu automatically, and when i pressed my switch it swap the menu quicker.

Just a suggestion here : pin 1 and 0 are for Tx & Rx, im additionally using Serial.begin, while having connected my switch button on pin 1 and moreover activated the INPUT_PULLUP.

Any suggestion if the bug is derivating from these, stuffs.
[edit] i just comment on the : // Serial.begin (9600) . . and here my display work as the code dictates . . :), but when my switch case for modedisplay return to 0 each time, everything seems to reset and start over again along with small bugs.

If im commenting the //Serial.begin (9600), should i also be commenting all instruction within the code making use of the serial port ?
(. .trying this in meantime . . )

Explanations are welcomed;

Thanks . . .

taz . .

It certainly doesn't seem like a very good idea to use pin one for your switch. It doesn't look as though you've used all the analog pins - you can use them as digital pins too, so try moving the switch to one of them.

Hello,

I did close up all serial comminication within the code.

Change the Switch button place to A2, analog pin, Enable pullup resistor on the pin as well.

All is working nicely as it should, still some fine tuning is needed please . .

I know that i did place a flag called ButtonRead, to activate my Swapmenu function, and after each 6 seconds the Controller will read that, explaining my delay in swapping display on my LCD. I did that, as what explained in the arduino tutorial when activating analog pins to act as digital, a delay should be implemented to avoit jitty and electrical noise in reading analog sensors. Any approach how can i tackle this more easily.

    switch(itemIdx){

      /*-- Soil Moisture Reading Sequence--*/
      // Allow current to flow in Forward and Reverse direction while reading value from Soil Moisture Probe in both direction. Allowing delay between reading of analog sensors.  

....

    case 4:
      ReadPhoto = true; 
      break;

    case 5:
      TempRead = true;
      break;

    case 6:
      ButtonRead = true; 
    }
  }
  /*--- Function for swap menu display ---*/
  if (ButtonRead){
    SwapMenu();
    ButtonRead = (! ButtonRead);  
  }

Am having a weird issue, since morning, before coding the Revision 5 of my code, i.e. still running Rev 4 my temperature reading is unstable, it display values like 45 C and 18 C.
Yesterday night, before it did work nice, displaying amb. temperature in the range of 24 C.

When i run the code for temperature alone, i get correct values. . . Where is my mistake here . . . ? ? ?

Thanks for your time and consideration towards my issues . .

Find attached Rev 5 of my code.

GreenHouse_Rev_F_05.ino (11.4 KB)

The analog pins share an ADC. The reading you get on one pin may be influenced by a prior reading on another if they occur in rapid succession. A common workaround if this is happening is to read twice on the same pin and just ignore the first reading. Try it for your temp input.

Thats make some sense why the reading is as such.

Do i need to take another reading on same pin and ignoring the previous or can i take a sample of 2 to 3 readings and then calculate an average ?

Another thing i just updated with the code:

I removed alll serial command lines, and eventually my lcd display menu would act as flashy, i implement a timer [millis()] with a delay of 450ms, and my flashy display get solved. However, my pump conditions to run is either run for 7 sec or if percent moisture is > 90 %, when i remove my soil probe, it appears as if my timer with the pump is not working, that is it does goes off after 7 sec. Which it did before i implement the timer for the lcd display.

void pumpControlOFF (){
  if (( millis() - lastPump >= timerRateX ) || (percentage_Moist >= 90.00)){
    lastPump += timerRateX;
    digitalWrite(pump_Active,LOW);  
    pump_Status = false; 
  }
}
  /*--- Function for swap menu display ---*/
  if (millis()- lastdisplay >= timerRateZ)
  {
    lastdisplay += timerRateZ;  

    SwapMenu();
  }

Thanks

Do i need to take another reading on same pin and ignoring the previous or can i take a sample of 2 to 3 readings and then calculate an average ?

Average subsequent readings if you like, but ignore the first one.

Thats great bill, . .

I did as you suggested, reading the pin twice, and ignoring the first reading,. . Im getting my real temperature reading . . Room temperature actually reading 26.86 *C . .

My only issue now is, the pump time off . .

Will be looking in that!

Thanks again . . + karma for you .. :smiley: