Arduino: text on lcd according to buttons pressed (slow running code)

4,523 views
Skip to first unread message

gazz

unread,
Jan 27, 2013, 4:08:31 PM1/27/13
to notti...@googlegroups.com
I recently bought an arduino uno from the vending machine, as i was using a minimus and making it accept arduino code, which it did nicely, but some problems with the lcd sketch on the minimus made me get a proper arduino and test things out on that for now, and i'll figure out 'porting' it to the minimus later, 

Anyway, it's that bus ticket machine project of mine again, i'm starting with the basics and adding functions to them as i go along, 
so far i have my lcd working, and i have 4 standard push buttons connected (will be a 9x4 matrix of buttons later) 
i am running a simple sketch that checks which button has been pressed, then displays the appropriate text on the lcd, 

my script so far is : 

#include <LiquidCrystal.h>

int buttonPin1= 6;    //sets pins buttons are connected to
int buttonPin2= 7;
int buttonPin3= 8;
int buttonPin4= 9;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //sets pins lcd is connected to, running in 4 bit mode, RW pin tied to gnd

void setup()
{
  pinMode(buttonPin1, INPUT);  //assigns button pins as inputs
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);

  digitalWrite(buttonPin1, LOW);  //sets internal pulldown resistors
  digitalWrite(buttonPin2, LOW);
  digitalWrite(buttonPin3, LOW);
  digitalWrite(buttonPin4, LOW);

  lcd.begin(20, 1);   //lcd size
  lcd.print("Der Omnibussimulator"); //welcome text
  delay(2000);  //displays for 2 secs
  lcd.clear();  //clears display
}


void loop(){
  int buttonState1 = digitalRead(buttonPin1);  
  int buttonState2 = digitalRead(buttonPin2);
  int buttonState3 = digitalRead(buttonPin3);
  int buttonState4 = digitalRead(buttonPin4);

  if(buttonState1== HIGH){  //button pressed
    lcd.setCursor(0,0);  //clears display
    lcd.print("   >>Belegdruck<<   ");  //German for 'printing ticket'

    delay(1000);  //display it for 1 sec
    lcd.clear();  //then blank the lcd
  }

  else if (buttonState2==HIGH){
    lcd.setCursor(0,0);
    lcd.print("Fahrschein Nor  2.70");  //German for single ticket, stays on the lcd untill another buttons is pressed
  }
  else if(buttonState3== HIGH){
    lcd.setCursor(0,0);
    lcd.print("Kurzstrecke     1.70");  //German for short trip ticekt, stays on lcd untill another button is presed
  }  
  else if (buttonState4== HIGH){
    lcd.setCursor(0,0);
    lcd.print("Tageskarte      9.00");  //German for day ticket, stays on display untill another button is pressed
  }  
}

This works, BUT, if i press button 4, it displays the message, if i press button 3 or 2, it instantly displays the relevant message, and if i then press button 1, it displays the printing message, then clears the screen, 

However, if i press button 2, it instantly displays that message, but press say button 4, it will not display the new message for about 4 seconds, same with button 3 etc,  so going up the message structure from the code is instant (4,3,2,1), but going down the message structure (1,2,3,4) is delayed, 

Also the final print message would only stay on screen for a second, but stays on for 4 to 5 seconds, 

Can someone tell me what i've done wrong? i assume it is because the loop is constantly running to check buttons, 

I will eventually have 10 or 11 buttons with text assigned to them, so i can only imagine if i carry on with this script, the delay will just get longer and longer, 

Gazz

jfowkes

unread,
Jan 27, 2013, 4:20:51 PM1/27/13
to notti...@googlegroups.com
Is there any debouncing on your input pins? If you don't know what that means, try reading this, or search for "debouncing". You can probably find lots of Arduino-specific examples to help.

Something quick you can do is add a 100ms delay at the very end of your loop() function and see if that helps.

Michael Erskine

unread,
Jan 27, 2013, 4:24:16 PM1/27/13
to notti...@googlegroups.com
Only button 1 clears the display - read your code!

Ian Dickinson

unread,
Jan 27, 2013, 4:25:48 PM1/27/13
to notti...@googlegroups.com
Gazz,
I think your problem may be DigitalWrite(buttonPinX,LOW);. This simply turns off the internal 20K pull up. It doesn't enable a pull down. I suspect your switches are between 5V and the inputs, and there is enough voltage remaining on the pin after you've released the switch to keep it HIGH for longer than you expected.
An external pull down will help. Alternatively consider, connecting the switches between ground and the input pins.
Hope this helps.
Ian

DPS - LWK

unread,
Jan 27, 2013, 4:27:02 PM1/27/13
to notti...@googlegroups.com
James is correct your going to need some de-bounce in there

Also
Arduino does not have internal pull down only pull up, this line and the following 3 let the input float

digitalWrite(buttonPin1, LOW);  //sets internal pulldown resistors
how do you have the switches wire externally?

Matt

--
 
 

Gazz

unread,
Jan 27, 2013, 4:40:01 PM1/27/13
to notti...@googlegroups.com
'Doh,
I can't believe i made such a silly mistake, my fault for using bits of code
i find on the internet,

I've changed the inputs to high, enabling the pull up resistor, and changed
the switches to be detected when they go low, and it works, nice and
responsive,

Thanks for sorting that out for me guys,

Gazz

--------------------------------------------------
From: "DPS - LWK" <dps...@gmail.com>
Sent: Sunday, January 27, 2013 9:27 PM
To: <notti...@googlegroups.com>
Subject: Re: [Nottinghack] Re: Arduino: text on lcd according to buttons
pressed (slow running code)

> James is correct your going to need some de-bounce in there
>
> Also
> Arduino does not have internal pull down only pull up, this line and the
> following 3 let the input float
> digitalWrite(buttonPin1, LOW); //sets internal pulldown resistors
> how do you have the switches wire externally?
>
> Matt
>
> On 27 January 2013 21:20, jfowkes <james...@gmail.com> wrote:
>
>> Is there any debouncing on your input pins? If you don't know what that
>> means, try reading
>> this<http://www.engscope.com/pic-example-codes/basic-io-button-debounce/>,

Gazz

unread,
Jan 27, 2013, 4:43:13 PM1/27/13
to notti...@googlegroups.com
Yup, that's how i want it,

the 3 ticket selection buttons display the ticket type and price, and they
stay on the screen until i either press the cancel button (not implemented
yet) or press the print button, where it displays the printing ticket text,
then clears (it will actually flash the 'printing ticket' text a few times
before clearing, but i removed that bit of code in'case the delays were
causing my problems,

Gazz

--------------------------------------------------
From: "Michael Erskine" <mse...@googlemail.com>
Sent: Sunday, January 27, 2013 9:24 PM
To: <notti...@googlegroups.com>
Subject: Re: [Nottinghack] Re: Arduino: text on lcd according to buttons
pressed (slow running code)

Gazz

unread,
Jan 27, 2013, 7:14:31 PM1/27/13
to notti...@googlegroups.com
Can i pick the collectives brains once more :)

i have the lcd text responding instantly to the buttons, and untill my
matrix keypads arrive in the post (maplins sell the 3x4 ones for �2.99, but
have discontinued them, however they had over 40 at head office, so i got a
couple on order) i'd like to make it so the print ticket button only
displays it's text after a ticket selection button has been pressed.


my script is now:

#include <LiquidCrystal.h>

int buttonPin1= 6; //sets pins buttons are connected to
int buttonPin2= 7;
int buttonPin3= 8;
int buttonPin4= 9;
int buttonPin5= 13;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //sets pins lcd is connected to,
running in 4 bit mode, RW pin tied to gnd

void setup()
{
pinMode(buttonPin1, INPUT); //assigns button pins as inputs
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);

digitalWrite(buttonPin1, HIGH); //sets internal pulldown resistors
digitalWrite(buttonPin2, HIGH);
digitalWrite(buttonPin3, HIGH);
digitalWrite(buttonPin4, HIGH);
digitalWrite(buttonPin5, HIGH);

lcd.begin(20, 1); //lcd size
lcd.print("Der Omnibussimulator"); //welcome text
delay(2000); //displays for 2 secs
lcd.clear(); //clears display
}


void loop(){
int buttonState1 = digitalRead(buttonPin1);
int buttonState2 = digitalRead(buttonPin2);
int buttonState3 = digitalRead(buttonPin3);
int buttonState4 = digitalRead(buttonPin4);
int buttonState5 = digitalRead(buttonPin5);


if (buttonState1== LOW){
lcd.setCursor(0,0);
lcd.print("Fahrschein Nor 2.70"); //German for single ticket, stays on
the lcd untill another buttons is pressed
}
else if(buttonState2== LOW){
lcd.setCursor(0,0);
lcd.print("Kurzstrecke 1.70"); //German for short trip ticekt,
stays on lcd untill another button is presed
}
else if (buttonState3== LOW){
lcd.setCursor(0,0);
lcd.print("Tageskarte 9.00"); //German for day ticket, stays on
display untill another button is pressed
}
else if(buttonState4== LOW){ //button pressed
lcd.setCursor(0,0); //clears display
lcd.print(" >>Belegdruck<< "); //German for 'printing ticket'
delay(350);
lcd.noDisplay();
delay(250);
lcd.display();
delay(350);
lcd.noDisplay();
delay(250);
lcd.display();
delay(550);
lcd.clear();
}
else if(buttonState5== LOW){
lcd.clear();
}
}

So i want button 4 to only respond after either button 1, 2 or 3 have been
pressed,
button 5 is the cancel button btw,

i think i need to change one of the 'else if' commands, but need to be able
to still use the cancel button or select another ticket button,

DPS - LWK

unread,
Jan 27, 2013, 7:19:49 PM1/27/13
to notti...@googlegroups.com
in setup()
byte selectionMade = Fasle;

in if's for button 1,2,3
selectionMade =Trure;
to the code block

in if for button 5
selectionMade = False;
to the code block


in if for button 4 for add
&& selectionMade == True
to the condtion
might also want to clear the flag in the code block to reset

--
You received this message because you are subscribed to the Google Groups "Nottingham Hackspace - Nottinghack" group.
To unsubscribe from this group, send email to nottinghack+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



jfowkes

unread,
Jan 27, 2013, 7:22:54 PM1/27/13
to notti...@googlegroups.com
You want to store the state of the program in a variable. This could be a boolean for two states, or an integer for more than one. You can set the variable at certain events, and check it in your if blocks.

I'll let you see if you can get anywhere with that before posting more.


On Sunday, January 27, 2013 9:08:31 PM UTC, gazz wrote:

Gazz

unread,
Jan 27, 2013, 8:53:12 PM1/27/13
to notti...@googlegroups.com
i think i understand what's supposed to happen,

But it will not compile with the 'byte selectionMade = false;' in the setup
part of the sketch, "'selectionMade' was not declared in this scope
" is the error.
If i put it in the loop part, it will compile, but i don't think it's
supposed to work like that.

I then put
" if (buttonState1== LOW) {
selectionMade =true;
lcd.setCursor(0,0);"
etc for buttons 1, 2 and 3.

and
" else if(buttonState5== LOW){
selectionMade =false;
lcd.clear();"
for button 5,

Then i put
" else if
(buttonState4== LOW && selectionMade == true){"
for button 4,

it compiles, but only with the byte selectionMade =false; bit in the loop,

result is button 4 dosent respond at all :(

Stumped am I :)
--------------------------------------------------
From: "DPS - LWK" <dps...@gmail.com>
Sent: Monday, January 28, 2013 12:19 AM
To: <notti...@googlegroups.com>
Subject: Re: SPAM-LOW: Re: [Nottinghack] Re: Arduino: text on lcd according
to buttons pressed (slow running code)

gazz

unread,
Jan 27, 2013, 9:10:33 PM1/27/13
to notti...@googlegroups.com
oops, i think i figured it out, 
i needed to put the  byte selectionMade = false; bit before the setup part of the script, 

it now works as it should... first time after a reset, or after pressing the cancel button, 
so i need to figure out how to "clear the flag in the code block to reset" bit, 

i'm really pleased with the way you guys are helping me, rather than just taking my code, altering it and handing it back working, you are making me work things out for my self with hints, so i am learning something, 

Cheers,

Gazz

DPS - LWK

unread,
Jan 28, 2013, 12:16:30 AM1/28/13
to notti...@googlegroups.com
my bad, indeed the variable declaration should have been global.

Matt

--
You received this message because you are subscribed to the Google Groups "Nottingham Hackspace - Nottinghack" group.
To unsubscribe from this group, send email to nottinghack...@googlegroups.com.

Gazz

unread,
Jan 28, 2013, 6:58:02 AM1/28/13
to notti...@googlegroups.com
Most excelent, cheers,

i also put a 'selectionMade = false;' at the end of the button 4 code, is
that what you ment by 'clear the flag in the code block to reset'??

But it seem's to work exactly as i planned it,
all simple stuff to people with a programmers mind, but a steep learning
curve for me, but hey, it's fun and the sense of achievement when it works
is great,

Gazz

--------------------------------------------------
From: "DPS - LWK" <dps...@gmail.com>
Sent: Monday, January 28, 2013 5:16 AM
To: <notti...@googlegroups.com>
Subject: Re: SPAM-LOW: Re: [Nottinghack] Re: Arduino: text on lcd according
to buttons pressed (slow running code)

>>> ------------------------------**--------------------
> To unsubscribe from this group and stop receiving emails from it, send an

Syed Burhan Mansoor

unread,
Apr 20, 2017, 12:51:57 PM4/20/17
to Nottingham Hackspace - Nottinghack
CAN I KNOW THE circuit diagram of this code that where did u add button the board and other things.... it will be very helpful thanks in advance.....
 
Reply all
Reply to author
Forward
0 new messages