# Employing C++ code for Arduino

I have Googled quite a lot regarding using a C++ program for uploading to Arduino and related, however I am confused now. Right, my problem is this:

I have written a C++ program which imports RGB data of a 24 bit bitmap (which was generated using Matlab) and now I would like to use this array (3 arrays - R,G,B) for an Arduino sketch to be used for a LED matrix project that I am working on. I am not exactly a programmer and hence had to resort to this path -- MATLAB to c++ and then Arduino, although this could be entirely done in C++ too (importing a bitmap and extracting pixel info). So, my C++ code spits out the 3 arrays and I am not sure how to get those into an Arduino sketch.

I read a lot about this and I understood this - As the Arduino "language" is C and C++ therefore it is possible to program an Arduino in C++/c. I need a maker file and a compiler. However my question is that isn't there a more non-programmer friendly way to get this done, some software or an IDE which supports Arduino programming in C++? I cannot understand much of this method, hence trying to bypass this.

If anybody's got any better method, I would be really glad if you could share it. I am also looking to use the Arduino package for MATLAB to send the array directly from there using serial communication. But I just wanted to make sure I at least tried this path.

Would really appreciate if somebody could offer some help. I am quite stuck here.

Can share any other info if required.

Thanks a lot!

EDIT 1:

To clarify as Nick Gammon pointed out in the comments below. In brief steps, this is what I have done till now:

1. Got a 24 bit bitmap (some random image)
2. Extracted the RGB data for each pixel using Matlab
3. Have 3 arrays of (for eg.) 122X300 dimensions whose every element has values between 0-255 (8 bit) and for R,G and B colors
4. Want to get these 3 arrays for use in Arduino sketch for further working
5. Stuck here - how to do that .....
6. So I did this - I imported that the RGB data into 3 separate text files and made a c++ program to read those arrays assuming that I should be able to then somehow use this program in Arduino sketch.
7. Now I am not sure what to do....my goal would be to have these 3 arrays (CRGB arrays, FastLED) in my arduino sketch which I can control and then upload to my Due.
8. On googling I found that to use c++ for arduino , you need the avr compiler and make file, which I want to avoid. Tried other IDEs too, Eclipse, PlatformIO too .. ummmm ... not that successful to be honest.
9. as per Paul's ans- Matlab to Arduino method I have to try. Hope I have clarified my process till now :)

EDIT 2:

C++ code for reading the above array (thought I could use the output in Arduino)

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
#include <sstream>
#include <stdlib.h>

const int HEIGHT = 4;

const int WIDTH = 5;

using namespace std;

int main(){

int array_req [HEIGHT][WIDTH];

string userinputprompt, filename;

userinputprompt = "COLORDATA FILENAME: ";

cout<<userinputprompt<<endl;

getline(cin,filename);

ifstream inputfile;

inputfile.open(filename.c_str());

for (int i = 0; i < HEIGHT; ++i)
{
std::string tmpString;
std::getline(inputfile, tmpString);

std::stringstream ss(tmpString);

for(int j=0;j < WIDTH; ++j)
{
ss >> array_req[i][j];
}
}

for(int p=0; p<HEIGHT; p++)
{
for(int q=0; q<WIDTH; q++)
{
cout<<array_req[p][q]<<" ";
}
cout<<"\n";
}

inputfile.close();
getchar();
return 0;

}

• Comments are not for extended discussion; this conversation has been moved to chat. – Nick Gammon Apr 13 '16 at 1:48
• @NickGammon dont have 20 rep for chat. Would have to wait. Sorry. – fattypanda Apr 13 '16 at 1:51
• You have 23 rep now. :) – Nick Gammon Apr 13 '16 at 1:57
• Although down voted twice by people who apparently are uninformed, many of the C++ problems you are describing as it pertains to your coding environment can be overcome by using Visual Studio free edition with Visual Micro add-in fro Arduino. It makes using C++ a snap for Arduino. – dinotom Apr 23 '16 at 10:19

You've got quite an interesting project.

Programming an Arduino (C++)

Arduino is C++ (or atleast a subset).

The Arduino IDE uses the GNU AVR-GCC compiler. And it takes care of all "make" files and stuff. You shouldn't really bother too much. Just paste the code right in, and fix whatever is broken ;)

Also, the Arduino IDE is basically click&install, click&compile/upload, I'm not sure why you're even trying all the other options.

For the sake of clarity/simplicity I'll give you an example of sending a single R/G/B pixel to the Arduino.

Matlab

You should set up your Matlab code something like this. I'm not a "Matlab guru" nor do I know whatever code you have. You should open a COM port and write to it.

s = serial('COM1','BAUD',115200);
fopen(s);
Pixel pix = getPixelFromBMP("image.bmp",0);
fprintf(s,'%s',pix.r.to_String()+pix.g.to_String()+pix.b.to_String());
fclose(s);


Arduino

In the Arduino IDE (withouth worrying about makefiles and toolchains).

You do the reverse of what you've done in your Matlab code. Read the serial input and write it to the LED strip. I'm quite sure this code won't compile, and there are a lot of fancier ways to read the serial buffer:

#include <ledstrip.h>

LedStrip ledStrip;

int incomingByteR = 0;   // for incoming serial data
int incomingByteG = 0;   // for incoming serial data
int incomingByteB = 0;   // for incoming serial data

void setup() {
Serial.begin(115200);     // opens serial port, sets data rate to 115200 bps
ledStrip.begin(1,RGB);    // initializes the ledstrip (RGB).
}

void loop() {
// send data only when you receive data:
if (Serial.available() > 3) {
// read the incoming byte:
// say what you got:
Serial.println(incomingByteR, DEC);
Serial.println(incomingByteG, DEC);
Serial.println(incomingByteB, DEC);
ledStrip.pixel[0].r = incomingByteR;
ledStrip.pixel[0].g = incomingByteG;
ledStrip.pixel[0].b = incomingByteB;
ledStrip.show();
}
}


C++

So, you don't really need C++ (other than to program the Arduino). You could interchange Matlab for C++ or Arduino for pure AVR C++, but I wouldn't recommend it.

• wow! your answer did clear some of my doubt. Thanks! I just realised when I was trying to use a cpp code for arduino in Codeblocks that I have to eventually go the hard way - Avr compliler,make files etc. etc...installed WinAVR and got lost again. I guess Matlab should save me. Let me try with it, your code and concept and will update here maybe tom. Hope that's fine :) – fattypanda Apr 12 '16 at 19:17
• Everything is fine. You can consider accepting this as an answer. If you run in other problems, you could create a new question. But I indeed find your project interesting. – Paul Apr 12 '16 at 19:22
• Hmmm...Right mate. I will accept it..no issues. I thought creating another similar question would be frowned upon ;) I will look into it and get back on this later. need some time. Thanks! – fattypanda Apr 12 '16 at 19:34
• @fattypanda I'm not such a frowner ;) But it's very likely you'll run into a problem that's completely different. If you then update/change the question, the inital problem might get lost. If you'd like to discuss the project or minor questions related to it. I'd be happy to answer them in chat (chat.stackexchange.com/rooms/38302/…) – Paul Apr 12 '16 at 19:45
• lol yes. .pretty nice to see though you worked with APA102. Let me see how to get around this (+2 rep min) – fattypanda Apr 12 '16 at 20:27

Here's a sample program you could use as reference, just replace the values in the array and adjust VALUES_NUMBER accordingly.

Disclaimer: I didn't try to compile/run it, but it should give you an idea of how to use the values you already have.

// These are Arduino Pins that support "analog" output.
const unsigned short ANALOG_RED_PIN = 3;
const unsigned short ANALOG_GREEN_PIN = 5;
const unsigned short ANALOG_BLUE_PIN = 6;

const unsigned int VALUES_NUMBER = 12;

const unsigned short REDS[VALUES_NUMBER] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B
};

const unsigned short GREENS[VALUES_NUMBER] = {
0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05
};

const unsigned short BLUES[VALUES_NUMBER] = {
0x09, 0x0A, 0x0B, 0x03, 0x04, 0x05,
0x00, 0x01, 0x02, 0x06, 0x07, 0x08
};

void setup() {
pinMode(analogRed, OUTPUT);
pinMode(analogGreen, OUTPUT);
pinMode(analogBlue, OUTPUT);
}

void loop() {
unsigned int counter;
for (counter = 0; counter < VALUES_NUMBER; counter++) {
analogWrite(ANALOG_RED_PIN, REDS[counter]);
analogWrite(ANALOG_GREEN_PIN, GREENS[counter]);
analogWrite(ANALOG_BLUE_PIN, BLUES[counter]);
delay(1000); //sleep 1 second
}
}

• .thanks for the reply. Your code is a very simplified version. However, its my mistake, apologies..i didnt mention..I am implementing this project using APA102 led strips which has a data and a clock pin. So, as far as my knowledge goes, control of strip using individual analog pins is not possible. Also, the second part of the code where the data is passed on to the leds, I am done with that part. Its the first part where I am stuck. Acquiring of data in 3 different arrays. Rest is done using the fantastic FastLED lib. :) – fattypanda Apr 12 '16 at 19:08

As the arduino "language" is C and C++ therefore it is possible to program an Arduino in C++/c. I need a maker file and a compiler. However my question is that isn't there a more non-programmer friendly way to get this done, some software or an IDE which supports Arduino programming in C++?

Yes, it's called the Arduino IDE. You plug your board into the USB port, write your program, or use an existing example, and then hit the Upload button.

• lol...I guess you misconstrued my question :P I meant that I wanted to be more flexible with the Arduino IDE by incorporating c++ environment. I was sort of handicapped (most probably because I am a non-programmer). Couldn't relate the output of a c++ program (or for that matter Matlab) directly into Arduino sketch. I have been using Arduino IDE for the last couple of months :) – fattypanda Apr 12 '16 at 23:07
• I'm not sure where this is heading. In the Arduino IDE you are programming in C++. You don't need to install or change anything to use it - or to "incorporate the C++ environment". It's just there. – Nick Gammon Apr 12 '16 at 23:18

Use the Visual Studio free community edition (or the paid versions if you already have them) with the Visual Micro extension for Arduino. Makes life a lot easier since you have indicated your desire to write in C++, and you get true debugging and intellisense as well, which you DO NOT get with the aforementioned IDE's.

• Could you please tell me how this is relevant to this question? – Avamander Apr 13 '16 at 18:30
• Your first line, I have Googled quite a lot regarding using a C++ program for uploading to Arduino and related, however I am confused now. Right, my problem is this:....and that is what I answered – dinotom Apr 15 '16 at 1:18
• There is nothing about needing debugging in the original question. If you wanted to say that one can use VS CE then edit the answer appropriately. – Avamander Apr 15 '16 at 10:13
• @Avamander....is my answer now more suitable for you. – dinotom Apr 18 '16 at 10:28