简体   繁体   中英

How to create array and store values in it without knowing its size in Arduino

I'm trying to write Arduino code for receiver that receives hexadecimal values from another Arduino (Transmitter), then I want to check this values and store them in new array which I don't know its size, while transmitter sends values receiver will store them.

Values reaches successfully to receiver but I don't know how to store them in new array.

My receiver code is like this:

for (i = 0; i < len; i++)
{
 if (receivedValue[i]==0x60)
          {
    //digitalWrite(LED1,LOW); 
    // store receivedValue[i] in the new array
          }
          else
          {
            if(receivedValue[i]==0x61)
            {
            //digitalWrite(LED3,LOW); 
             // store receivedValue[i] in the new array
            }

            if (receivedValue[i]==0x62)
            {
            //digitalWrite(LED4,LOW); 
              // store receivedValue[i] in the new array
          }

          // any other receivedValue[i] dont do anything
        }
    }

LEDs works successfully as I want but how to store them in array?

Two approaches suggested:

  1. Array pre-defined with fixed size

     #define MAX_ITENS 50 // The size of buffer uint8_t buffer[MAX_ITENS]; // The Buffer uint8_t posBuffer = 0; // Pointer to actual position loop() { .... // Add data to buffer posBuffer++; if (posBuffer == MAX_ITENS) { // Buffer overflow - You can set position to 0 // or give some error } else { buffer[posBuffer] = data; // Save the data } } 
  2. Use a dynamic array library I like this one .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM