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.

Anindo Ghosh
50.8k8 gold badges108 silver badges205 bronze badges
asked Jun 12, 2013 at 16:57
\$\endgroup\$
2
  • \$\begingroup\$ Sounds like you need to implement a stack. \$\endgroup\$ Commented Jun 12, 2013 at 17:01
  • \$\begingroup\$ possible duplicate of Implementing an I2C buffer in C \$\endgroup\$ Commented Jun 12, 2013 at 17:25

1 Answer 1

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));
answered Jun 12, 2013 at 19:47
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.