Go Down

Topic: Passing Arrays into a function (Read 3150 times) previous topic - next topic

mjaredwilson

I'm new to the Arduino and I am having problems passing an array to a function to print to a 4D uLCD-320PMD2 screen using a Picaso graphics controller.


This code prints an array of characters successfully:


Code: [Select]

void setup() {
Serial.begin(9600);            //open serial port at 9600 bps

Serial.print(0x55,BYTE);      //initialize LCD

char str[]="abctest";
int i = 0;

Serial.print(0x53,BYTE);     //print string cmd
Serial.print(0x05,BYTE);     //x coord
Serial.print(0x00,BYTE);     //y1 coord
Serial.print(0x05,BYTE);     //y2 coord
Serial.print(0x00,BYTE);     //font
Serial.print(0x0f,BYTE);      //color
Serial.print(0xff,BYTE);      //color
Serial.print(0x02,BYTE);     //width
Serial.print(0x02,BYTE);     //height
 
 while(str[i]!='\0')
 {  
   Serial.print(str[i],BYTE);   //print each array element until '\0'
   i++;
 }

 Serial.print(0x00,BYTE);         //Terminator

}



while this code prints nothing:

Code: [Select]

void setup() {
Serial.begin(9600);            //open serial port at 9600 bps

Serial.print(0x55,BYTE);      //initialize LCD

char str1[]="abctest";
printstring(str1);

}
void loop() {
}

void printstring(char str[]) {
 
 delay(100);
 int i = 0;

 Serial.print(0x53,BYTE);        //print string cmd
 Serial.print(0x05,BYTE);        //x coord
 Serial.print(0x00,BYTE);        //y1 coord
 Serial.print(0x05,BYTE);        //y2 coord
 Serial.print(0x00,BYTE);        //font
 Serial.print(0x0f,BYTE);         //color
 Serial.print(0xff,BYTE);         //color
 Serial.print(0x02,BYTE);        //width
 Serial.print(0x02,BYTE);       //height

 while(str[i]!='\0')
 {  
   Serial.print(str[i],BYTE);
   i++;
 }
 Serial.print(0x00,BYTE);         //Terminator
}



I've also tried:
void printstring(char *str[]) {


Thanks for the help, and sorry if I looked over the obvious solution when searching the forums!

AWOL

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

cr0sh

A little explanation for AWOL's post:

What "void printstring(char* str)" does is declare the function as having an argument thru which you are passing a pointer. Basically, a pointer is nothing more than an address which "points" to the memory where the array begins.

That's the basics; I won't go into pointer math, etc - when pointers are involved, things get complicated quickly if you aren't careful (even if you know what you are doing!)...

;D
I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

mjaredwilson

Still no luck.  I feel like it isn't keeping the actual character elements in the new array.  Code:

Code: [Select]

char str1[]="abctest";
printstring(str1);

}
void loop() {
}

void printstring(char* str) {
 
 delay(100);
 int i = 0;

 Serial.print(0x53,BYTE);        //print string cmd
 Serial.print(0x05,BYTE);           //x coord
 Serial.print(0x00,BYTE);          //y1 coord
 Serial.print(0x05,BYTE);          //y2 coord
 Serial.print(0x00,BYTE);        //font
 Serial.print(0x0f,BYTE);      //color
 Serial.print(0xff,BYTE);      //color
 Serial.print(0x02,BYTE);      //width
 Serial.print(0x02,BYTE);     //height

 while(str[i]!='\0')
 {  
   Serial.print(str[i],BYTE);
   i++;
 }

 Serial.print(0x00,BYTE);         //Terminator

}


Here is the serial output for this code:





Here is the Serial Output for the original code without a printstring function:



This output matches that of the code with the function but this code actually prints "abctest" to the LCD screen.

PaulS

Quote
I feel like it isn't keeping the actual character elements in the new array.


What new array?

The serial monitor output shows that the pointer to the array is being passed to the function, and that the function has access to the pointed-to memory locations.

BDub

#5
Feb 16, 2010, 11:31 pm Last Edit: Feb 16, 2010, 11:32 pm by SufiBrett Reason: 1
Looks like it prints out exactly what it is supposed to.  What do you want it to do differently?

mjaredwilson

Well it looks like it prints the correct data to the serial console but this text is not showing up on the LCD as it does for the code without the function.

BDub

#7
Feb 17, 2010, 12:05 am Last Edit: Feb 17, 2010, 12:06 am by SufiBrett Reason: 1
I don't see any code for the LCD, so you must be just unplugging the computer and plugging in the LCD on TX/RX?

I can't see a difference timing wise... and if the console displays the same characters with the printstring() function or without, and at the same baud rate, it should work with your LCD.

mjaredwilson

I am leaving the usb cable plugged in and each serial.print(hex value here, BYTE) sends a command to the graphics controller on the LCD.

 Serial.print(0x53,BYTE);          //print string cmd
 Serial.print(0x05,BYTE);          //x coord
 Serial.print(0x00,BYTE);          //y1 coord
 Serial.print(0x05,BYTE);         //y2 coord
 Serial.print(0x00,BYTE);         //font
 Serial.print(0x0f,BYTE);          //color
 Serial.print(0xff,BYTE);          //color
 Serial.print(0x02,BYTE);         //width
 Serial.print(0x02,BYTE);         //height
 Serial.print('G',BYTE);            //print G
 Serial.print(0x00,BYTE);         //Terminator


This set of commands prints The letter 'G' on the LCD.

BDub

#9
Feb 17, 2010, 12:37 am Last Edit: Feb 17, 2010, 12:38 am by SufiBrett Reason: 1
Well I do see one difference...

In the function version you have a delay between initializing the display and sending the first piece of data, effectively doing this:

Code: [Select]
Serial.print(0x55,BYTE);      //initialize LCD
delay(100);
Serial.print(0x53,BYTE);        //print string cmd


Maybe take out the delay?  I have no idea what your LCD will tolerate or why it wouldn't like a delay.

Other ideas are check the RX line on the LCD with a scope to see what it's receiving with the two different versions of your code.

mjaredwilson

I removed the delay to no avail.  How do I go about checking the RX line with a scope?

BDub

Well that's a bummer.

If you have an oscilloscope available, check the delays between all of the bytes sent, and zoom in on each byte and see if they are identical.

Since that's a lot of work, can you post the complete code listing for both versions... and can you retest the non-function version to make sure it still works?

mjaredwilson

I figured it out!  Thanks for the help.  The issue was that I was not allowing a long enough delay after a command to change the background so the LCD was never receiving the bytes telling it to print the array!

PogCarr

You want to elaborate on what you did to make it work?  I am curious as I am just starting to play with this same piece of hardware....

Go Up