Go Down

Topic: Slow response - how to improve . . ? (Read 3232 times) previous topic - next topic

PaulS

A return statement says "We're done here. Go back to the caller." If you put that in the loop, on the first iteration of loop, the function will end. The remaining iterations of loop will not happen.

It really does not make sense for SoilMosture() to get data for all 4 probes, AND for you to call it 4 times.

Either it gets data for all 4 probes, and you call it once, OR, it gets data for one probe and you call it 4 times. I vote for the latter.

michinyon

If green is so popular,  why do plants absorb all the colors except green ?

AWOL

Quote
If green is so popular,  why do plants absorb all the colors except green ?

I always assumed our green sensitivity was because, as a prey animal, our ancestors would have had a significant advantage being able to distinguish things against a green background.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

GoForSmoke


If green is so popular,  why do plants absorb all the colors except green ?


If you are not a creationist, it's a matter of what worked by chance being passed down. It's not a matter of the early life being able to pick and choose.

Yellow and green are very thin slices of the light spectrum but the first light-eaters reflected violet/purple and floated in the upper layers of the sea leaving a niche below where the green ones developed. Then there was a big wipe-out that only left the green type we know today. The ancestors of those bacteria live in plant leaves.

They absorb *most strongly* in red and blue so that's the colors to spend on grow lights. But again, check on indoor grow sites. Sunlight is the best but that's because it's so strong and free.

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

taz3m

Quote

A return statement says "We're done here. Go back to the caller." If you put that in the loop, on the first iteration of loop, the function will end. The remaining iterations of loop will not happen.
Code: [Select]

int reading[4];

for (int i=0; i<4; i++)
  {
  reading[i] = analogRead(moisture_input[i]);
   return reading[i];  >> this is where i go wrong . . !!
    }



Quote
It really does not make sense for SoilMosture() to get data for all 4 probes, AND for you to call it 4 times.


i got your point here . . i been able to figure out the flaws . . . :%

Code: [Select]

int SoilMoisture() >> /* only one variable to hold out the return result, Only one result from int reading and rest of probes rendered useless !!
how to call for more   */


Quote

Either it gets data for all 4 probes, and you call it once, OR, it gets data for one probe and you call it 4 times. I vote for the latter.


i did get it here! if i need to get that data from x probes and want to call it x times, then i will need to have such a function repeated x times in my code. .                ? :. :.?
tz
.....

taz3m

Quote

They absorb *most strongly* in red and blue so that's the colors to spend on grow lights. But again, check on indoor grow sites. Sunlight is the best but that's because it's so strong and free.


will get hold of some good literatures on that . . i shall post links here once i come across the one . .

thanks GoForSmoke . .

taz . . 
tz
.....

PaulS

Quote
Sunlight is the best but that's because it's so strong and free.

Yeah, right. Just ask anyone living above the Arctic circle in the winter how "free" sunshine is.

GoForSmoke

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

taz3m

#38
Jan 26, 2013, 06:07 am Last Edit: Jan 26, 2013, 06:15 am by taz3m Reason: 1
Hello,

Here is a good link to start with the growing LEDS: http://www.stealthgrow.com/advantages.htm

Can i start up if i use normal LEDS (Red & Blue) in addition with my T5 light for experimental purpose, as i have designed a small greenhouse with volume of 2' 4" x 1' 2.5" x 4' .

You did mention about getting a red and blue led to act as a sensor for these spectrums, still not yet come across. . . !

Edit : This link is quite informative about Leds use as detectors : https://led-brdf.wikispaces.com/Led+Sensing+and+Spectral+Sensitivity
tz
.....

GoForSmoke

I didn't "come across" because I don't have all day to find again what you should search for yourself.

Here's the most complete doc:
http://www.merl.com/papers/docs/TR2003-35.pdf

And here's the cookbook exercise:
http://playground.arduino.cc/Learning/LEDSensor

Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

taz3m

Hello . .

Thanks for the help GoforSmoke . . thats is certainly a complete guide . . thanks again . . ;)

I rewritten my code concerning the int SoilMoisture(); but this time i did it based on Blink without delay.

Without doubt the slow response is gone, but something is wrong in updating the soil moisture probe it always give 105.26 . . .

Code: [Select]


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


/* Declaration of I/Os */

 const int moisture_input = 1;        // Analog Input pin 1 connected to collect Soil Moisture reading.
 const int LDR_input = 2;             // Analog Input pin 2 connected to read Light Intensity from Ambient environment.
 const int Moisture_Input1 = 3;        // Analog Input pin 3 connected to Collect Soil Moisture reading
 const int Moisture_Input2 = 4;        // Analog Input pin 4 connected to Collect Soil Moisture reading
 const int Moisture_Input3 = 5;        // Analog Input pin 5 connected to Collect Soil Moisture reading
 #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


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


/* Library Code to be included */

#include <LiquidCrystal.h>      // include the library code:


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


/* Declaration Of Variables */

  int moisture;                 // Analogical value obtained from the experiment S.probe 1
  int Moisture1;                // Analogical value obtained from the experiment S.probe 1
  int Moisture2;                // Analogical value obtained from the experiment S.probe 1
  int Moisture3;                // Analogical value obtained from the experiment S.probe 1
  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;
 
  long PreviousMilis = 0;
  long CPT_Interval = 1000;
  long RvFw_Interval = 1000;
 
  float RLDR;
  float Vout;
 
  double PercentMoisture;      // Varible to store % Percentage Soil Moisture Value
  double PercentMoisture1;
  double PercentMoisture2;
  double PercentMoisture3;
 

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

/*LCD Pins Connections and Command*/


LiquidCrystal lcd(11, 10, 9, 8, 7, 6, 5);   // Initialize the library with the numbers of the interface pins


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

void setup (void) {
 
 Serial.begin(9600);                       // Set up Serial Communication at 9600 bps.
 
 pinMode(Pump,OUTPUT);                     // Define Digital Pin as output
 
 pinMode(SwapButton,INPUT);                // Define Digital Pin as Input    
 buttonState = digitalRead(SwapButton);    // Read the initial state and save into buttonState.


 lcd.begin(16,2);                          // Rows, columns. use 16,2 for a 16x2 LCD.
 lcd.clear();                              // Start with a blank screen
 lcd.setCursor(0,0);                       // Set cursor to column 0, row 0
}



void LCDprintMenu (void) {
 
 lcd.clear();
 lcd.print("Tazlim -- GrnHse");
 lcd.setCursor(0,1);
 lcd.print("  EngineerinG");
}


void LCDprintSoilMoist(void) {

lcd.clear();
lcd.print("SoilMoisture");
lcd.setCursor(0,1);
lcd.print("Percentge:");
lcd.print(PercentMoisture);
lcd.print("%");
}

void LCDprintLux (void){
 lcd.clear();
 lcd.print("Light Intensity");
 lcd.setCursor(0,1);
 lcd.print(" : ");
 lcd.print (Luminosity);
 lcd.print ("LUX");
 
}
 
void PumpControl (){
 
if (PercentMoisture <= 60.00)
   {digitalWrite (Pump,HIGH);}
   
   else {
   
   if (PercentMoisture >= 90.00)
      {digitalWrite (Pump,LOW);}
   }
 
}

void SwapMenu (void){
 
// Push Button connected to PIN 4 by Pull up resistor Circuit for swapping Menu on LCD

 val = digitalRead(SwapButton);   // Read Input value and store in val.
 delay(10);
 val2 = digitalRead(SwapButton);  // Read Input again to check for bounces
 
 if (val == val2){
   
   if(val != buttonState){
     
     if (val == LOW){
       
       if (lcdMode == 0)
           {lcdMode = 1;}             // Execute LCDprintSoilMoist().
       
     else{
     
       if (lcdMode == 1)            
          {lcdMode = 2;}              // Execute LCDprintMenu().
                         
                         
     else {
     
       if (lcdMode == 2)
          {lcdMode = 0;}      
       
    }
   }
  }
     
 buttonState = val;               // Save the new state in the Variable for monitoring further button press (Continuous program loop).
}  
}      
     
      if (lcdMode == 0)
            {LCDprintMenu();}                                               // Display Intro Display on the LCD.
     
      if (lcdMode == 1)
            {LCDprintSoilMoist();}                                          // Display SoilMoist Reading in %
   
      if (lcdMode == 2)
            {LCDprintLux();}                                                //Display Light Intensity in LUX
 
}


int LightSensorLDR (void){
 
 int ADC;

 ADC = analogRead (LDR_input);

 Vout = (ADC * 0.0048828125);           // Vout = Output voltage from potential Divider. [Vout = ADC * (Vin / 1024)]

 RLDR = (10.0 * (5 - Vout))/Vout;     // Equation to calculate Resistance of LDR, [R-LDR =(R1 (Vin - Vout))/ Vout]
  // R1 = 10,000 Ohms , Vin = 5.0 Vdc.
                                       


 Lux = (500 / RLDR);  
 
 return Lux;
 
}  
 


void loop (void) {

 
 
 SwapMenu();

 
 Luminosity = LightSensorLDR();

 
 moisture=SoilMoisture();         // assign the result of SoilMoisture() to the global variable 'moisture'
       // assign the result of SoilMoisture 3() to the global variable 'Moisture3'

 
 PercentMoisture  = ((moisture/950.00)*100.00); // Derivation of Soil Moisture in %

}  

int SoilMoisture(){
 
  unsigned long CurrentMilis = millis();
 
 // Variable to hold value from Moisture input.
 int reading;

 
 // 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
if (CurrentMilis - PreviousMilis > CPT_Interval)
    {PreviousMilis = CurrentMilis;

  // Take readings
 
 reading = analogRead(moisture_input);


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

// Give as much time in 'reverse' as in 'forward'

  if(CurrentMilis - PreviousMilis > RvFw_Interval)
     {PreviousMilis = CurrentMilis;

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

 return reading;

}

}


Can anyone guide me where im going wrong!!
tz
.....

GoForSmoke

Code: [Select]
   int SoilMoisture(){
 
   unsigned long CurrentMilis = millis();
 
  // Variable to hold value from Moisture input.
  int reading;

 
  // 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
if (CurrentMilis - PreviousMilis > CPT_Interval)
     {PreviousMilis = CurrentMilis;

   // Take readings
 
  reading = analogRead(moisture_input);


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

// Give as much time in 'reverse' as in 'forward'

   if(CurrentMilis - PreviousMilis > RvFw_Interval)
      {PreviousMilis = CurrentMilis;

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

  return reading;

}

}


You have set up a nice little logic problem where both if() are looking for the same thing but you want to run one after the other.

You only make a return   if(CurrentMilis - PreviousMilis > RvFw_Interval)
And you only make a reading   if (CurrentMilis - PreviousMilis > CPT_Interval) which sets previous to current
So what you get for the return is whatever was on the stack when the function ended but what the heck, even if you quick fixed that, reading is a non-static local that is lost before the analog read value could be returned. Quick fix that and you will find the next problem.

Not only that but every time the function is run, top is set LOW and bottom is set HIGH which undoes the reversal you coded at the end of   if (CurrentMilis - PreviousMilis > CPT_Interval).

Really you try to do too much in this function and why? To make a nice clean loop()?
I would at least test for timeouts inside loop() to avoid umpteen function calls for every 1 that -does- something and use a flag/state variable to determine which thing to do. Me, I wouldn't make functions at all but if you do then don't try rolling them into just one function... that just makes the code harder to work through.

Beyond that I don't know or want to know why the current reverse or any of that.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up