How to save data from analog input into an array and serial print it?

Hi everyone, good evening.

I’m confused about coding with array
I want to read the value from the analog input pin on Arduino due and then write in an array.

After that, i want to print to serial in a format of first line with values of voltage array and second line with values of current array.

How do i achieve that?
here is my uncompleted code, hope someone could fulfill it for me

void loop()
{
    int i,j;
    float voltage = 0;
    float current = 0;
    float primaryVoltage[256];
    float primaryCurrent[256];

   
    
    for (i = 0; i < 256; i++)
    {
      voltage = analogRead(A0);
      current = analogRead(A1);
      primaryVoltage[i] = (voltage * 0.6547);
      primaryCurrent[i] = (current * 0.0179); 
    }

OK, so it looks like you know how to put the values in the array. That part looks fine, although you didn't show the entire code so who knows if there is an error in the rest of it.

What part of this is giving you trouble? What have you tried so far? It looks like all you need to do is make another for loop and start calling Serial.print to put out the values.

Kevinzht:
Hi everyone, good evening.

I’m confused about coding with array
I want to read the value from the analog input pin on Arduino due and then write in an array.

After that, i want to print to serial in a format of first line with values of voltage array and second line with values of current array.

How do i achieve that?
here is my uncompleted code, hope someone could fulfill it for me

This example sketch should give you the results you want. Note that using “[b]int main (void)[/b]” is just the way I do it. You can easily do the same thing by putting all the code into “void setup (void)” and nothing in “void loop (void)”.

I just hate using setup/loop! :slight_smile:

int main (void)
{
    int i, j;
    float voltage = 0;
    float current = 0;
    float primaryVoltage[256];
    float primaryCurrent[256];

    char buffer [64]; // must be large enough for string size
    char dstrbuf[16]; // buffer for dtostrf

    init ();
    Serial.begin (115200);

///////////// just fill the arrays with dummy data for test ////////////
    for (i = 0; i < 256; i++) {
        // put in simulated data alternating 120 and 240 volts
        primaryVoltage[i] = (i % 2) ? 120.0 : 240.0;
        // put in simulated data alternating 5 and 8 amps
        primaryCurrent[i] = (i % 2) ? 5.0 : 8.0;
    }
////////////////////////////////////////////////////////////////////////

    for (i = 0; i < 256; i++) {
        dtostrf (primaryVoltage[i], 0, 2, dstrbuf); // float value to buffer
        sprintf (buffer, "Voltage at array index %d is %s\r\n", i, dstrbuf); // generate string into buffer
        Serial.print (buffer); // print the "voltafe" string
        dtostrf (primaryCurrent[i], 0, 2, dstrbuf); // float value to buffer
        sprintf (buffer, "Current at array index %d is %s\r\n", i, dstrbuf); // generate string into buffer
        Serial.print (buffer); // print the "voltage" string
        Serial.print ("\r\n"); // print a separator line if you want it
        delay (1000); // just a 1 sec delay so you can see it print
    }
    while (1);
}

You will get this as output:

[b]Voltage at array index 0 is 240.00
Current at array index 0 is 8.00

Voltage at array index 1 is 120.00
Current at array index 1 is 5.00

Voltage at array index 2 is 240.00
Current at array index 2 is 8.00
..............
..............
Voltage at array index 254 is 240.00
Current at array index 254 is 8.00

Voltage at array index 255 is 120.00
Current at array index 255 is 5.00
[/b]

To READ in data, basically do it the same way… use the FOR loop to generate the array index, read your analog values and then put the data into “primaryVoltage[index]” and “primaryCurrent[index]”. OK?

Hope this helps!

(edit to add); Technical note: I should have been able to use just an [b]sprintf[/b] line to format the entire string (using “%f” to do the floating point) but the Arduino IDE is setup to exclude the floating point support code to “save size”.

This is well and good, but they SHOULD at least put in a configuration option to enable or disable floating point support if the USER wants to use it.

I modified my IDE a lot… including adding floating point to my Preferences selection.

Anyway, to kludge around the lack of floating point, I had to use the “[b]dtostrf[/b]” function to convert the float values into a string, then add the string to the main output buffer.

This is my “Preferences” window:

(click for full size)
snapshot1.jpg