Pages: [1]   Go Down
Author Topic: Message display - help  (Read 994 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I am trying to build a display so that upon an input being activated (either high or low i will receive a message on my lcd display) so far i have this working for the most part with teh following code:
Code:
/*

  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 * Pushbutton 1 to +5V & pin 7 ( also a 10K resistor to ground)
 * Pushbutton 2 to +5V & pin 8 ( also a 10K resistor to ground)

*/

// include the library code:
#include <LiquidCrystal.h>
const int buttonPin1 = 7;     // the number of the pushbutton pin
const int buttonPin2 = 8;     // the number of the pushbutton pin
const int buttonPin3 = 9;     // the number of the pushbutton pin
const int buttonPin4 = 10;     // the number of the pushbutton pin
// variables will change:

int buttonState1 = 0;         // variable for reading the pushbutton1 status
int buttonState2 = 0;         // variable for reading the pushbutton2 status
int buttonState3 = 0;         // variable for reading the pushbutton1 status
int buttonState4 = 0;         // variable for reading the pushbutton2 status
int val = 0;     // variable for reading the pin status
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);



void setup() {

}

void loop() {
  lcd.clear();  // Clear the LCD
 
  // check if the pushbutton is pressed. 
  buttonState1 = digitalRead(buttonPin1);  // read the state of the pushbutton1 value
  buttonState2 = digitalRead(buttonPin2);  // read the state of the pushbutton2 value
  buttonState3 = digitalRead(buttonPin3);  // read the state of the pushbutton1 value
  buttonState4 = digitalRead(buttonPin4);  // read the state of the pushbutton2 value
  // if it is, the buttonState is HIGH:
 
  if (buttonState1 == HIGH) {     // If button 1 is high 
    lcd.setCursor(0, 0);  // Set the cursor
  lcd.print("INPUT 1 HIGH");  // Display the message
  } 
 
  if (buttonState2 == HIGH) {     // If button 2 is high 
    lcd.setCursor(0, 0);  // Set the cursor
  lcd.print("BUTTON 2 ON");  // Display the message
  }
 
  if (buttonState3 == LOW) {     // If button 3 is hlow 
    lcd.setCursor(0, 0);  // Set the cursor
  lcd.print("INPUT 2 LOW");  // Display the message
  }
 
  if (buttonState4 == LOW) {     // If button 4 is low 
    lcd.setCursor(0, 0);  // Set the cursor
  lcd.print("BUTTON 4 PRESS");  // Display the message
  }
 
    else {
    lcd.setCursor(0, 0);  // Set the cursor
  lcd.print("ALL BUTTONS OFF");  // Display the message
}
 
 delay(1000);  // Wait 1 second before re-checking button states
}

my problem is all i can get to display is the idle message (ALL BUTTONS OFF) or the status of input 4, none of the other inputs will display their respective message.

I have managed to get arround this by removing the else function so i just have a blank screen when no inputs are active then individually all inputs display as they should however if multiple inputs are activated at one time it will only display the highest input number and again i would like to be able to still include the 'idle screen' if no inputs are active.

is there a way i can still include idle 'ALL BUTTONS OFF' screen and if more than one input is active cycle the messages on teh screen i.e. 'input 1' > 'input 2' > 'input 1' etc?

Thanks again,

Phil.
Logged

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

You have 4 if tests, and one else case. The else case is invoked only if the last if test is false.

You need to use else ifs:
Code:
  if (buttonState1 == HIGH)
  {     // If button 1 is high 
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("INPUT 1 HIGH");  // Display the message
  } 
  else if (buttonState2 == HIGH)
  {     // If button 2 is high 
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("BUTTON 2 ON");  // Display the message
  }
  else if (buttonState3 == LOW)
  {     // If button 3 is hlow 
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("INPUT 2 LOW");  // Display the message
  }
  else if (buttonState4 == LOW)
  {     // If button 4 is low 
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("BUTTON 4 PRESS");  // Display the message
  }
  else
  {
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("ALL BUTTONS OFF");  // Display the message
  }

Why some buttons are considered pressed when the state is HIGH and others are considered pressed when the state is low is a mystery, though.
Logged

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

Thanks for your reply.

i have loaded the code and now i can display individually the inputs, the reason for some being high is because inputs are being activated by vairos sources, either pressure or temperature switches in en existing system, i am simply using the switches to simulate the inputs.

my code now stands as

Code:
/*

  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 * Pushbutton 1 to +5V & pin 7 ( also a 10K resistor to ground)
 * Pushbutton 2 to +5V & pin 8 ( also a 10K resistor to ground)

*/

// include the library code:
#include <LiquidCrystal.h>
const int buttonPin1 = 7;     // the number of the pushbutton pin
const int buttonPin2 = 8;     // the number of the pushbutton pin
const int buttonPin3 = 9;     // the number of the pushbutton pin
const int buttonPin4 = 10;     // the number of the pushbutton pin
// variables will change:

int buttonState1 = 0;         // variable for reading the pushbutton1 status
int buttonState2 = 0;         // variable for reading the pushbutton2 status
int buttonState3 = 0;         // variable for reading the pushbutton1 status
int buttonState4 = 0;         // variable for reading the pushbutton2 status
int val = 0;     // variable for reading the pin status
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);



void setup() {

}

void loop() {
  lcd.clear();  // Clear the LCD
 
  // check if the pushbutton is pressed. 
  buttonState1 = digitalRead(buttonPin1);  // read the state of the pushbutton1 value
  buttonState2 = digitalRead(buttonPin2);  // read the state of the pushbutton2 value
  buttonState3 = digitalRead(buttonPin3);  // read the state of the pushbutton1 value
  buttonState4 = digitalRead(buttonPin4);  // read the state of the pushbutton2 value
  // if it is, the buttonState is HIGH:
 
   if (buttonState1 == HIGH)
  {     // If button 1 is high 
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("INPUT 1 HIGH");  // Display the message
  } 
  else if (buttonState2 == HIGH)
  {     // If button 2 is high 
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("BUTTON 2 ON");  // Display the message
  }
  else if (buttonState3 == LOW)
  {     // If button 3 is hlow 
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("INPUT 2 LOW");  // Display the message
  }
  else if (buttonState4 == LOW)
  {     // If button 4 is low 
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("BUTTON 4 PRESS");  // Display the message
  }
  else
  {
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("ALL BUTTONS OFF");  // Display the message
  }
 
 delay(500);  // Wait 0.5 seconds before re-checking button states
}

The only thing i am now stuck on is that it will only display one input at a time, i would like to have the state of any active input cycle if one or more input is active i.e. if 2 buttons are pressed display 'input 1' then 'input 2' then repeat untill all inputs are clear.

Thanks,

Phil.
Logged

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

Your LCD is capable of displaying more that one bit of information at a time. So, you need to put the if tests back to independent ifs, and in each block set a flag that says "Yes some input was set". The else block gets executed, then, only if that flag is not set.

Code:
  bool gotInput = false;
  if (buttonState1 == HIGH)
  {     // If button 1 is high 
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("INPUT 1 HIGH");  // Display the message
    gotInput = true;
  } 

  if (buttonState2 == HIGH)
  {     // If button 2 is high 
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("BUTTON 2 ON");  // Display the message
    gotInput = true;
  }

  if (buttonState3 == LOW)
  {     // If button 3 is hlow 
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("INPUT 2 LOW");  // Display the message
    gotInput = true;
  }

  if (buttonState4 == LOW)
  {     // If button 4 is low 
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("BUTTON 4 PRESS");  // Display the message
    gotInput = true;
  }

  if(!gotInput)
  {
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("ALL BUTTONS OFF");  // Display the message
  }
Logged

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

Thanks, i have tried this and now when i activate an input that is higher than the one already activated it displays the message for the higest input untill i release the bottons. i have changed the code to make it simpler for teh time being and set all inputs as high and added a 5th as i will need 5 in teh finished project.

code is as follows now
Code:
/*

  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 * Pushbutton 1 to +5V & pin 7 ( also a 10K resistor to ground)
 * Pushbutton 2 to +5V & pin 8 ( also a 10K resistor to ground)

*/

// include the library code:
#include <LiquidCrystal.h>
const int buttonPin1 = 7;     // the number of the pushbutton pin
const int buttonPin2 = 8;     // the number of the pushbutton pin
const int buttonPin3 = 9;     // the number of the pushbutton pin
const int buttonPin4 = 10;     // the number of the pushbutton pin
const int buttonPin5 = 14;     // the number of the pushbutton pin
// variables will change:

int buttonState1 = 0;         // variable for reading the pushbutton1 status
int buttonState2 = 0;         // variable for reading the pushbutton2 status
int buttonState3 = 0;         // variable for reading the pushbutton3 status
int buttonState4 = 0;         // variable for reading the pushbutton4 status
int buttonState5 = 0;         // variable for reading the pushbutton5 status
int val = 0;     // variable for reading the pin status
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);



void setup() {

}

void loop() {
  lcd.clear();  // Clear the LCD
 
  // check if the pushbutton is pressed. 
  buttonState1 = digitalRead(buttonPin1);  // read the state of the pushbutton1 value
  buttonState2 = digitalRead(buttonPin2);  // read the state of the pushbutton2 value
  buttonState3 = digitalRead(buttonPin3);  // read the state of the pushbutton3 value
  buttonState4 = digitalRead(buttonPin4);  // read the state of the pushbutton4 value
  buttonState5 = digitalRead(buttonPin5);  // read the state of the pushbutton2 value
  // if it is, the buttonState is HIGH:
 
 bool gotInput = false;
   if (buttonState1 == HIGH)
  {     // If button 1 is high 
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("INPUT 1 HIGH");  // Display the message
    gotInput = true;
  } 

   if (buttonState2 == HIGH)
  {     // If button 2 is high 
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("INPUT 2 HIGH");  // Display the message
    gotInput = true;
  }

   if (buttonState3 == HIGH)
  {     // If button 3 is high
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("INPUT 3 HIGH");  // Display the message
    gotInput = true;
  }

   if (buttonState4 == HIGH)
  {     // If button 4 is high 
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("INPUT 4 HIGH");  // Display the message
    gotInput = true;
  }
  if (buttonState5 == HIGH)
  {     // If button 5 is high 
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("INPUT 5 HIGH");  // Display the message
    gotInput = true;
  }

  if(!gotInput)
  {
    lcd.setCursor(0, 0);  // Set the cursor
    lcd.print("READY TO RACE!");  // Display the message
  }
 
 delay(500);  // Wait 0.5 seconds before re-checking button states
}
Logged

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

Quote
Your LCD is capable of displaying more that one bit of information at a time.
BUT NOT IN THE SAME PLACE!
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19350
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

As PaulS said you need to use "else if". You can simplify your code a great deal and avoid this problem of only the last button being shown like this:

Code:
void loop() {
  lcd.clear();  // Clear the LCD
  lcd.setCursor(0, 0);  // Set the cursor
  if (digitalRead(buttonPin1) == HIGH)
    lcd.print("INPUT 1 HIGH");
  else if (digitalRead(buttonPin2) == HIGH)
    lcd.print("INPUT 2 HIGH");
  else if (digitalRead(buttonPin3) == HIGH)
    lcd.print("INPUT 3 HIGH");
  else if (digitalRead(buttonPin4) == HIGH)
    lcd.print("INPUT 5 HIGH");
  else if (digitalRead(buttonPin5) == HIGH)
    lcd.print("INPUT 5 HIGH");
  else
    lcd.print("READY TO RACE!"); 
 delay(500);  // Wait 0.5 seconds before re-checking button states
}

If you want to show all the buttons you are going to have to do something a bit more sophisticated.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

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

Thanks again Guys,

i have loaded th code as sugested above but now it will only show the last input pressed if it is lower than the previos input. is what i am trying to acheive not possible as i will stop wasting everyones time if so, also i am putting it down to the delay at teh bottom of the code but the display blinks every 500 mils, are there any sugestions out there how to stop this? i have a clock running on a display wich is the same running 500 mils delay and this does not flicker in teh same way.

Best regards,

Phil.
Logged

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

Yes, it is possible to display the status of all 4 switches BUT NOT IN THE SAME PLACE. Displaying 4 different pieces of information IN THE SAME PLACE is what you keep trying to do. You REALLY need to display them in 4 DIFFERENT places.
Logged

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

In this case i think i will buy a 20 x 4 display and display them in different places as i have taken up enough of everyones time already.

Has anyone experienced teh flicker issue and managed to ger round it in any way?

Thanks again,

Phil.
Logged

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

Quote
Has anyone experienced teh flicker issue and managed to ger round it in any way?
The "flicker" comes from clearing the screen and then immediately repainting the same text. Stop clearing the LCD on every pass through loop to stop the flicker.
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19350
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

In this case i think i will buy a 20 x 4 display and display them in different places as i have taken up enough of everyones time already.

You are still going to have trouble fitting in 5 lots of buttons in 4 lines, unless you restructure. For example, display:

Code:
Waiting: 1/2/3/4/5

That will fit on 20 characters even if all buttons are high.

You could do that something like this:

Code:
char msg [30];
strcpy (msg, "Waiting: ");

if (digitalRead(buttonPin1) == HIGH)
   strcat (msg, "1/");
end -- if
if (digitalRead(buttonPin2) == HIGH)
   strcat (msg, "2/");
end -- if
if (digitalRead(buttonPin3) == HIGH)
   strcat (msg, "3/");
end -- if
if (digitalRead(buttonPin4) == HIGH)
   strcat (msg, "4/");
end -- if
if (digitalRead(buttonPin5) == HIGH)
   strcat (msg, "5/");
end -- if

msg [strlen (msg) - 1] = 0;  // get rid of final slash

if (strlen (msg) < 10)
  strcpy (msg, "Ready to race!");

lcd.print (msg);  // show it

(untested)
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Pages: [1]   Go Up
Jump to: