Hi,
I am making a bar bot. I pretty much have the hardware part down. I bought a touchscreen and I want to store mixed drink objects on my Arduino so I can display all the properties of a "drink" on the screen. I don't need help with the touch screen, this is a standard C++ question.
I found a big list of drinks online in JSON format. I want to represent these json objects as structs in my Arduino code as I believe that will be the easiest way to reference them and their properties. The JSON structure looks like this:
{
"idDrink": "17834",
"strDrink": "Abbey Cocktail",
"strCategory": "Ordinary Drink",
"strIngredient1": "Gin",
"strIngredient2": "Orange bitters",
"strIngredient3": "Orange",
"strIngredient4": "Cherry",
"strMeasure1": "1 1/2 oz ",
"strMeasure2": "1 dash ",
"strMeasure3": "Juice of 1/4 ",
"strMeasure4": "1 "
}
There are a LOT of drinks in this list. So to save on memory I am trying to store them in PROGMEM, because even a mega can't handle the # of drinks I want to store. One memory saving technique I want to try is to first store a list of unique ingredient strings as a lot of the drinks share the same ingredients and I figured a reference to a char array would take up less memory than the string "vodka" duplicated 40+ times. I had no problem making the ingredients array:
const char string_54[] PROGMEM = "Blue Curacao";
const char string_55[] PROGMEM = "Blueberry Schnapps";
const char string_56[] PROGMEM = "Bourbon";
const char string_57[] PROGMEM = "Brandy";
...
const char *const ingredients[] PROGMEM = {
string_54,
string_55,
string_56
string_57
...
};
But I am having trouble referencing these strings in my "drink" structs (Which I also want to store in PROGMEM to save on memory). So far the only way I have gotten PROGMEM structs working is like this:
// I know this doesn't match the JSON structure from above, I am just trying to get an MVP working here first
struct MixedDrink {
int id;
char* ing1;
};
// Then instance a whole bunch of "MixedDrink"s in an array:
const MixedDrink drinksPROGMEM[] PROGMEM =
{
{1, "foo"},
{2, "String2"},
};
// MixedDrink struct in SRAM we will constantly overwrite with one from PROGMEM
MixedDrink drinksSRAM;
// Then here is an example of how I have been accessing them in the loop.
void loop() {
delay(500);
for( int i=0; i<(sizeof(drinksPROGMEM)/sizeof(MixedDrink)); i++)
{
memcpy_P( &drinksSRAM, &drinksPROGMEM[i], sizeof(MixedDrink));
Serial.println( drinksSRAM.id); // display prob A
Serial.println( drinksSRAM.ing1); // display prob B etc.
}
}
Could someone please show me how I would reference one of the ingredients[] strings in my drinksPROGMEM[] definitions? I know this doesn't work but what I am trying to achieve is something like this:
const char string_999[] PROGMEM = "My Ingredient String";
const char *const ingredients[] PROGMEM = {
string_999
}
struct MixedDrink {
int id;
char* ing1;
};
const MixedDrink drinksPROGMEM[] PROGMEM =
{
{1, ingredients[0]},
};
MixedDrink drinksSRAM;
void loop() {
delay(500);
// would only loop once in this example but whatever
for( int i=0; i<(sizeof(drinksPROGMEM)/sizeof(MixedDrink)); i++)
{
memcpy_P( &drinksSRAM, &drinksPROGMEM[i], sizeof(MixedDrink));
Serial.println( drinksSRAM.id); // displays 1
Serial.println( drinksSRAM.ing1); // displays "My Ingredient String"
}
}