Understanding arrays

I have an UNO using an ESP8266 to communicate with my pc via processing. I have the programs working for sending messages at the moment, but I'm hoping to send my sensor data and graph it on the processing side.

I have data that I want to send constantly, but I have also values that I want to send specifically while a particular process is running so that I can compare it. I don't know if an array is the best way of doing this but I suspect something will have to be done to compile it on the arduino side within the timeframe or ill lose repeatability on the wifi transmission.

Having a quick look at a C++ tutorial of arrays, it seems you need to specify the size when you declare it because the memory needs to be set aside. How do I know how big my array needs to be for a specific time frame?

How do I know how big my array needs to be for a specific time frame?

It needs to be as big as it needs to be.

That'll be in your specification

trojanhawrs:
How do I know how big my array needs to be for a specific time frame?

that's your decision (within limits).

a common approach to transmitting an unlimited amount of data is to allocate 2 buffers: ping and pong. while data is accumulating in one buffer, the other buffer is transmitted. 256 byte buffers might be reasonable.

of course code needs to keep track of where to insert data into a buffer, which buffer is being used to accumulate, which is being transmitted and when to swap buffers. pointers to the buffers make this convenient.

the assumption is that it takes less time to transmit the data than accumulate it. otherwise your limited by available memory and it is transmitted after capture

Hi gcjr, thanks for the reply. It might be worth mentioning that this particular data set is going to be running a maximum of 5 seconds (i dont know if that makes any difference as to whether I need to add more complicated code in or not).

I take it when you specify the size you're not required to fill it?

the length of time doesn't answer the question of size. it also depends on the sampling rate.

once byte every msec or 5000 byte in 5 secs would exceed the sram capacity of an arduino uno

Buffer it is then, will start having a read up now.

If my sensor is being read by my analog pin 0-1023, is the most efficient variable for me to store that in a "byte"? I was looking at char but it seems that can only store up to 255

trojanhawrs:
Buffer it is then, will start having a read up now.

If my sensor is being read by my analog pin 0-1023, is the most efficient variable for me to store that in a "byte"? I was looking at char but it seems that can only store up to 255

It's answered in the reference section:
Arduino Reference - Arduino Reference. A 'byte' is an Arduino specific definition of unsigned char.

trojanhawrs:
If my sensor is being read by my analog pin 0-1023, is the most efficient variable for me to store that in a "byte"? I was looking at char but it seems that can only store up to 255

The char data type is meaningless if you're saving numeric values. Use a char for saving 8-bit characters, use the [

stdint

](https://en.cppreference.com/w/c/types/integer) integers if you need an integer of fixed size, such as int8_t (which is the integer equivalent of char on many platforms).

The byte data type is an Arduino-specific datatype that doesn't show the right intent either. It's fine for handling raw data such as network buffers, but not for numeric data.

If you want to store a 10-bit unsigned number, such as your analog reading, use uint16_t. It takes up two bytes, so you might need a data framing mechanism when you send them to another device.

Pieter

Thanks for all the input gents, all still a bit over my head at the moment. Can anyone recommend some good tutorials that might give me a better grounding or am I as well to keep chucking shit at the wall and seeing what sticks?

trojanhawrs:
Thanks for all the input gents, all still a bit over my head at the moment. Can anyone recommend some good tutorials that might give me a better grounding or am I as well to keep chucking shit at the wall and seeing what sticks?

It all depends on you and how you learn best. There are many options:

  • Examples in the Arduino IDE.
  • Online tutorials.
  • Books.
  • Classes at a community college.

What works best for you?

Two things that I guarantee won't work:

  • Throwing crap at the wall to see what sticks.
  • Copy / Paste programming.

trojanhawrs:
Thanks for all the input gents, all still a bit over my head at the moment. Can anyone recommend some good tutorials that might give me a better grounding

it's often time consuming to change, download, test, repeat. i use the the code to interactively run code by entering single character commands thru the serial monitor. it's takes a while to figure out what capabilities you need. the code makes it easy to add and change those capabilities

i've use this approach to run code that captures data and then dump it on the serial monitor. it allows parameters to be changed and then capture/dump more data.

i don't know of a tutorial to figure out what you need to do.

// pcRead - debugging using serial monitor

const char version [] = "PcRead 200416a";

int debug = 0;

// ---------------------------------------------------------
// toggle output bit
void
pinToggle (
    int pin)
{
    static int  bits = 0;
    int     bit = 1 << pin;

    if (debug)  {
        Serial.print ("pinToggle: ");
        Serial.println (pin);
    }

    if (bits & bit)  {
        digitalWrite (pin, LOW);
        bits &= ~bit;
    }
    else {
        digitalWrite (pin, HIGH);
        bits |= bit;
    }
}

// ---------------------------------------------------------
// toggle output bit
int
readString (
    char *s,
    int   maxChar )
{
    int  n = 0;

    Serial.print ("> ");
    do {
        if (Serial.available()) {
            int c    = Serial.read ();

            if ('\n' == c)
                break;

            s [n++] = c;
            if (maxChar == n)
                break;
        }
    } while (true);

    return n;
}

// -----------------------------------------------------------------------------
// process single character commands from the PC
#define MAX_CHAR  10
char s [MAX_CHAR] = {};

int  analogPin = 0;

void
pcRead (void)
{

    static int  val = 13;

    if (Serial.available()) {
        int c = Serial.read ();

        switch (c)  {
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            val = c - '0' + (10 * val);
            break;

        case 'A':
            analogPin = val;
            Serial.print   ("analogPin = ");
            Serial.println (val);
            val = 0;
            break;

        case 'D':
            debug ^= 1;
            break;

        case 'I':
            pinMode (val, INPUT);
            Serial.print   ("pinMode ");
            Serial.print   (val);
            Serial.println (" INPUT");
            val = 0;
            break;

        case 'O':
            pinMode (val, OUTPUT);
            Serial.print   ("pinMode ");
            Serial.print   (val);
            Serial.println (" OUTPUT");
            val = 0;
            break;

        case 'P':
            pinMode (val, INPUT_PULLUP);
            Serial.print   ("pinMode ");
            Serial.print   (val);
            Serial.println (" INPUT_PULLUP");
            val = 0;
            break;


        case 'a':
            Serial.print   ("analogRead: ");
            Serial.println (analogRead (val));
            val = 0;
            break;

        case 'c':
            digitalWrite (val, LOW);
            Serial.print   ("digitalWrite: LOW  ");
            Serial.println (val);
            val = 0;
            break;

        case 'p':
#if !defined(ARDUINO_ARCH_ESP32)
            analogWrite (analogPin, val);
            Serial.print   ("analogWrite: pin ");
            Serial.print   (analogPin);
            Serial.print   (", ");
            Serial.println (val);
            val = 0;
#endif
            break;

        case 'r':
            Serial.print   ("digitalRead: pin ");
            Serial.print   (val);
            Serial.print   (", ");
            Serial.println (digitalRead (val));
            val = 0;
            break;

        case 's':
            digitalWrite (val, HIGH);
            Serial.print   ("digitalWrite: HIGH ");
            Serial.println (val);
            val = 0;
            break;

        case 't':
            Serial.print   ("pinToggle ");
            Serial.println (val);
            pinToggle (val);
            val = 0;
            break;

        case 'v':
            Serial.print ("\nversion: ");
            Serial.println (version);
            break;

        case '\n':          // ignore
            break;

        case '"':
            while ('\n' != Serial.read ())     // discard linefeed
                ;

            readString (s, MAX_CHAR-1);
            Serial.println (s);
            break;

        case '?':
            Serial.println ("\npcRead:\n");
            Serial.println ("    [0-9] append to #");
            Serial.println ("    A # - set analog pin #");
            Serial.println ("    D # - set debug to #");
            Serial.println ("    I # - set pin # to INPUT");
            Serial.println ("    O # - set pin # to OUTPUT");
            Serial.println ("    P # - set pin # to INPUT_PULLUP");
            Serial.println ("    a # - analogRead (pin #)");
            Serial.println ("    c # - digitalWrite (pin #, LOW)");
            Serial.println ("    p # -- analogWrite (analogPin, #)");
            Serial.println ("    r # - digitalRead (pin #)");
            Serial.println ("    s   - digitalWrite (pin #, HIGH)");
            Serial.println ("    t   -- toggle pin # output");
            Serial.println ("    v   - print version");
            Serial.println ("    \"   - read string");
            Serial.println ("    ?   - list of commands");
            break;

        default:
            Serial.print ("unknown char ");
            Serial.println (c,HEX);
            break;
        }
    }
}

// -----------------------------------------------------------------------------
void
loop (void)
{
    pcRead ();
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin(115200);

    Serial.println (version);
#if defined(ARDUINO_ARCH_ESP32)
    Serial.println ("esp32");
#endif
}