[SOLVED] Functions not working/returning . surely scope issue.

Hi all.

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:

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

I think you need to step back a moment and read this.

Your function printallada returns a character, and also requires a parameter. We know that from this:

char printallada(byte numenu)

So you need to call it like this:

myChar = printallada(1);

... where myChar is previously declared as char, and in my example, 1 is the value to pass to printallada.

Hi Berseker_Quark

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.

Regards

Ray

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: http://imgur.com/6yi0jsW )

Berseker_Quark:
jimboZA: i have to study your suggestion further.
I already read what you told me like a dozen times :).

See if the markup in the attached screenshot helps at all…

JimboZA:

Berseker_Quark:
jimboZA: i have to study your suggestion further.
I already read what you told me like a dozen times :).

See if the markup in the attached screenshot helps at all…

I tried your stuff with a new and more simple example,
look at the video

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


byte mynumber ;
char myfunction (byte mynumber);  //declaring future custom function. (IS THAT LINE NECESSARY??)
char mychar;

void setup() {
    lcd.begin(16, 2);
    lcd.clear();                           
    lcd.print("Setup worked");
    delay(2000);   // time for humans to look at the screen
}

void loop() {
mynumber = 1; //to simplify

mychar = myfunction(mynumber);  
lcd.clear();
lcd.print(mychar);
 delay(5000);  //added this line to avoid ultrafast lcd printing loop.
}


char myfunction(byte mynumber) {   //my custom function

  if (mynumber == 1) {
  mychar = 'IT WORKS';
  }

return mychar;
}  //end of my custom function
  mychar = 'IT WORKS';

Please post a picture of your keyboard, with the ONE key circled that you pressed to get the ONE character in the single quotes.

PaulS:   mychar = 'IT WORKS';

Please post a picture of your keyboard, with the ONE key circled that you pressed to get the ONE character in the single quotes.

This is the character:

' ' ' ' ' '

If i try with " " i get : error: invalid conversion from 'const char*' to 'char'

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.

what's happening...?

'This' is not the same as "This".

Can you see the difference?

AWOL: 'This' is not the same as "This".

Can you see the difference?

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.

I don't think that the quotes are the problem

You're in a very small minority

I reply to myself:

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.