Pages: [1]   Go Down
Author Topic: Coding w/o delay func!  (Read 347 times)
0 Members and 1 Guest are viewing this topic.
Mauritius
Offline Offline
Jr. Member
**
Karma: 2
Posts: 95
Learning Never Ends . . .
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I have brought some changes to my previous code under the topic Slow response, how to improve.

In this one i did not use the delay function in reading the soil moisture. I have 3 of these function in the code. However, i made the probe active high on only one side and did not do the alternate current on the soil probe.

I did try to make it but could not as it would not work at all. Any suggestion please.

I notice on the LCD, things are quite flashy as if the values want to change but not changing, but itd updating quite nice, any approach please.

Thanks

Taz.

Code:
/*Declaration of I/Os */

const int Moisture_Input0 = 1;
const int Moisture_Input1 = 2;
const int Moisture_Input2 = 3;

const int SwapButton = 12;

const int dcSource = 13;

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

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

/*
LCD Connections:
rs (LCD pin 4) to Arduino pin 11
rw (LCD pin 5) to Arduino pin 10
enable (LCD pin 6) to Arduino pin 9
LCD pin 15 to Arduino pin 4
LCD pins d4, d5, d6, d7 to Arduino pins 8, 7, 6, 5
*/
/*--------------------------------------------------------------*/

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



/* Declaration of Variables*/

unsigned long Sprb_timer;
const unsigned long Cpt_Efcts = 1000;
int Moist_Val_0;
int Moist_Val_1;
int Moist_Val_2;
double Percentage_S_Moist_0;
double Percentage_S_Moist_1;
double Percentage_S_Moist_2;

int val;
int val2;
int buttonState;
int lcdMode = 0;

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

 void setup (void) {
 
   Serial.begin (9600);
   
   pinMode(dcSource,OUTPUT);
   
   pinMode(SwapButton,INPUT);                // Define Digital Pin as Input   
   buttonState = digitalRead(SwapButton);
     
   Sprb_timer = millis (); // Returns the milliseconds ellapsed since Prgm started
   
   lcd.begin(16,2); // rows, columns. use 16,2 for a 16x2 LCD, etc.
   lcd.clear(); // start with a blank screen
   lcd.setCursor(0,0); // set cursor to column 0, row 0
   
 }
 
 void Sprb_i_strt (){
 
  digitalWrite (dcSource,HIGH);
 
// Remember when Fwd direction of current has been set.
    Sprb_timer = millis (); 


 
 int S_Moisture_Rd_0(){
   
   int i;
   int Moist_Read_0;
   int Moist_Vals_0[4];
   int Av_Moist_0 = 0;   
 //Variable to hold value from Moisture Probe


for (i=0;i<4;i++){
 Moist_Vals_0[i] = analogRead(Moisture_Input0); 
 // Take reading from Soil Moisture Probe.
 
 
 Av_Moist_0 = Av_Moist_0 + Moist_Vals_0[i];
}
 
 Moist_Read_0 = (Av_Moist_0/4);
 
 return Moist_Read_0;

 }
 
  int S_Moisture_Rd_1(){
   
   int i;
   int Moist_Read_1;
   int Moist_Vals_1[4];
   int Av_Moist_1 = 0;   
 //Variable to hold value from Moisture Probe


for (i=0;i<4;i++){
 Moist_Vals_1[i] = analogRead(Moisture_Input1); 
 // Take reading from Soil Moisture Probe.
 
 
 Av_Moist_1 = Av_Moist_1 + Moist_Vals_1[i];
}
 
 Moist_Read_1 = (Av_Moist_1/4);
 
 return Moist_Read_1;

 }
 
  int S_Moisture_Rd_2(){
   
   int i;
   int Moist_Read_2;
   int Moist_Vals_2[4];
   int Av_Moist_2 = 0;   
 //Variable to hold value from Moisture Probe


for (i=0;i<4;i++){
 Moist_Vals_2[i] = analogRead(Moisture_Input2); 
 // Take reading from Soil Moisture Probe.
 
 
 Av_Moist_2 = Av_Moist_2 + Moist_Vals_2[i];
}
 
 Moist_Read_2 = (Av_Moist_2/4);
 
 return Moist_Read_2;

 }
 
 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("Percent: ");
 lcd.print(Percentage_S_Moist_0,DEC);
 lcd.print("%");
}
 
  void LCDprintSoilMoist1(void) {

 lcd.clear();
 lcd.print("SoilMoisture");
 lcd.setCursor(0,1);
 lcd.print("Percent: ");
 lcd.print(Percentage_S_Moist_1,DEC);
 lcd.print("%");
}
 
  void LCDprintSoilMoist2(void) {

 lcd.clear();
 lcd.print("SoilMoisture");
 lcd.setCursor(0,1);
 lcd.print("Percent: ");
 lcd.print(Percentage_S_Moist_2,DEC);
 lcd.print("%");
}
 
 
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){
       
       lcdMode++;
       
         if (lcdMode == 4)
           {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)
              {LCDprintSoilMoist1();}
         
         if (lcdMode == 3)     
              {LCDprintSoilMoist2();}
}
}
 
 void loop (void){
 
   // SoilMoisture sequence
   
   Sprb_i_strt ();
   
   if( (millis() - Sprb_timer) >= Cpt_Efcts )
        {
   S_Moisture_Rd_0();
   S_Moisture_Rd_1();
   S_Moisture_Rd_2();
        }
         
   Moist_Val_0 = S_Moisture_Rd_0();   
   Moist_Val_1 = S_Moisture_Rd_1();
   Moist_Val_2 = S_Moisture_Rd_2();       
       
   
 // Stop current through Probe 
   digitalWrite(dcSource,LOW);
 
  Percentage_S_Moist_0 =((Moist_Val_0/1000.00)*100.00);
  Percentage_S_Moist_1 =((Moist_Val_1/1000.00)*100.00);
  Percentage_S_Moist_2 =((Moist_Val_2/1000.00)*100.00);
     
   // End of Soil Moisture Sequence
 
 
 SwapMenu ();
 
 /*
 Serial.print("Moist0 = ");
 Serial.println (Moist_Val_0);
 
 Serial.print("Moist1 = ");
 Serial.println (Moist_Val_1);
 
 Serial.print("Moist2 = ");
 Serial.println (Moist_Val_2);

 
 Serial.print("% Moist0");
 Serial.println (Percentage_S_Moist_0);
 
 Serial.print("% Moist1");
 Serial.println (Percentage_S_Moist_1);
 
 Serial.print("% Moist2");
 Serial.println (Percentage_S_Moist_2);
  delay(1000);
*/
}
 
Logged


tz
.....

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The only difference between your three functions is the pin that they read from. Why won't you create one function that takes a pin number as the argument?

Why do the functions return a global value?

Why do you have 3 nearly identical functions to print to the LCD?
Why do you have 3 nearly identical functions to print to the LCD?
Why do you have 3 nearly identical functions to print to the LCD?

Code:
   if( (millis() - Sprb_timer) >= Cpt_Efcts )
        {
   S_Moisture_Rd_0();
   S_Moisture_Rd_1();
   S_Moisture_Rd_2();
        }
         
   Moist_Val_0 = S_Moisture_Rd_0();   
   Moist_Val_1 = S_Moisture_Rd_1();
   Moist_Val_2 = S_Moisture_Rd_2();       
If it's time to read, read. If not, read, anyway. I see. Ummm. No, I don't.
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is the problem with the reading of the moisture sensors, or is the problem with the display of the result on the LCD.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is the problem with the reading of the moisture sensors, or is the problem with the display of the result on the LCD.
I don't know. You are the one with the hardware. I see no reason for you to expect us to wade through 3 times as much code as is needed, though, to help you solve a problem. We ask you to post the minimum code needed to reproduce a problem. Three nearly identical functions, twice, is no where near the minimum code required to reproduce the problem and is three times as much work to implement a fix, when one is found.
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is the problem with the reading of the moisture sensors, or is the problem with the display of the result on the LCD.
I don't know. You are the one with the hardware.  ... ... ...

No,  actually I'm not.

Question was intended for OP
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Question was intended for OP
Without quoting anything, or using the @OP notation, that was hard to see, without paying close attention. Which, clearly, I wasn't.
Logged

Mauritius
Offline Offline
Jr. Member
**
Karma: 2
Posts: 95
Learning Never Ends . . .
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Is the problem with the reading of the moisture sensors, or is the problem with the display of the result on the LCD.

The problem is with the result on LCD display, the characters are kind of "flashy" . . seems as if they want to change but are not.

@ paulS . . i did get the message though . . .   
Logged


tz
.....

Mauritius
Offline Offline
Jr. Member
**
Karma: 2
Posts: 95
Learning Never Ends . . .
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The only difference between your three functions is the pin that they read from. Why won't you create one function that takes a pin number as the argument?
Can anyone help towards it, i did try but failed as such.

Quote
Why do the functions return a global value?
If i read directly from  S_Moisture_Rd_N(); i do not get any valuable reading, thats what i observed.

Quote
Why do you have 3 nearly identical functions to print to the LCD?

Im displaying each reading of percentage moisture of the 3 plant pots!

Code:
   if( (millis() - Sprb_timer) >= Cpt_Efcts )
        {
   S_Moisture_Rd_0();
   S_Moisture_Rd_1();
   S_Moisture_Rd_2();
        }
         
   Moist_Val_0 = S_Moisture_Rd_0();   
   Moist_Val_1 = S_Moisture_Rd_1();
   Moist_Val_2 = S_Moisture_Rd_2();       
If it's time to read, read. If not, read, anyway. I see. Ummm. No, I don't.
[/quote]

But don't i need to return the reading of the function S_Moisture_Rd to a global variable ?? if i do not, i do not get any results . . ??

thanks
Logged


tz
.....

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The following is a very quick and dirty modification of your code in order to suggest changes that could be made to your code to simplify and condense it in order to make it more readable and maintainable.

It has not been compiled and can probably simplified even more.

Again it is suggestive and not necessarily usable code ...

Code:
#define ENTRIES(ARRAY)          (sizeof(ARRAY) / sizeof(ARRAY[0]))

/*Declaration of I/Os */

const uint8_t Moisture_Input0   = 1;
const uint8_t Moisture_Input1   = 2;
const uint8_t Moisture_Input2   = 3;

const uint8_t SwapButton        = 12;

const uint8_t dcSource          = 13;

const unsigned long Cpt_Efcts   = 1000UL;

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

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

/*
   LCD Connections:
   rs (LCD pin 4) to Arduino pin 11
   rw (LCD pin 5) to Arduino pin 10
   enable (LCD pin 6) to Arduino pin 9
   LCD pin 15 to Arduino pin 4
   LCD pins d4, d5, d6, d7 to Arduino pins 8, 7, 6, 5
 */
/*--------------------------------------------------------------*/

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


/* Declaration of Variables*/

unsigned long   Sprb_timer;
uint8_t         buttonState;
uint8_t         modeDisplay;

double Percentage_S_Moist_0;
double Percentage_S_Moist_1;
double Percentage_S_Moist_2;

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

void setup()
{
    Serial.begin(9600);

    lcd.begin(16, 2);               // rows, columns. use 16, 2 for a 16x2 LCD, etc.
    lcd.clear();                    // start with a blank screen
    lcd.setCursor(0, 0);            // set cursor to column 0, row 0

    pinMode(dcSource,   OUTPUT);

    pinMode(SwapButton, INPUT);     // Define Digital Pin as Input
    buttonState = digitalRead(SwapButton);

    Sprb_timer = millis();          // Returns the milliseconds ellapsed since Prgm started
}

void Sprb_i_stop()
{
    // Stop current through Probe
    digitalWrite(dcSource, LOW);
}

void Sprb_i_strt()
{
    digitalWrite(dcSource, HIGH);

    // Remember when Fwd direction of current has been set.
    Sprb_timer = millis();
}


int averageSensorSamples(const uint8_t pinAnalogSensor, const int count)
{
    int samples[count];
    int sum = 0;

    for ( int i = ENTRIES(samples); i--; )
    {
        // Take reading from Soil Moisture Probe.

        samples[i] = analogRead(pinAnalogSensor);
        sum += samples[i];
    }

    return (sum / ENTRIES(samples));
}

void LCDprintMenu()
{
    lcd.clear();

    lcd.print("Tazlim -- GrnHse");

    lcd.setCursor(0, 1);
    lcd.print("  EngineerinG");
}

void LCDprintSoilMoist(const double value)
{
    lcd.clear();

    lcd.print("SoilMoisture");

    lcd.setCursor(0, 1);

    lcd.print("Percent: ");
    lcd.print(value, DEC);
    lcd.print("%");
}


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

    int val = digitalRead(SwapButton);

    delay(10);

    // Read Input again to check for bounces
    if ( val == digitalRead(SwapButton) )
    {
        if ( val != buttonState )
        {
            if ( val == LOW )
            {
                modeDisplay = ++modeDisplay % 4;
            }

            buttonState = val;  // Save the new state in the Variable for monitoring further button press (Continuous program loop).
        }

        switch ( modeDisplay )
        {
            case 0: LCDprintMenu();                             break;  // Display Intro Display on the LCD.
            case 1: LCDprintSoilMoist(Percentage_S_Moist_0);    break;  // Display SoilMoist Reading in %
            case 2: LCDprintSoilMoist(Percentage_S_Moist_1);    break;
            case 3: LCDprintSoilMoist(Percentage_S_Moist_2);    break;
        }
    }
}

void loop()
{
    // SoilMoisture sequence

    if ( (millis() - Sprb_timer) >= Cpt_Efcts )
    {
        Sprb_i_strt();

            int Moist_Val_0 = averageSensorSamples(Moisture_Input0, 4);
            int Moist_Val_1 = averageSensorSamples(Moisture_Input1, 4);
            int Moist_Val_2 = averageSensorSamples(Moisture_Input2, 4);

        Sprb_i_stop();
   
        Percentage_S_Moist_0 = ((Moist_Val_0 / 1000.00) * 100.00);
        Percentage_S_Moist_1 = ((Moist_Val_1 / 1000.00) * 100.00);
        Percentage_S_Moist_2 = ((Moist_Val_2 / 1000.00) * 100.00);
   
        SwapMenu();
    }

    delay(1000UL);
}
Logged

Pages: [1]   Go Up
Jump to: