Go Down

Topic: **HELP** changing pages on a 16x2 LCD with a button (Read 1 time) previous topic - next topic

Joy

I am connecting button in the right way...

Its isnt that the code is not working with the button...
What is happening is that when I press the button once from Case 1 it changes to Case 2 then when I release the button again it changes from Case 2 to Case 3

dxw00d

#16
Feb 24, 2012, 10:04 am Last Edit: Feb 24, 2012, 10:06 am by dxw00d Reason: 1
Code: [Select]
 currstate = digitalRead(switchPin);
 if (currstate != prevstate){


Your code responds to a transition in either direction, LOW -> HIGH and HIGH -> LOW, so reacts when you press the switch and when you release it. You only want to respond one or the other. So, try something like this:

Code: [Select]
 currstate = digitalRead(switchPin);
 if ((currstate != prevstate) && (currstate == HIGH)){


Assuming currstate goes HIGH when the switch is pressed.

ernope

Hi
how to, clean display between the case 1 and 2, 2 and 3?
can somebody help me with some tips?

UKHeliBob

Clean the display ?
How about lcd.clear() - is that clean enough ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

ernope

Thanks for the tip, I'm perhaps a newbie, but I know what clear (), is, I've tried it in slightly different locations in the code, without quite get it to work.

This is the modified code I've tried.

Quote

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

int switchPin = 13;   // momentary switch on 8, other side connected to ground
int Display = 0;
int prevstate = HIGH;
int currstate;

void setup()
{
  lcd.begin(16, 2);
  pinMode(switchPin, INPUT);
  digitalWrite(switchPin, HIGH);      // turn on pullup resistor
}

void loop()
{
  currstate = digitalRead(switchPin);
  if (currstate != prevstate){ 
                

    Display = Display + 1;
    if(Display > 3){
      lcd.clear();
      
      Display = 1;
    }
  }
    
    switch (Display) {
      case 1: {
        lcd.setCursor(0, 0);
        lcd.print("joy");
        break;
        
      }
       
      case 2: {
        lcd.clear();
        lcd.setCursor(5, 0);
        lcd.print("raha");
        break;

      }
        
       case 3: {
         lcd.clear();
         lcd.setCursor(1, 1);
         lcd.print("hkfl");
         break;
       
       }
  }
   prevstate = currstate;
}


PaulS

Quote
This is the modified code I've tried.

And the results are known only to you.

ernope

code was not working for me.
when I use the original code, case 1 shows what is in case 1, it is ok, but when I press the button again, the case 1 and 2 the display, when I again press the button, the display case 1.2 and 3. I want help to get cleaner display, a display that only shows:

case 1 shows only contents in case 1
case 2 shows only contents in case 2
case 3 shows only contents in case 3

can anyone help me modify the code?

PaulS

As your code is written, Display will be increment when you press the switch and again when you release it. Is that what you intended?

Serial.print()ing the value in Display, to confirm that it matches what you expect would be useful. Clearing the screen regardless of whether Display is greater than 3 is probably necessary.

ernope

the display switches each time I press the button, this is right, the problem is that the value in case 1 remains on the display when I click on this button to switch to case 2, I want to clean the display for each case.

PaulS

Quote
the display switches each time I press the button, this is right

It should switch twice - once when you press and once when you release - since you are incrementing the counter each time the state is not the same as the previous state. The states are not the same when the switch changes from released to pressed AND when the switch changes from pressed to released.

You could have an lcd.clear() call in each case.


liudr

This code doesn't debounce buttons so it is unreliable even if you make the change to only trigger on release (I typically trigger on depress, which is a bit quicker reaction).

Go Up