Go Down

Topic: Coding w/o delay func! (Read 405 times) previous topic - next topic

PaulS

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.

taz3m

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 . . . 
tz
.....

taz3m

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: [Select]
   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
tz
.....

lloyddean

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: [Select]

#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);
}


Go Up