Looking up products from table

Hi all,

I will try to explain what I have and where I am up to. I have a script which needs to look up a value from a table. This lookup is called from a byte array within the main code. For example, beaconPacket[125]

This is the structure I think I need with the table of products stored in 'products.c'

products.c

const product_id product_desc[] PROGMEM = {
  {0, "Unknown"},
  {1, "Bread"},
  {2, "Milk"},
  {3, "Butter"},
  {4, "Jam"},
  {5, "Coffee"}
};

So I would like to call the product from beaconPacket[125] along the lines of....

if beaconPacket[125] = 4, "Jam" is returned. 

Main code

#include "products.c" ;

const byte max_length = 18; //maximum length of product_name + 1 character for null
struct product_id {
  long product_number;
  char product_name[max_length];
};
char buf[max_length]; //buffer to copy product_name from PROGMEM to RAM

void setup()
{
  Serial.begin(115200);
}

// Main Loop
void loop() {
  for ( unsigned int i = 0; i < sizeof(product_desc) / sizeof(product_desc[0]); i++) {
    Serial.print(pgm_read_dword_near(&product_desc[i].product_number));
    Serial.print(F("\t")); //tab
    memcpy_P(&buf, &product_desc[i].product_name, max_length);
    Serial.println(buf);
  }
  delay(3000); //wait 3 seconds before repeating the loop
}

Would anyone be able to assist in getting this code to work as there are a few issues stopping it and I'm not sure where to start looking. The actual table currently consists of about 60 entries, it's just shortened here.

Many thanks for any help offered.

Such as ?

Where is this array defined?
What is in it?
Why are you doing the for loop in the loop function, it seems unnecessary.

You are including "products.c" BEFORE defining the product_id struct.

Move it after the definition (or move the definition of struct inside product.c)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.