In a quest to back away from a 60% ram usage message I finally investigated PROGMEM. After a lot of stumbling and assistance I managed to get some strings into PROGMEM and bring them out for display on my 16x2 LCD. The template to create them is copied from Nick Gammon’s forum.
My next target is a list of error messages. The codes defining each type of error are fixed in the VL6180X TOF sensor. As can be seen below, these codes are not sequential. To relate error code to message text I created a 2D array (of structures) using the received error code as the argument for a search loop. It partly works - the loop will retrieve, and I can print to the LCD, the correct error code. The lcd.print() however, shows nothing. My latest attempt, shown below, is lifted from the AVR user manual.
Creation of the data and a short bit to test the function call
// LASER ERROR MESSAGE TEXT
//
typedef struct { // create structure
int8_t errorCode; // 6180 error code
char* errorText[22]; // error description
} laserErr_t;
const char LasErrSt0[] PROGMEM = "System Error";
const char LasErrSt1[] PROGMEM = "ECE Failure";
const char LasErrSt2[] PROGMEM = "Convergence fail";
const char LasErrSt3[] PROGMEM = "Range Ignore";
const char LasErrSt4[] PROGMEM = "SNR Error";
const char LasErrSt5[] PROGMEM = "Raw Underflow";
const char LasErrSt6[] PROGMEM = "Raw Overflow";
const char LasErrSt7[] PROGMEM = "Range Underflow";
const char LasErrSt8[] PROGMEM = "Range Overflow";
const laserErr_t laserMsg[2][9] PROGMEM = {
1, LasErrSt0,
6, LasErrSt1,
7, LasErrSt2,
8, LasErrSt3,
11, LasErrSt4,
12, LasErrSt5,
13, LasErrSt6,
14, LasErrSt7,
15, LasErrSt8,
};
byte noOfErrMsgs = (sizeof (laserMsg) / sizeof (laserErr_t)) / 2;
/*
END VARIABLE DECLARATIONS -- SETUP ROUTINE
*/
void setup() {
Serial.begin(9600); // for debug only
lcd.begin(16, 2);// open comms. with 1602 display unit
lcd.createChar(0, ramAtTop);
lcd.createChar(1, ramAtLoad);
lcd.createChar(2, ramAtBottom);
lcd.createChar(3, rotateCCW);
lcd.createChar(4, rotateCW);
// diagnostic_display();
status = 13;
displayLaserError(status);
while (1);
Function to print error messages
// variable i will hold index to code and text
// print buffer is declared as: char lcdPrintBufr[22];
void displayLaserError(int errNum) {
for ( int i = 0; i < noOfErrMsgs; i++) { // nine error messges currently
if (errNum == pgm_read_byte(&laserMsg[0][i])) {
memcpy_P(&lcdPrintBufr, (&laserMsg[1][i]), sizeof lcdPrintBufr);
//(&(mydata[i][j]));
break;
}
lcd.clear();
lcd.print("Laser Err no. ");
lcd.print(errNum);
lcd.setCursor(0, 01);
// memcpy_P(&lcdPrintBufr, &laserMsg[1][1], sizeof lcdPrintBufr);
lcd.print(lcdPrintBufr);
}
}
No doubt it’s something blindingly simple but I’m out of ideas. Throw me a bone?