0
\$\begingroup\$

I am currently working with an Arduino Mega and a SainSmart LCD and I have gotten most of my project to work except for a problem I am currently having.

On my LCD screen, I have various buttons, and these buttons in my code, correlate with an array. Thus, when a user presses the button, the array stores that value.

I have created a one line array that contains 12 int values. The following is where I am getting stuck:

I would like to save the array and be able to recall it as needed.

I have created a page where there are "saved programs" and there are buttons that coordinate the order of how the arrays were saved.

I would like to be able to save the arrays and then when I press the button, the arrays can be called on and the task performed.

\$\endgroup\$
1
\$\begingroup\$

I believe your question actually has nothing to do with inputs, and everything to do with arrays?

What does "called on" mean? Do you want the values need to be preserved across power off? If so, use EEPROM. The Arduino EEPROM functions only do a byte at a time, so use the AVR ones.

#include <avr/eeprom.h>

int array[12];

// save the contents of 'array' persistently
eeprom_write_block((void *)0x20, array, sizeof(array));
// load the contents from eeprom back into array
eeprom_read_block(array, (void *)0x20, sizeof(array));

To store multiple variants of the array, use different EEPROM addresses, spaced sufficiently far apart (at least 24 bytes for an array of 12 double-byte ints, so 0x20+N*24 for various N.)

The 0x20 is there to allow you to store other information in the first 32 bytes. You can start storing your arrays at offset 0x0 instead if you want.

If the question is simply "how do I save the state of an array to somewhere else" then the answer is "memcpy()."

#include <string.h>

int array[12];
int save_1[12];
int save_2[12];

// save "array" to "save_1"
mempcy(array, save_1, sizeof(array));
// restore "save_2" to "array"
memcpy(save_2, array, sizeof(array));
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.