I have a program that reads a 16-key encoder and prints messages to an LCD.
I have a CASE function that reads a variable called "Key" (the value mapped to the returned key (by array).
Thus with the range 0 to 15 with 16 keys I have 16 values, and therefore 16 CASEs.
Currently I have a different LCD.print statement for each CASE. Actually two, one for line-1 and one for line-2.
I thought I should be able to create a function that contains the variable "Key" in the print statements
Like this:
I omitted the "void" in front of it because it takes an argument.
This is the call to it in the switch CASE funtion:
I included the next CASE so you could see that I have only modified the CASE-0 (of 16 cases).
/* Output the test message to the LCD */
switch(Key)
{
case 0:
printmsg(Key);
break;
case 1:
lcd.setCursor(0,0);
lcd.print("CMD-1 ");
lcd.setCursor(0,1);
lcd.print("Do CMD-1 ");
break;
When I try to run this I get the following compiler errors:
C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=105 -IC:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino -IC:\Program Files (x86)\Arduino\hardware\arduino\variants\standard -IC:\Program Files (x86)\Arduino\libraries\LiquidCrystal C:\Users\Robert\AppData\Local\Temp\build7707330811771088722.tmp\Matrix_4x4_16_key_encoder_LCD_CASE_w_newfunction.cpp -o C:\Users\Robert\AppData\Local\Temp\build7707330811771088722.tmp\Matrix_4x4_16_key_encoder_LCD_CASE_w_newfunction.cpp.o
Matrix_4x4_16_key_encoder_LCD_CASE_w_newfunction.ino: In function 'void loop()':
Matrix_4x4_16_key_encoder_LCD_CASE_w_newfunction:47: error: 'printmsg' was not declared in this scope
Matrix_4x4_16_key_encoder_LCD_CASE_w_newfunction.ino: At global scope:
Matrix_4x4_16_key_encoder_LCD_CASE_w_newfunction:148: error: expected constructor, destructor, or type conversion before '(' token
Clearly I am doing something wrong.
Is there another way to accomplishe the desired result ?
Is it fixable or am I trying to do something that can't be done ?
OOPS !
Yeah ! That works .
THANKS !
Here's the new function with the extra print statements with spaces because I can't combine ints & strings in the print:
Also attached is the corrected working program used to obtain the attached photo of the display.
Thanks guys.
That is my first attempt at creating a function to simplify a CASE statement.
It seems that the trick to making it work is taking the contents of a line of characters that I want to display
and chopping it into pieces that are displayed by different print statements. Using that method I should be able
to print any combination of different data types combined with text strings.
Depending on whether you have anything else displayed on the LCD that you need to preserve you could reduce the number of print statements in your function by using lcd.clear()
No, this is just a template.
The final version will have a different function name on line-1 and a routine name on line-2.
ie:
Line-1: "FNCT-1: Onewire temp sensors"
Line-2: "Display DS18B20 temp sensors"
OR
Line-1: "FNCT-4: Thermocouple Temp"
Line-2: "Display Thermocouple temp"
or
Line-1: "FNCT-6: Blower fan"
Line-2: "Turn on blower fan relay"
Right now I'm experimenting with the printf function , trying to get this to work but I'm getting compiler errors. #include <printf.h>
Serial.begin(9600);
printf.begin();
I discovered when running the NRF24L01 LedRemote example that I could use the serial monitor to tell
when the radios had detected a signal and also all of the other nRF24L01 programs that used printf would
print to my serial monitor so I could use all the programs and monitor the radio signal link on the serial
monitor, which is really helpful when you don't have a spectrum analyzer with an antenna on it to see
the signal. Basically, I don't really need the printf function for this application, but the big picture is that
the only reason I am doing any of this is to upgrade my technician job skills and in that respect everything
is relevant . I want to understand how to use the printf function when I do need it. According to my book
"C Programming, Absolute Beginner's Guide"
by Greg Perry and Dean Miller (QUE publishing co.)
I can mix different datatypes as explicit or as variable names with text strings using %s ,%d,%f etc..
That would be nice for an RS232 output of the info displayed on the LCD.
I finally got this to work (just once) only when a key is pressed: // Prints: "CMD-[key] " ( | where "Key"= value returned) printf("\n\r%s %d %s \n\r","CMD-",Key," "); (in function below)
You're probably thinking of sprintf(), which prints to a string which you could then send to a single call to lcd.print(). There is a considerable amount of overhead using the function, but it should work. Also, lcd.clear() is one of the slowest methods in the library. It probably doesn't matter in this application, but FYI for down the road.