Slow response - how to improve . . ?

Hello to all,

I would like to state that im still a noob and trying to learn as much as it needs to continue my progress with the Arduino.

Thanks for your prompt reply, i appreciate all your comments, that make me more motivated to go on further to improve my code and learn more and more. Thanks again. . :wink:

PaulS:
You could read the switch state after each call to SoilMoistureN().

Im i correct, if in the void loop, i place the SwapMenu() after each MoistureN = SoilMoistureN(); ? I.e using in all 4 SwapMenu(); ??

Why do you need 4 nearly identical functions. One would do, if the pin numbers were passed in. Then, you could call the one function in a for loop, testing the switch state after each iteration.

Im actually testing the soil moisture percentage for 4 different variety of plants, i shall conclude if they consume almost the same amount of water per day.

According to what you said about passing the pin numbers and calling a for loop, can you kindly please explain a little more so that i can grasp the max and try to implement it.

The SwapMenu() code is ridiculous. Testing to see what value lcdMode has before incrementing it is silly. Just increment is, whatever value it has. Then, test to see if it is out of range. Fix it, it if is.

I shall try to perform this increment algorithm, will keep thing posted to see if im on the right track.

robtillaart:
Also the use of arrays can simplify the code as you have four times the same measurements.

int SoilMoisture(int i) is a function that needs to be rewritten to but with the above +
the info about arrays in the tutorial section you should be able to figure it out.

I Shall consult the array tutorial and come back here for clarifications if needed, and im sure i will need those.

michinyon:
Well your lux calculation is wrong, for a start.

You need to re-read the description of how the light-sensitive device works, and how it is approximately
linear on a log-log scale. It's the last bit of that sentence, I think you didn't understand.

It seems i did not much understand the linear on a log-log scale, but i presumed it was for data logging, and i wrong, can you please guide me on this issue.

Thanks

taz3m:
I shall try to perform this increment algorithm, will keep thing posted to see if im on the right track.

The "increment algorithm"? As in, adding one to a variable?

Hello,

I have been through the tutorial for Arrays and For loop, that indeed will simplify several things.

Im in the process of rewriting my code, i shall keep things posted to know if im on the right track.

Can you please clarify these for me :

lcd.print(i, DEC);

lcd.print(PercentMoisture[i], 1);

Thanks

taz . .

Can you please clarify these for me :

Sure. You tell us what is not clear, and we'll clear that up.

lcd.print(i, DEC);

Display the integer value "i" in DECimal (the default)

lcd.print(PercentMoisture[i], 1);

Assuming "PercentMoisture" is a floating-point value, display it with one place of decimals after the point.

taz3m:
Can you please clarify these for me :
...

Add egg whites?

lcd.print(i, DEC);
Display the integer value "i" in DECimal (the default)

It is only explicitly needed when i is a unit8_t or byte as then the print() method is ambiguous (can be resolved in multiple ways by the compiler)

Hello,

Seems you did not like the phrase about clarification . . sorry about that . . :%

Im getting an error while compiling this code . .

Error Code :

Rev_05_Array_for_SoilMoist.cpp: In function 'int SoilMoisture(int)':
Rev_05_Array_for_SoilMoist:49: error: expected primary-expression before '[' token
Rev_05_Array_for_SoilMoist:49: error: expected primary-expression before ']' token
Rev_05_Array_for_SoilMoist:49: error: expected primary-expression before '{' token

Thanks

Taz
/* Declaration of I/Os */

  #define moisture_input [] = {1,3,4,5}        // Analog Input pin 1 connected to collect Soil Moisture reading.
  #define LDR_input 2                          // Analog Input pin 2 connected to read Light Intensity from Ambient environment.
  #define SwapButton 4                         // Digital pin 4 Push Button connected - Purpose : To sawp menu on LCD screen.
  #define dcSource_top 13                      // Power source 5V top connected to digitsl pin 13
  #define dcSource_bottom 12                   // Power Source 5V bottom connected to digital pin 12
  #define Pump 3                               // Irrigation Pump connected to digital pin 3
 

/*--------------------------------------------------------------------*/

   
   int Moisture[4];                 // Analogical value obtained from the experiment S.probes (1,2,3,4)
   int val;                      // Variable for reading the pin status to compare with buttonState
   int val2;                     // Variable to read the debounced status
   int buttonState;              // Variable to hold the last button state
   int lcdMode = 0;              // Variable to store different Modes to swap between LCD menu.
   int Lux;                      // 
   int Luminosity;
   
   float RLDR;
   float Vout;
   
   double PercentMoisture[4];      // Varible to store % Percentage Soil Moisture Value for S.probe (1,2,3,4)


int SoilMoisture(int i){
  
   
  // Variable to hold value from Moisture input.
  int reading[4];
 
  
  // Set driver pins to outputs
  pinMode(dcSource_top,OUTPUT);
  pinMode(dcSource_bottom,OUTPUT);

  // Drive a current through the divider in one direction
  digitalWrite(dcSource_top,LOW);
  digitalWrite(dcSource_bottom,HIGH);

  // Wait a moment for capacitance effects to settle
  delay(1000);

  // Take readings
  
  for (int i=0; i<4; i++)
  {
  reading[1] = analogRead(moisture_input[i]);
   //return reading[i];  
    }


  // Reverse the current
  digitalWrite(dcSource_top,HIGH);
  digitalWrite(dcSource_bottom,LOW);

  // Give as much time in 'reverse' as in 'forward'
  delay(1000);

  // stop the current
  digitalWrite(dcSource_top,LOW);

  

}
#define moisture_input [] = {1,3,4,5}

What's that?

try to get the application right for 1 sensor first. Then increase it to multiple sensors and then use array's.

Also read the book Kerningham & RItchie C language 2nd edition. "Worth its weight in brains"

hello,

I found that by using

#define moisture_input [] = {1,3,4,5}

whereby i was defining analog pin 1,pin 3, pin 4, pin 5 to be used to collect information from soil moisture probes.

Instead when i use

int moisture_input [] = {1,3,4,5};

It do compiles . .

whereby i was defining analog pin 1,pin 3, pin 4, pin 5

No, you weren't. You were defining a strange macro.
Do the substitution, and you'll see why the compiler complained.

robtillaart:
try to get the application right for 1 sensor first. Then increase it to multiple sensors and then use array's.

I already try the application for 1 sensor, it did work . . thats why i would like to extend to 4 now.

Anyway thanks for the reference book .

taz ..

AWOL:
Do the substitution, and you'll see why the compiler complained.

Which thing do i need to substitute . . ??

A #define is a macro.
When the C preprocessor sees the macro name it substitutes the macro value as a simple 1:1 text replacement.

just to see if i did understand the working of the array.

im i doing it right ?

/* Declaration of I/Os */

  int moisture_input [] = {1,3,4,5};        // Analog Input pin 1 connected to collect Soil Moisture reading.
  #define dcSource_top 13                      // Power source 5V top connected to digitsl pin 13
  #define dcSource_bottom 12                   // Power Source 5V bottom connected to digital pin 12
 
 

/*--------------------------------------------------------------------*/

   
   int Moisture[4];                 // Analogical value obtained from the experiment S.probes (1,2,3,4)
   

void setup (){


Serial.begin (9600);

}


int SoilMoisture(){
  
   
  // Variable to hold value from Moisture input.
  int reading[4];
 
  
  // Set driver pins to outputs
  pinMode(dcSource_top,OUTPUT);
  pinMode(dcSource_bottom,OUTPUT);

  // Drive a current through the divider in one direction
  digitalWrite(dcSource_top,LOW);
  digitalWrite(dcSource_bottom,HIGH);

  // Wait a moment for capacitance effects to settle
  delay(1000);

  // Take readings
  
  for (int i=0; i<4; i++)
  {
  reading[i] = analogRead(moisture_input[i]);
   //return reading[i];  
    }


  // Reverse the current
  digitalWrite(dcSource_top,HIGH);
  digitalWrite(dcSource_bottom,LOW);

  // Give as much time in 'reverse' as in 'forward'
  delay(1000);

  // stop the current
  digitalWrite(dcSource_top,LOW);

}

void loop (){

for (int j=0; j<4; j++){
  Moisture[j] = SoilMoisture();  
}
  
Serial.print ("value = ");

Serial.print (Moisture[0]);

}
#define moisture_input [] = {1,3,4,5}

You can't use defines like that. Change that line to:

const int moisture_input [] = {1,3,4,5};       // Analog Input pin 1 connected to collect Soil Moisture reading.

im i doing it right ?

Better.

Seems you did not like the phrase about clarification . . sorry about that .

As long as it made you smile. :slight_smile:

So, SoilMoisture() is defined as returning a value. There is no return statement, so there will be garbage on the stack that is popped off and stored in the array, each time the function ends. If that's what you want, carry on.

const int moisture_input [] = {1,3,4,5};       // Analog Input pin 1 connected to collect Soil Moisture reading.

Here const means that the value assigned to the array wont change, like here the pin number, correct ?

Better.

Motivating.

As long as it made you smile. :slight_smile:

I shall make the effort to always :slight_smile: :wink:

So, SoilMoisture() is defined as returning a value. There is no return statement, so there will be garbage on the stack that is popped off and stored in the array, each time the function ends. If that's what you want, carry on.

oops i think i forget to remove the // coment line here :

 for (int i=0; i<4; i++)
  {
  reading[i] = analogRead(moisture_input[i]);
   return reading[i]; >>  remove // coment  
    }

if that is correct i shall be moving towards the reading of the switch.
I did not yet upload, may be this afternooon after work.

Thanks

Taz ...

for (int i=0; i<4; i++)
  {
  reading[i] = analogRead(moisture_input[i]);
   return reading[i]; >>  remove // coment  
    }

You really don't want to remove the comment - think what will happen.

Edit: Or, more to the point, think what will not happen.