Im trying to do a "LCD menu practice" but I can't make functions work.
I think that I have some scope problem, like this guy: http://forum.arduino.cc/index.php?topic=248386.0
but I don't know how to pass the values properly. (in case im doing it wrong)
In the arduino, THE LCD JUST SHOWS "Setup worked". that's all.
Please help!
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
char printalla; //put this here to to make a global variable so i can access it anywhere (is that correct for my intended use?)
byte numenu = 0; // with this variable i will choose what to display in a more complex sketch
char printallada(byte numenu);
/* This one above is the declaration of the future FUNCTION. it doesn't seem to be neccesary in many
codes that I've read. I tried with and without this line with same result. I saw a page that told about doing this in C++ and arduino */
void setup(){
lcd.begin(16, 2);
Serial.begin(9600);
lcd.clear();
lcd.print("Setup worked");
delay(1000); //all void setup works great.
}
void loop(){
// while (Serial.available() > 0) { //[i've also tried with this to see if it was a serial issue, same result]
if (Serial.read() == 'w') { //if I send w by serial
numenu = 1; } //change numenu variable to 1
// } //end curly bracket of the serial issue check. ommited
printallada; //callin my custom function
if (printalla >0) { /*Re-print the LCD ONLY when some information is incoming (see custom function to understand)
I also tried without the previous line, but i just get a semi faded strange simbol, even if I don't use the serial*/
lcd.clear();
lcd.print(printalla); }
} //end of void loop
char printallada(byte numenu) { //my custom function
if (numenu == 1) {
if (printalla != 'ONE OK') {
printalla = 'ONE OK'; }}
else { printalla = 0;} // else i want to return nothing to NOT re-print the lcd screen.
return printalla;
} //end of my custom function
The comments in the program suggest you want to update the LCD only when something has been received from serial monitor.
Is your function printallada() meant to communicate this from the code that reads serial monitor to the code that writes to the LCD? If so, there is an easier way.
Declare a global boolean variable and set it to false initially.
boolean updateLCD = false;
Then, when you set numenu in your code, also set the flag:
if (Serial.available())
{
if (Serial.read() == 'w')
{
numenu = 1;
updateLCD = true;
}
}
Finally, change your LCD write code to:
if (updateLCD)
{
lcd.clear();
lcd.print(printalla); // not changed from your code
updateLCD = false; // stops LCD updates until next character received from serial monitor
}
I'm not sure what you want to print on the LCD depending on the value on numenu. If you could clarify that, then we can suggest other changes.
jimboZA: i have to study your suggestion further.
I already read what you told me like a dozen times :).
Hackscribble:
The comments in the program suggest you want to update the LCD only when something has been received from serial monitor.
That's right.
Hackscribble:
I'm not sure what you want to print on the LCD depending on the value on numenu. If you could clarify that, then we can suggest other changes.
I eventually want to code a menu similar to
//Pseudocode
switch
case 1: print this
case 2: print that
[...]
case 11: submenu child of "print this"
I tried your changes and it ALMOST worked, now it doesn't get stuck on "setup worked", but when it read serial monitor this happens insted of "ONE OK"
(image below,, image link: Imgur: The magic of the Internet )
I noticed that the S printed in the LCD is the S of 'it workS'.
i deleted the s and it shows a K. (worK)
Adding a delay(100); to the code doesn't solve it. It's nota shortage of time to pass the data to the screen.
I think so..
'this' has a single quote . It's what I use with char to not get an error.
"this" has double quote. i can't use it as i explained a couple post above. (error)
I don't understand why you two ask about it..
are you seeing the same as i'm doing?
I don't think that the quotes are the problem . were talking about them because PaulS asked.
Tom Carpenter said in another thread:
"You aren't trying to return a char (a single byte signed variable), you are wanting to return a char* (a pointer to a multi-element array of chars)."
So I changed the myfunction type in declaration and calling
char myfunction (byte mynumber);
char mychar;
to
String myfunction (byte mynumber); //declaring future custom function. (IS THAT LINE NECCESARY??)
String mychar;
and then changed the message 'it works' to (double quote)-> "IT WORKS"
because if not i obtained: invalid conversion from 'int' to 'const char*'
EDIT: I'd like to thank all you guys who helped me with this problem. Thank you for your support.