Pages: [1]   Go Down
Author Topic: storing to memory in one function to use in another  (Read 1200 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

im having trouble storing text to a memeory location in one function to be used in another.

when i try and diplay the saved text on the lcd it shows nothing.

my code is too big so i have attached it.

any advice would be apreachiated.

* storing2memory.ino (10.18 KB - downloaded 14 times.)
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13477
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

use a global char array to store the text.

This is the concept (not tested)
Code:
char ar[24];

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  f();
  g();
  delay(1000);
}

void f()
{
  strcpy(ar, "hello world");
}

void g()
{
  Serial.println(ar);
  strcpy(ar, "");
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you so much for your relpy, its now stores the text and displays what i want it to.
I have one more small problem now, when i display the text line by line on the lcd id does until i go to scroll the lcd.
using this code
Code:
void lcddisplay()//subroutine (function) lcddisplay
{//start subroutine
  switch (linenum) //case statement
  {//start case statement
  case 0:// if linenum=0
    lcd.setCursor (0,1);//set cursor to row1 charector 0 of lcd display
    lcd.print(line1);//display the contents of line1 on lcd on row 1
    Serial.println(line1);
    delay(100);
    break;//end of case1 exit switch
  case 1:// if linenum=1
    lcd.setCursor (0,2);//set cursor to row2 charector 0 of lcd display
    lcd.print (line2);//display the contents of line2 on lcd on row 2
    Serial.println(line2);
    delay(100);
    break;//end of case2 exit switch
  case 2://if linenum=2
    lcd.setCursor (0,3);//set cursor to row3 charector 0 of lcd display
    lcd.print (line3);//display the contents of line3 on lcd on row 3
    Serial.println(line3);
    delay(100);
    break;//end of case3 exit switch
  case 3:// if linenum=3
    lcd.setCursor (0,1);//set cursor to row1 charector 0 of lcd display
    lcd.print (line2);//display the contents of line2 on lcd on row 1
    Serial.println(line2);
    delay(100);
    lcd.setCursor (0,2);//set cursor to row2 charector 0 of lcd display
    lcd.print (line3);//display the contents of line3 on lcd on row 2
    Serial.println(line3);
    delay(100);
    lcd.setCursor (0,3);//set cursor to row3 charector 0 of lcd display
    lcd.print (line4);//display the contents of line4 on lcd on row 3#
    Serial.println(line4);
    delay(100);
    break;//end of case4 exit switch
  case 4:// if linenum=4
    lcd.setCursor (0,1);//set cursor to row1 charector 0 of lcd display
    lcd.print (line3);//display the contents of line3 on lcd on row 1
    Serial.println(line3);
    delay(100);
    lcd.setCursor (0,2);//set cursor to row2 charector 0 of lcd display
    lcd.print (line4);//display the contents of line4 on lcd on row 2
    Serial.println(line4);
    delay(100);
    lcd.setCursor (0,3);//set cursor to row3 charector 0 of lcd display
    lcd.print (line5);//display the contents of line4 on lcd on row 3
    Serial.println(line5);
    delay(100);
    break;//end of case4 exit switch
  }//end switch
}//end subroutine

on case0-2 it displays the text stored in the lines.
on case 3 when ii try and move the lines up one it goes a little wrong.

on line(0,1) it displays line2,line3,line4
on line(0,2) it displays line3,line4
on line(0,3) it displays line4.

can you see in my code where im going wrong?
Logged

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

Code:
int cardone[16] = {
  2,53,57,48,48,50,57,51,49,67,51,56,50,13,10,3};//stores the string id of rf tag one
int cardthree[16] = {
  2,53,57,48,48,50,57,52,55,50,69,49,57,13,10,3};//stores the string id of rf tag three
int cardfour[16] = {
  2,53,57,48,48,50,57,50,67,57,69,67,50,13,10,3};//stores the string id of rf tag four
int cardfive[16] = {
  2,53,57,48,48,50,57,55,66,48,49,48,65,13,10,3};//stores the string id of rf tag five
int newcard [16] = {
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//stores a new sting of same integers as tags but all set to 0
160 bytes gone, where only 80 need to be used. The tag data is byte, not int.

Code:
String line1;
String line2;
String line3;
String line4;
String line5;
Wrong. Learn to use c strings (NULL terminated arrays of chars). No wasting resources dynamically allocating/deallocating memory.

Code:
    delay(100);//delay so all data is read
Absolutely, unequivocally, wrong!

There is no need to wait for "all the data" to arrive. If you think that it is, you might as well may this delay(4294967295) so that you can be absolutely certain.

Code:
    for (int z=0; z<16; z++)//start at z=0 then add number for every number that comes in from tag
    {
      data=Serial.read();//input numbers from rf id and store into data
      newcard[z] = data;// store number from data into array of z long in newcard
      Serial.flush();//stops multiple reads
    }
You know that there was one character to read. You hope that 15 more arrived while you were sitting on your thumbs. Then, you assume that that happened. Then, you block waiting for all sent data to be sent.

Do you see what is wrong with this picture?

Code:
    switch (items)//case statment
This isn't a case statement. It isn't a for statement, an if statement, or a function call, either.

Most people can see what it is. Does an incorrect comment help?

Code:
        String line1="Hienz";// store the text hienz in memory loaction line1
Creating local variables in a case statement is rarely a good idea. The variable goes out of scope at the end of the case statement.

How do cases 1, 2, and 3 differ from case 1?


Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

im sorry that was the wrong code to send, this is the snip that i wanted to send, although i have introduced the differences you suggested.
Code:
LiquidCrystal lcd(4,5,6,7,8,9);//sets digitial pins for lcd connection
int data = 0;//sets data to an integer 0
byte cardone[16] = {
  2,53,57,48,48,50,57,51,49,67,51,56,50,13,10,3};//stores the string id of rf tag one
byte cardthree[16] = {
  2,53,57,48,48,50,57,52,55,50,69,49,57,13,10,3};//stores the string id of rf tag three
byte cardfour[16] = {
  2,53,57,48,48,50,57,50,67,57,69,67,50,13,10,3};//stores the string id of rf tag four
byte cardfive[16] = {
  2,53,57,48,48,50,57,55,66,48,49,48,65,13,10,3};//stores the string id of rf tag five
byte newcard [16] = {
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//stores a new sting of same integers as tags but all set to 0
float cost = 0;// sets cost of items to 0.00 float means there is a decimal point
float total = 0;// sets total to 0.00 float means there is a decimal point
int linenum = 0;// sets the integer linenum to 1
int items = 0;//sets the integer items to 0
char line1[19];
char line2[19];
char line3[19];
char line4[19];
char line5[19];
int i = 0;

void setup ()//set up
{
  delay (1000);// delay
  Serial.begin(9600);//initialize serial communications at 9600 bps:
  Serial.flush();//
  lcd.begin (20,4);//set up number or charectors and rows on lcd
  lcd.clear();//clear the lcd display
  lcd.setCursor(2,1);//set the lcd cursor to line 1 charector 2
  lcd.print ("Hello shopper");//display hello shopper on lcd display
  delay(5000);//delay
  lcd.clear();//clear lcd display
  totalpay();//run subroutine (function) totalpay
}

void totalpay()// subroutine (function) totalpay
{//start subroutine
  total=cost+total;//add the current number in cost to the total and puts back into total
  delay (500);//delay
  lcd.home();//set cursor to row0 charector 0
  lcd.print ("items:");// display the text items on lcd display
  Serial.print("items:");
  lcd.print (items);// display the integer number held in items
  Serial.print(items);
  lcd.print (" total:");// display the text total on lcd display
  Serial.print("  total");
  lcd.print (total,2);// display the number held in total to two decial places
  Serial.println(total);
  delay (1000);//delay
  cost = 0;//reset cost back to 0
}//end subroutine

void lcddisplay()//subroutine (function) lcddisplay
{
  switch (linenum)
  {
  case 0:// if linenum=0
    lcd.setCursor (0,1);//set cursor to row1 charector 0 of lcd display
    lcd.print(line1);//display the contents of line1 on lcd on row 1
    Serial.println(line1);
    delay(100);
    break;//end of case1 exit switch
  case 1:// if linenum=1
    lcd.setCursor (0,2);//set cursor to row2 charector 0 of lcd display
    lcd.print (line2);//display the contents of line2 on lcd on row 2
    Serial.println(line2);
    delay(100);
    break;//end of case2 exit switch
  case 2://if linenum=2
    lcd.setCursor (0,3);//set cursor to row3 charector 0 of lcd display
    lcd.print (line3);//display the contents of line3 on lcd on row 3
    Serial.println(line3);
    delay(100);
    break;//end of case3 exit switch
  case 3:// if linenum=3
    lcd.setCursor (0,1);//set cursor to row1 charector 0 of lcd display
    lcd.print (line2);//display the contents of line2 on lcd on row 1
    Serial.println(line2);
    delay(100);
    lcd.setCursor (0,2);//set cursor to row2 charector 0 of lcd display
    lcd.print (line3);//display the contents of line3 on lcd on row 2
    Serial.println(line3);
    delay(100);
    lcd.setCursor (0,3);//set cursor to row3 charector 0 of lcd display
    lcd.print (line4);//display the contents of line4 on lcd on row 3#
    Serial.println(line4);
    delay(100);
    break;//end of case4 exit switch
  case 4:// if linenum=4
    lcd.setCursor (0,1);//set cursor to row1 charector 0 of lcd display
    lcd.print (line3);//display the contents of line3 on lcd on row 1
    Serial.println(line3);
    delay(100);
    lcd.setCursor (0,2);//set cursor to row2 charector 0 of lcd display
    lcd.print (line4);//display the contents of line4 on lcd on row 2
    Serial.println(line4);
    delay(100);
    lcd.setCursor (0,3);//set cursor to row3 charector 0 of lcd display
    lcd.print (line5);//display the contents of line4 on lcd on row 3
    Serial.println(line5);
    delay(100);
    break;//end of case4 exit switch
  }//end switch

this is the output i get on the serial monitor


items:0  total0.00
Hienz baked beans 
items:1  total0.10
pint skimmed milk 
items:2  total0.60
loaf white bread   
items:3  total1.30
pint skimmed milk  loaf white bread   6 eggs             
loaf white bread   6 eggs             
6 eggs             
items:4  total1.55
loaf white bread   6 eggs             Hienz baked beans 
6 eggs             Hienz baked beans 
Hienz baked beans 
items:5  total1.65


it should only have one item per line.
Logged

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

Code:
lcd.print (line5);//display the contents of line4 on lcd on row 3
Code:
void lcddisplay()//subroutine (function) lcddisplay
Over-commenting is almost as ineffective as under-commenting.

Code:
char line1[19];
Isn't each line 20 characters wide?

When you find yourself typing the same, related numeric value multiple times, it is time to give them a name, so when you come to correct it, you only need to make a single edit.
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.

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
When you find yourself typing the same, related numeric value multiple times, it is time to give them a name, so when you come to correct it, you only need to make a single edit.

sorry i dont understand
Logged

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

Like here
Code:
char line1[19];
char line2[19];
char line3[19];
char line4[19];
char line5[19];
(though in this case, a two dimensional array would be more in order. Still need to give 19 a useful name, though)
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.

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

as you can see in the code i printed on here last i have used char as an array to store the text.  The problem comes when i try and display the text on case 3 and onwards.  i just want to display one item on each line and move them up so a new line can be added but instead i get three items per line. not sure how to stop this.

serial monitor result:
items:0  total0.00
Hienz baked beans 
items:1  total0.10
pint skimmed milk 
items:2  total0.60
loaf white bread   
items:3  total1.30
pint skimmed milk  loaf white bread   6 eggs             
loaf white bread   6 eggs             
6 eggs             
items:4  total1.55
loaf white bread   6 eggs             Hienz baked beans 
6 eggs             Hienz baked beans 
Hienz baked beans 
items:5  total1.65


the code for it

Code:
LiquidCrystal lcd(4,5,6,7,8,9);//sets digitial pins for lcd connection
int data = 0;//sets data to an integer 0
byte cardone[16] = {
  2,53,57,48,48,50,57,51,49,67,51,56,50,13,10,3};//stores the string id of rf tag one
byte cardthree[16] = {
  2,53,57,48,48,50,57,52,55,50,69,49,57,13,10,3};//stores the string id of rf tag three
byte cardfour[16] = {
  2,53,57,48,48,50,57,50,67,57,69,67,50,13,10,3};//stores the string id of rf tag four
byte cardfive[16] = {
  2,53,57,48,48,50,57,55,66,48,49,48,65,13,10,3};//stores the string id of rf tag five
byte newcard [16] = {
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//stores a new sting of same integers as tags but all set to 0
float cost = 0;// sets cost of items to 0.00 float means there is a decimal point
float total = 0;// sets total to 0.00 float means there is a decimal point
int linenum = 0;// sets the integer linenum to 1
int items = 0;//sets the integer items to 0
char line1[19];
char line2[19];
char line3[19];
char line4[19];
char line5[19];
int i = 0;

void setup ()//set up
{
  delay (1000);// delay
  Serial.begin(9600);//initialize serial communications at 9600 bps:
  Serial.flush();//
  lcd.begin (20,4);//set up number or charectors and rows on lcd
  lcd.clear();//clear the lcd display
  lcd.setCursor(2,1);//set the lcd cursor to line 1 charector 2
  lcd.print ("Hello shopper");//display hello shopper on lcd display
  delay(5000);//delay
  lcd.clear();//clear lcd display
  totalpay();//run subroutine (function) totalpay
}

void totalpay()// subroutine (function) totalpay
{//start subroutine
  total=cost+total;//add the current number in cost to the total and puts back into total
  delay (500);//delay
  lcd.home();//set cursor to row0 charector 0
  lcd.print ("items:");// display the text items on lcd display
  Serial.print("items:");
  lcd.print (items);// display the integer number held in items
  Serial.print(items);
  lcd.print (" total:");// display the text total on lcd display
  Serial.print("  total");
  lcd.print (total,2);// display the number held in total to two decial places
  Serial.println(total);
  delay (1000);//delay
  cost = 0;//reset cost back to 0
}//end subroutine

void lcddisplay()//subroutine (function) lcddisplay
{
  switch (linenum)
  {
  case 0:// if linenum=0
    lcd.setCursor (0,1);//set cursor to row1 charector 0 of lcd display
    lcd.print(line1);//display the contents of line1 on lcd on row 1
    Serial.println(line1);
    delay(100);
    break;//end of case1 exit switch
  case 1:// if linenum=1
    lcd.setCursor (0,2);//set cursor to row2 charector 0 of lcd display
    lcd.print (line2);//display the contents of line2 on lcd on row 2
    Serial.println(line2);
    delay(100);
    break;//end of case2 exit switch
  case 2://if linenum=2
    lcd.setCursor (0,3);//set cursor to row3 charector 0 of lcd display
    lcd.print (line3);//display the contents of line3 on lcd on row 3
    Serial.println(line3);
    delay(100);
    break;//end of case3 exit switch
  case 3:// if linenum=3
    lcd.setCursor (0,1);//set cursor to row1 charector 0 of lcd display
    lcd.print (line2);//display the contents of line2 on lcd on row 1
    Serial.println(line2);
    delay(100);
    lcd.setCursor (0,2);//set cursor to row2 charector 0 of lcd display
    lcd.print (line3);//display the contents of line3 on lcd on row 2
    Serial.println(line3);
    delay(100);
    lcd.setCursor (0,3);//set cursor to row3 charector 0 of lcd display
    lcd.print (line4);//display the contents of line4 on lcd on row 3#
    Serial.println(line4);
    delay(100);
    break;//end of case4 exit switch
  case 4:// if linenum=4
    lcd.setCursor (0,1);//set cursor to row1 charector 0 of lcd display
    lcd.print (line3);//display the contents of line3 on lcd on row 1
    Serial.println(line3);
    delay(100);
    lcd.setCursor (0,2);//set cursor to row2 charector 0 of lcd display
    lcd.print (line4);//display the contents of line4 on lcd on row 2
    Serial.println(line4);
    delay(100);
    lcd.setCursor (0,3);//set cursor to row3 charector 0 of lcd display
    lcd.print (line5);//display the contents of line4 on lcd on row 3
    Serial.println(line5);
    delay(100);
    break;//end of case4 exit switch
  }//end switch
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 108
Posts: 4016
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Seeing your whole code would be more helpful
Where does the text description of your items come from ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

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

Normally, when I see a switch/case being used with individual variable names, I think "array", in this case, a two-dimensional array. It'll make the code a lot shorter.
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.

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

Code:
  delay (1000);// delay
Why? There is no reason at all to delay() as the first thing in setup().

Code:
  Serial.begin(9600);//initialize serial communications at 9600 bps:
  Serial.flush();//
Block until all sent data has been shifted out. You have not sent anything. The flush() call is useless.

Code:
  delay(5000);//delay
Why?

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i have uploaded my code as it is too big to put on here.

i am still getting the same problem

serial monitor result:
items:0  total0.00
Hienz baked beans 
items:1  total0.10
pint skimmed milk 
items:2  total0.60
loaf white bread   
items:3  total1.30
pint skimmed milk  loaf white bread   6 eggs             
loaf white bread   6 eggs             
6 eggs             
items:4  total1.55
loaf white bread   6 eggs             Hienz baked beans 
6 eggs             Hienz baked beans 
Hienz baked beans 
items:5  total1.65


and the output to the display is fine until case 3 where it display

6eggs    //(its meant to be pink skimmed milk)
loaf white bread
6eggs

the same happens on case 4, it displays

Hienz baked beans  //(should be loaf white bread)
6 eggs           
Hienz baked beans


any ideas why it does this and how i could change it?

ps, sorry if my code doesnt read well, only got arduino 2 weeks ago so still trying to figure it all out

* storing2memory.ino (15.67 KB - downloaded 12 times.)
Logged

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

Code:
  if (Serial.available() > 0)//if a card is swiped read incoming rx number from rfid tag
  {
    for (int z=0; z<16; z++)//start at z=0 then add number for every number that comes in from tag
    {
      data=Serial.read();//input numbers from rf id and store into data
      newcard[z] = data;// store number from data into array of z long in newcard
      Serial.flush();//stops multiple reads
      delay (50);
    }
This is still crap. You have been told that before. If there is ONE byte of serial data to read, and that is all that is required to be in the buffer when the if test evaluates to true, you can NOT read all of the the one characters.

You do not need the flush() call. Period. Get rid of it.

Get rid of all the crap that writes to the LCD until you KNOW that the data in line1, line2, line3, line4, and line5 is correct.

The arrays line1, line2, line3, line4, and line5 are NOT strings, so passing them to functions that expect strings is an exercise in futility.

Get a book on C and research the difference between a string and a char array. Then, fix your code so that it NULL terminates the char arrays, making them strings. Oops, I think I gave it away...

Logged

Pages: [1]   Go Up
Jump to: