Csv to arduino array

Hello. I am fairly new to arduino, but am familiar with cpp. I have a cpp script that will take a csv and create an array from that csv file. I am wanting to do that same thing with my arduino code without using an SD card.

I am using the arduino to control a dc latching solenoid with the help of an h bridge. I am using a csv file as a way to organize when I want the solenoid to open and close. This is for a project, and the times are to be customizable by a user (times are not to be hard coded).

Each time the excel file is updated, it will be saved as a new csv file with the same file name as before. I am wondering if there is a way to attach this csv as a header file? Or is there a way to read a csv file while my computer is plugged into the arduino?

Instead of CSV, get Excel to create a C++ Header.

Then use that.

Is that as simple as "save as" in excel and then selecting "cpp header" in the file options?

I don't think so - you'd have to either layout the sheet so that it saves as text as a valid header, or do a macro.

Or write a C++ program on the PC which turns the CSV into a header.

C++ isn't usually considered a "scripting" languge

Only using a PC program to send the data over serial.

To create a "header file", most people write a short program that formats the data as an array declaration/definition statement, which can be included in your code. Or just modify the .csv text with an editor.

It is pretty simple to write a program that outputs text like this, for copy and paste into an include file named something like "data.h". There are a couple of web sites that will do this for small image files, like image2cpp

byte position_commands[][3] = {
{102,55,27},
{95, 57, 33},
...
{233,0,99}
};

1 Like

Alternative. Add an SD card reader, have your Arduino read the file. Then, no recompile or reload is required.

1 Like

@dplum7 ...

An example of text formatting...

  "parts": [ { "type": "wokwi-arduino-nano", "id": "nano", "top": -100, "left": 100, "rotate": 0, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb1", "top": 0, "left": 120, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb2", "top": 0, "left": 140, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb3", "top": 0, "left": 160, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb4", "top": 0, "left": 180, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb5", "top": 0, "left": 200, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb6", "top": 0, "left": 220, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb7", "top": 0, "left": 240, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb8", "top": 0, "left": 260, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb9", "top": 0, "left": 280, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb10", "top": 0, "left": 300, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb11", "top": 0, "left": 320, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb12", "top": 0, "left": 340, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb13", "top": 0, "left": 360, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb14", "top": 0, "left": 380, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb15", "top": 0, "left": 400, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb16", "top": 0, "left": 420, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb17", "top": 0, "left": 440, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb18", "top": 0, "left": 460, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb19", "top": 0, "left": 480, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb20", "top": 0, "left": 500, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb21", "top": 0, "left": 520, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb22", "top": 0, "left": 540, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb23", "top": 0, "left": 560, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb24", "top": 0, "left": 580, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb25", "top": 0, "left": 600, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb26", "top": 0, "left": 620, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb27", "top": 0, "left": 640, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb28", "top": 0, "left": 660, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb29", "top": 0, "left": 680, "rotate": 270, "attrs": {} },
    { "type": "wokwi-neopixel", "id": "rgb30", "top": 0, "left": 700, "rotate": 270, "attrs": {} }
   ],

  "connections": [
  ["rgb1:DOUT","rgb2:DIN","green",["h5","v-20"]],["rgb1:VSS","rgb2:VSS","black",["h0"]],["rgb1:VDD","rgb2:VDD","red",["v0"]],
  ["rgb2:DOUT","rgb3:DIN","green",["h5","v-20"]],["rgb2:VSS","rgb3:VSS","black",["h0"]],["rgb2:VDD","rgb3:VDD","red",["v0"]],
  ["rgb3:DOUT","rgb4:DIN","green",["h5","v-20"]],["rgb3:VSS","rgb4:VSS","black",["h0"]],["rgb3:VDD","rgb4:VDD","red",["v0"]],
  ["rgb4:DOUT","rgb5:DIN","green",["h5","v-20"]],["rgb4:VSS","rgb5:VSS","black",["h0"]],["rgb4:VDD","rgb5:VDD","red",["v0"]],
  ["rgb5:DOUT","rgb6:DIN","green",["h5","v-20"]],["rgb5:VSS","rgb6:VSS","black",["h0"]],["rgb5:VDD","rgb6:VDD","red",["v0"]],
  ["rgb6:DOUT","rgb7:DIN","green",["h5","v-20"]],["rgb6:VSS","rgb7:VSS","black",["h0"]],["rgb6:VDD","rgb7:VDD","red",["v0"]],
  ["rgb7:DOUT","rgb8:DIN","green",["h5","v-20"]],["rgb7:VSS","rgb8:VSS","black",["h0"]],["rgb7:VDD","rgb8:VDD","red",["v0"]],
  ["rgb8:DOUT","rgb9:DIN","green",["h5","v-20"]],["rgb8:VSS","rgb9:VSS","black",["h0"]],["rgb8:VDD","rgb9:VDD","red",["v0"]],
  ["rgb9:DOUT","rgb10:DIN","green",["h5","v-20"]],["rgb9:VSS","rgb10:VSS","black",["h0"]],["rgb9:VDD","rgb10:VDD","red",["v0"]],
  ["rgb10:DOUT","rgb11:DIN","green",["h5","v-20"]],["rgb10:VSS","rgb11:VSS","black",["h0"]],["rgb10:VDD","rgb11:VDD","red",["v0"]],
  ["rgb11:DOUT","rgb12:DIN","green",["h5","v-20"]],["rgb11:VSS","rgb12:VSS","black",["h0"]],["rgb11:VDD","rgb12:VDD","red",["v0"]],
  ["rgb12:DOUT","rgb13:DIN","green",["h5","v-20"]],["rgb12:VSS","rgb13:VSS","black",["h0"]],["rgb12:VDD","rgb13:VDD","red",["v0"]],
  ["rgb13:DOUT","rgb14:DIN","green",["h5","v-20"]],["rgb13:VSS","rgb14:VSS","black",["h0"]],["rgb13:VDD","rgb14:VDD","red",["v0"]],
  ["rgb14:DOUT","rgb15:DIN","green",["h5","v-20"]],["rgb14:VSS","rgb15:VSS","black",["h0"]],["rgb14:VDD","rgb15:VDD","red",["v0"]],
  ["rgb15:DOUT","rgb16:DIN","green",["h5","v-20"]],["rgb15:VSS","rgb16:VSS","black",["h0"]],["rgb15:VDD","rgb16:VDD","red",["v0"]],
  ["rgb16:DOUT","rgb17:DIN","green",["h5","v-20"]],["rgb16:VSS","rgb17:VSS","black",["h0"]],["rgb16:VDD","rgb17:VDD","red",["v0"]],
  ["rgb17:DOUT","rgb18:DIN","green",["h5","v-20"]],["rgb17:VSS","rgb18:VSS","black",["h0"]],["rgb17:VDD","rgb18:VDD","red",["v0"]],
  ["rgb18:DOUT","rgb19:DIN","green",["h5","v-20"]],["rgb18:VSS","rgb19:VSS","black",["h0"]],["rgb18:VDD","rgb19:VDD","red",["v0"]],
  ["rgb19:DOUT","rgb20:DIN","green",["h5","v-20"]],["rgb19:VSS","rgb20:VSS","black",["h0"]],["rgb19:VDD","rgb20:VDD","red",["v0"]],
  ["rgb20:DOUT","rgb21:DIN","green",["h5","v-20"]],["rgb20:VSS","rgb21:VSS","black",["h0"]],["rgb20:VDD","rgb21:VDD","red",["v0"]],
  ["rgb21:DOUT","rgb22:DIN","green",["h5","v-20"]],["rgb21:VSS","rgb22:VSS","black",["h0"]],["rgb21:VDD","rgb22:VDD","red",["v0"]],
  ["rgb22:DOUT","rgb23:DIN","green",["h5","v-20"]],["rgb22:VSS","rgb23:VSS","black",["h0"]],["rgb22:VDD","rgb23:VDD","red",["v0"]],
  ["rgb23:DOUT","rgb24:DIN","green",["h5","v-20"]],["rgb23:VSS","rgb24:VSS","black",["h0"]],["rgb23:VDD","rgb24:VDD","red",["v0"]],
  ["rgb24:DOUT","rgb25:DIN","green",["h5","v-20"]],["rgb24:VSS","rgb25:VSS","black",["h0"]],["rgb24:VDD","rgb25:VDD","red",["v0"]],
  ["rgb25:DOUT","rgb26:DIN","green",["h5","v-20"]],["rgb25:VSS","rgb26:VSS","black",["h0"]],["rgb25:VDD","rgb26:VDD","red",["v0"]],
  ["rgb26:DOUT","rgb27:DIN","green",["h5","v-20"]],["rgb26:VSS","rgb27:VSS","black",["h0"]],["rgb26:VDD","rgb27:VDD","red",["v0"]],
  ["rgb27:DOUT","rgb28:DIN","green",["h5","v-20"]],["rgb27:VSS","rgb28:VSS","black",["h0"]],["rgb27:VDD","rgb28:VDD","red",["v0"]],
  ["rgb28:DOUT","rgb29:DIN","green",["h5","v-20"]],["rgb28:VSS","rgb29:VSS","black",["h0"]],["rgb28:VDD","rgb29:VDD","red",["v0"]],
  ["rgb29:DOUT","rgb30:DIN","green",["h5","v-20"]],["rgb29:VSS","rgb30:VSS","black",["h0"]],["rgb29:VDD","rgb30:VDD","red",["v0"]]
   ],

From this code:

void parts() {
  Serial.print("  \"parts\": [ { \"type\": \"wokwi-arduino-nano\", \"id\": \"nano\", \"top\": -100, \"left\": ");
  Serial.print(mcu); // put the mcu at the DIN end of the NeoPixel string
  Serial.println(", \"rotate\": 0, \"attrs\": {} },"); // orient VSS and VDD for clean connection

  for (int i = 1; i < number + 1; i++ ) {
    Serial.print("    { \"type\": \"wokwi-neopixel\", \"id\": \"rgb");
    Serial.print(i);
    Serial.print("\", \"top\": 0, \"left\": ");
    Serial.print(i * spacing + 100);
    Serial.print(", \"rotate\": 270, \"attrs\": {} }"); // remove final comma

    if (i < number)
      Serial.print(","); // add final comma
    Serial.println();
  }
  Serial.println("  ],");
}

void connections() {
  Serial.println("  \"connections\": [");
  for (int i = 1; i < number ; i++ ) { // one less connection than number of neopixels
    Serial.print("  [\"rgb");
    Serial.print(i);
    Serial.print(":DOUT\",\"rgb");
    Serial.print(i + 1);
    Serial.print(":DIN\",\"green\",[\"h5\",\"v-20\"]],");

    Serial.print("[\"rgb");
    Serial.print(i);
    Serial.print(":VSS\",\"rgb");
    Serial.print(i + 1);
    Serial.print(":VSS\",\"black\",[\"h0\"]],");

    Serial.print("[\"rgb");
    Serial.print(i);
    Serial.print(":VDD\",\"rgb");
    Serial.print(i + 1);
    Serial.print(":VDD\",\"red\",[\"v0\"]]");

    if (i < number - 1) // one less connection than number of neopixels
      Serial.print(","); // add final comma when not final line
    Serial.println();
  }
  Serial.println("  ],");
}

Header files imply recompiling the source code. Is that what you want your user(s) to do?

The alternative can be "Excel Streamer" (you can do the search yourself :wink:) or PLX-DAQ version 2 - now with 64 bit support! (and further new features)

1 Like

I don't mind having to recompile the source code each time. This is for a mechanical senior design class, so the code just needs to function and show that we were thinking of the end user (not too many hurdles to just through to update the sprinkler times). I will explore those 2 options that you mentioned. Are those forms of serial communication?

As far as I know, yes. I've never used them (no excel).

I know this is a few months old, but I happened to be browsing.

"Excel Streamer" is an Excel add-in. It can live stream from Arduino into Excel. I forget if it can do the other way, but I wouldn't be surprised. You can search YouTube and find videos about Excel Streamer and how to use it.

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