Pages: [1]   Go Down
Author Topic: LCD do not update information.  (Read 476 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 to all,

I just modified some codes concerning soil moisture measurement, i already implement  the hardware part as well and incorporate an LCD screen display 16 x 2, to print the soil moisture in percentage.
I added a push button in my sketch to enable myself to swap menu on the LCD display. For the time being it displays the soil moisture % and the other menu display hello world, later on i shall make it display some reading for light intensity.

My issue is before i implemented this swap menu function the reading of % moisture would display correctly and the value will be updated after a delay of 1000.

Later when i add this menu swap function my value of percentage do not update until i swap menu to 'Hello World" and back to soilMoist % reading.

Any comments please. My apologies for the code format as i am encountering an error while copying for forum.

The code is as follows:
------------------------------------------------------------------------------------------------------------
[ code ]
/*  Soil Moisture measurement v0.1
 Alternate DC current circuit
 Callibrated at 100% with water (H20)
 
 Started by Mike Rice, October 14, 2009
 Modified by M.A. de Pablo, October 17, 2009
 Modified and new adaptation by Tazlim Goolap, January 1, 2013
 
 Circuit:
 To connect two Copper Wire and a 100 KOhms resistor as shown:
 
        digital 13---*
                  |
                  \
                  /
                  \ R1(100K Ohms)
                  /
                  |
                  |
        analog 1----*
                  |
                  |
                  *----> nail 1
                
                  *----> nail 2
                  |
                  | R2 (100 Ohms)
                  |
        digital 12---*
 */

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


/* Declaration of I/Os */

  #define moisture_input 1      // Analog Input pin 1 connected to collect 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
  #define SwapButton 4          // Push Button connected to digital pin 4. Purpose : To sawp menu on LCD screen.

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


/* Library Code to be included */

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


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


/* Declaration Of Variables */

   int moisture;                // Analogical value obtained from the experiment
   double PercentMoisture;      // Varible to store % Percentage Soil Moisture Value
   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 swap between LCD menu.


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

/*
 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



void setup (void) {
  
  Serial.begin(115200);                       // Set up Serial Communication at 9600 bps.
  
  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
}


int SoilMoisture(){
  int reading;                              // Variable to hold value from Moisture input.
  
 
  // 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 a reading
  reading=analogRead(moisture_input);

  // 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);

  return reading;
}

void LCDprintSoilMoist(void) {

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



void LCDprintMenu (void) {
  lcd.clear();
  lcd.setCursor (0,0);
  lcd.print("Hello World");
}

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


void loop (void) {
 
  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;
      LCDprintMenu();    
        }
        else{
          lcdMode = 0;
          LCDprintSoilMoist();
        }
      }
    }
  buttonState = val; // Save the new state in the Variable for monitoring further button press (Continuous program loop).
}
 
  delay (1000);
  
  moisture=SoilMoisture(); // assign the result of SoilMoisture() to the global variable 'moisture'
  Serial.print("Soil moisture: ");
  Serial.print(moisture); // print the analogical measurement of the experiment
  
  PercentMoisture = ((moisture/950.00)*100.00); // Derivation of Soil Moisture in %
  Serial.print(" Percentage Soil Moisture = ");
  Serial.print(PercentMoisture);
    
  Serial.println();
  delay(1000);

  Serial.print ("buttonState");
  Serial.println (buttonState);
  
}

[ /code ]

  
  

__________________________________________________________________________________
« Last Edit: January 04, 2013, 01:33:01 pm by taz3m » Logged


tz
.....

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12577
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please put your code inside [ code ] [ /code ] tags to stop the forum software from mangling it.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

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

The decision, in loop, about what to do needs to be separated from the do something part.

Right now, you only do something if the switch is pressed. That should define/change what to do.

On every pass through loop, you should call one of the two LCDxxx functions.
Logged

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

Hi,

As far as i understand,should i use a call function ?

Will that be in the Loop part ?

 smiley  an example will make things more clearer if possible. .   smiley-draw

Thanks for the support . .

Rgds

taz ..

Logged


tz
.....

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

Code:
void loop (void)
{ // Moved down here where it belongs
  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;
          LCDprintMenu();   
        }
        else
        {
          lcdMode = 0;
          LCDprintSoilMoist();
        }
      }
    }
  buttonState = val;
}

Organized this way, you can see that you set lcdMode AND call a function.

Just set the mode:
Code:
        if (lcdMode == 0)
        {
          lcdMode = 1;
        }
        else
        {
          lcdMode = 0;
        }

Then, at the end of loop, add:
Code:
        if (lcdMode == 0)
        {
          LCDprintMenu();   
        }
        else
        {
          LCDprintSoilMoist();
        }
to actually make use of the mode.

The mode defines what happens on each pass through loop, but does not change on each pass through loop.
Logged

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

Hello ,

That worked perfect .. Thanks ...

 smiley


taz  . . .
Logged


tz
.....

Pages: [1]   Go Up
Jump to: