Pages: 1 [2]   Go Down
Author Topic: **HELP** changing pages on a 16x2 LCD with a button  (Read 1266 times)
0 Members and 1 Guest are viewing this topic.
INDIA
Offline Offline
Sr. Member
****
Karma: 0
Posts: 373
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 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:
 currstate = digitalRead(switchPin);
  if ((currstate != prevstate) && (currstate == HIGH)){

Assuming currstate goes HIGH when the switch is pressed.
« Last Edit: February 24, 2012, 04:06:22 am by dxw00d » Logged

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3464
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Clean the display ?
How about lcd.clear() - is that clean enough ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

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

Quote
This is the modified code I've tried.
And the results are known only to you.
Logged

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

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

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

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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.

Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 64
Posts: 6893
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged


Pages: 1 [2]   Go Up
Jump to: