Pages: [1]   Go Down
Author Topic: Passing Arrays into a function  (Read 2619 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26314
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

void printstring(char* str)
Logged

"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.

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 40
Posts: 5579
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

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

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.
Logged

Chicago, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 99
<3 U Long Time, Arduino!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks like it prints out exactly what it is supposed to.  What do you want it to do differently?
« Last Edit: February 16, 2010, 05:32:05 pm by SufiBrett » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Chicago, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 99
<3 U Long Time, Arduino!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: February 16, 2010, 06:06:14 pm by SufiBrett » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Chicago, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 99
<3 U Long Time, Arduino!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: February 16, 2010, 06:38:39 pm by SufiBrett » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Chicago, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 99
<3 U Long Time, Arduino!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 19
Arduino controlled scanning...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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....
Logged

Pages: [1]   Go Up
Jump to: