Passing Variable froma .cpp file

I have a rather large program and it is getting to the point where I need to get some of the functions out of the main .ino file.

After several hours of struggling, I have manged to figure out how to setup an .h file and an,cpp file to start moving my functions into. for the most part, I have it working. I can call the functions from the main program. I can call functions from functions in the .cpp file. I can even pass variables to the functions in the .cpp file.

Many of my functions generate data (or conditions) and pass the data to global variables in the main programs for other functions to use. This is where I am having trouble. I can't get the data from functions in the .cpp files back to the main program. Below is a small section of what I am doing:

//Main Program

 while (Serial.available() >= 1) { // receive Task
         Inbyte = Serial.read();
          if (Inbyte == 77)  {
               Serial.println(strcpy_P(ScreenLabel, (char*)pgm_read_word(&(TextHdrTbl[6])))); 
               SelectMap(MemBank); }  //function call 

        }
//  .h file

#include <Arduino.h>
#include <stdlib.h>
#include <stdint.h>


void SelectMap(uint8_t MemBank);
void SetBank(uint8_t MemBank);
// .cpp file

void SelectMap(uint8_t MemBank){
          Serial.print("MemBank= "); Serial.println(MemBank); 
          if (MemBank <= 7) MemBank = MemBank +1;  
          if(MemBank == 7) {MemBank = 0;}  
          Serial.print("MemBank= "); Serial.println(MemBank); 
          SetBank(MemBank);
       }



void SetBank(uint8_t MemBank) {   
                 Serial.println("SetBank");
                 Serial.print("MemBank="); Serial.println(MemBank); 
                 if(MemBank ==0){digitalWrite(42, LOW); digitalWrite(43, LOW); digitalWrite(44, LOW);}  
                 if(MemBank ==1){digitalWrite(42, LOW); digitalWrite(43, LOW); digitalWrite(44,HIGH);}  
                 if(MemBank ==2){digitalWrite(42, LOW); digitalWrite(43,HIGH); digitalWrite(44, LOW);}  
                 if(MemBank ==3){digitalWrite(42, LOW); digitalWrite(43,HIGH); digitalWrite(44,HIGH);}  
                 if(MemBank ==4){digitalWrite(42,HIGH); digitalWrite(43, LOW); digitalWrite(44, LOW);}  
                 if(MemBank ==5){digitalWrite(42,HIGH); digitalWrite(43, LOW); digitalWrite(44,HIGH);}  
                 if(MemBank ==6){digitalWrite(42,HIGH); digitalWrite(43, HIGH); digitalWrite(44,LOW);}  
                }
//Output 

MemBank=0  //initial "MemBank" condition

Map Select   // First pass through "SelectMap"
MemBank= 0
MemBank= 1

SetBank      // "SetBank" called from "SelectMap"
MemBank=1

Map Select   // Second  pass through "SelectMap"
MemBank= 0  // Notice the updated value for "MemBank" never got to the Global variable in the main
MemBank= 1

SetBank
MemBank=1   // "SetBank" called from "SelectMap"

I have even tried to pass updated the value through the function using a "return" statement and all I get is junk.

I know that there is something basic I am missing here..

Any assistence you can provide will be greatly appreciated.

I figured it out!!

You have to pass the variable by "Reference". You have to add a "&" to the Var when passing the variable. See below:

/,cpp file

 // Won't change " MemBank
void SelectMap(uint8_t MemBank){
          Serial.print("MemBank= "); Serial.println(MemBank);
          if (MemBank <= 7) MemBank = MemBank +1; 
          if(MemBank == 7) {MemBank = 0;} 
          Serial.print("MemBank= "); Serial.println(MemBank);
          SetBank(MemBank);
       }

 // Will change " MemBank
void SelectMap(uint8_t* MemBank){
          Serial.print("MemBank= "); Serial.println(MemBank);
          if (MemBank <= 7) MemBank = MemBank +1; 
          if(MemBank == 7) {MemBank = 0;} 
          Serial.print("MemBank= "); Serial.println(MemBank);
          SetBank(MemBank);
       }

Just thought I will update the post

To share variables across cpp files, declare them as 'extern' in your .h file and define them - once - in one of your cpp files.

-- foo.h --

extern int someVar;
extern int otherVar;

-- foo.ino --

#include "foo.h"

int someVar;

-- bar.cpp --

#include "foo.h"

int otherVar;

Thanks bunch.

I haven't gotten to that point yet but I will. To save space, I have placed all of my printed text in "Progmem". That in its self is some 50 lines of code at the top of my program. It will be nice to get that off the main .ino and of course, the text is used by a host of functions that could be in several .cpp files.

The next fun point will be passing pointers. Getting things out of "Progmem" requires the passing of pointers.

Thanks for the info.

Since the value being "shared" is a byte, I think it is simpler to simply have the function return a value. When the data being shared becomes more complex, like passing structs, arrays, or pointers, or when you need to pass more than one value back, then pass by reference becomes useful.

Here is where I'm at now.

As mentioned above, I have a lot of text stored in Progmem. I am using the procedure provided in the reference manual and it works well as long as everything is local and I don't try to pass the variables to the functions using them. When I try to pass the variables to the procedures, thing come apart.

Example:

//Program text stored in PROGMEM
const char TextHdr0[]  PROGMEM = "Kloster";
const char TextHdr1[]  PROGMEM = "AutoWorks";
const char TextHdr2[]  PROGMEM = "Task List: M=Select Map L=ListFiles E=Erase";
const char TextHdr3[]  PROGMEM = "G=InputArgs R=Read SD C=Convert D=Display Image";
const char TextHdr4[]  PROGMEM = "Display Image";
const char TextHdr5[] PROGMEM = "Screen Number 0-7";
const char TextHdr6[] PROGMEM = "File names & sizes ";
const char TextHdr7[] PROGMEM = "Initializing Complete";



const char* const PROGMEM  TextHdrTbl[] = {
                                           TextHdr0,  TextHdr1,  TextHdr2,  TextHdr3,
                                           TextHdr4,  TextHdr5, TextHdr6, TextHdr7
                                          };  



void ListFiles(){ 
// List files on SD
   volumesize = volume.blocksPerCluster(); 
   volumesize *= volume.clusterCount(); 
   volumesize *= 512;
   volumesize /= 1024;
   volumesize /= 1024;
   Serial.print(strcpy_P(ScreenLabel, (char*)pgm_read_word(&(TextHdrTbl[5])))); 
   Serial.println(volumesize);

//  list all files in the card with date and size
   Serial.println(strcpy_P(ScreenLabel, (char*)pgm_read_word(&(TextHdrTbl[7]))));
   root.openRoot(volume);  root.ls(LS_R | LS_SIZE);
}

void Loop() {
 List files();
}

The above works fine.

The code bellow will compile. But you will notice that the call for "list" function has been commented out.

//Program text stored in PROGMEM
const char TextHdr0[]  PROGMEM = "Kloster";
const char TextHdr1[]  PROGMEM = "AutoWorks";
const char TextHdr2[]  PROGMEM = "Task List: M=Select Map L=ListFiles E=Erase";
const char TextHdr3[]  PROGMEM = "G=InputArgs R=Read SD C=Convert D=Display Image";
const char TextHdr4[]  PROGMEM = "Display Image";
const char TextHdr5[] PROGMEM = "Screen Number 0-7";
const char TextHdr6[] PROGMEM = "File names & sizes ";
const char TextHdr7[] PROGMEM = "Initializing Complete";


const char* const PROGMEM  TextHdrTbl[] = {
                                           TextHdr0,  TextHdr1,  TextHdr2,  TextHdr3,
                                           TextHdr4,  TextHdr5, TextHdr6, TextHdr7
                                          };  


//  The definition on the line below appears to make the compiler happy.

void ListFiles(char ScreenLabel, const char* const PROGMEM TextHdrTbl){ 
// List files on SD
   volumesize = volume.blocksPerCluster();  
   volumesize *= volume.clusterCount(); 
   volumesize *= 512; 
   volumesize /= 1024; 
   volumesize /= 1024;
   Serial.print(strcpy_P(ScreenLabel, (char*)pgm_read_word(&(TextHdrTbl[5])))); 
   Serial.println(volumesize);

//  list all files in the card with date and size
   Serial.println(strcpy_P(ScreenLabel, (char*)pgm_read_word(&(TextHdrTbl[7]))));
   root.openRoot(volume);  root.ls(LS_R | LS_SIZE);
}

void Loop() {


 // List files(ScreenLabel, TextHdrTbl);
}

The function call line above will not compile.

Still trying to work this one out. "TextHdrTbl" is a pointer and it is also an array.

Again any help would be greatly appreciated.

I got it.

I did not put the "[]" around both arguments in the function declaration:

Wrong:

void ListFiles(char ScreenLabel, const char* const PROGMEM TextHdrTbl)

Correct:

void ListFiles(char ScreenLabel[], const char* const PROGMEM TextHdrTbl[])

Thanks anyway