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.