# C++ code converting to Arduino

Hi all,

I’m new on Arduino so I’d like to ask some help the expert like you.

The problem is Converting C++ to Arduino.
I couldn’t find out what I’m missing or wrong.

This is my Arduino code and original C++ code is attached.
(Program is about Curve fitting, find out curve function from data)

``````#define maxnpts 13	/*Maximum data pairs - increase if desired */
#define nterms 3     /*Number of parameters to be fit*/

int param, iteration, nloops, n, cycle, nfree;
int npts;	/* Number of data pairs */
double x[maxnpts], y[maxnpts], sigmay[maxnpts]; /*x,y,y, uncertainty*/
double weight[maxnpts];  /*Weighting factor*/
double yfit[maxnpts]; /*calculated values of y*/
double a[nterms], sigmaa[nterms], b[nterms]; /*a[i]=c[i] params*/
double beta[nterms], c[nterms]; /*To be fit by program*/
double finala[nterms], lastsigmaa[nterms];
double alpha[nterms][nterms], arry[nterms][nterms];
double aug[nterms][nterms*2];	/*For matrix inversion */
double deriv[maxnpts][nterms];	/*Derivatives */
double flambda; /*Proportion of gradient search(=0.001 at start)*/
double chisq; /*Variance of residuals in curve fit*/
double chisq1, fchisq, sy;

void curve(void)	/*the main routine*/
{
int i;
float a1, a2, a3;
for (i=0; i<nterms; i++){
a[i] = 1;
}
flambda = 0.001; iteration = 0; cycle = 0;
curvefit(npts);
iteration++;
Serial.println("A1");
Serial.println(a[1]);
Serial.println("A2");
Serial.println(a[2]);
Serial.println("A3");
Serial.println(a[3]);
//      displays();
}

/******************************/
/*The function you are fitting*/
/******************************/
float func(int i)
{
int loops;  float value;
if (param==1){
for(loops=0; loops<nterms; loops++)	c[loops] = b[loops];
} else {
for(loops=0; loops<nterms; loops++)	c[loops] = a[loops];
}

value = c[2]*x[i] + c[1]/x[i] + c[0];	/*van deemter equation*/
return(value);
}

{
int n=0;
for(n=0; n<=maxnpts ; n++){
x[n] = Latti[n];
y[n] = Long[n];

Serial.println(Latti[n]);
Serial.println("Xxxxxxxxxxxx");
Serial.println(x[n]);
}
npts = n - 1;
update_data();
}

void chisquare(void)
{
int i;
fchisq = 0;
for (i=0; i<npts; i++) {
fchisq += weight[i] * (y[i] - yfit[i])*(y[i]-yfit[i]);
}
fchisq /= nfree;
}

void calcderivative(void)
{
int i, m;	float atemp, delta;
for (m=0; m<nterms; m++){
atemp = a[m]; delta = fabs(a[m]/100000); a[m] = atemp+delta;
for (i=0; i<npts; i++) deriv[i][m] = (func(i) - yfit[i]) / delta;
a[m] = atemp;
}
}

void matrixinvert(void)
{
int i,j,k,ik[nterms], jk[nterms];  float rsave, amax;
for (k=0; k<nterms; k++) {
amax = 0.0;
for (i=k; i<nterms; i++) {
for (j=k; j<nterms; j++) {
if (abs(amax) <= abs(arry[i][j])) {
amax = arry[i][j];
ik[k] = i; jk[k] = j;
}
}
}
i = ik[k];
if (i>k) {
for (j=0; j<nterms; j++){
rsave = arry[k][j];
arry[k][j] = arry[i][j]; arry[i][j]=-1*rsave;
}
}
j = jk[k];
if (j>k) {
for (i=0; i<nterms; i++){
rsave = arry[i][k];
arry[i][k] = arry[i][j]; arry[i][j]=-1*rsave;
}
}
for (i=0; i<nterms; i++) {
if (i !=k) {
arry[i][k] = -1*arry[i][k] / amax;
}
}
for (i=0; i<nterms; i++) {
for (j=0; j<nterms; j++) {
if (j !=k && i !=k) {
arry[i][j] = arry[i][j] + arry[i][k]*arry[k][j];
}
}
}
for (j=0; j<nterms; j++) {
if (j !=k) {
arry[k][j] = arry[k][j] / amax;
}
}
arry[k][k] = 1/amax;
}
for (k=nterms-1; k>-1; k--) {
j = ik[k];
if (j>k) {
for (i=0; i<nterms; i++){
rsave = arry[i][k];
arry[i][k] = -1*arry[i][j];		arry[i][j] = rsave;
}
}
i = jk[k];
if (i>k) {
for (j=0; j<nterms; j++){
rsave = arry[k][j];
arry[k][j] = -1*arry[i][j];		arry[i][j] = rsave;
}
}
}
}

void curvefit(int npoints)
{
int i, j, k;
nfree = npoints - nterms;
for (j=0; j<nterms; j++){
b[j] = beta[j] = 0;
for (k=0;k<+j; k++)	alpha[j][k] = 0;
}
param = 0;
for (i=0; i<npoints; i++)		yfit[i] = func(i);
chisquare();
chisq1 = fchisq;
calcderivative();
for (i=0; i<npoints; i++) {
for (j=0; j<npoints; j++) {
beta[j] += weight[i]*(y[i]-yfit[i])*deriv[i][j];
for (k=0; k<=j; k++)
alpha[j][k] += (weight[i]*deriv[i][j]*deriv[i][k]);
}
}
for (j=0; j<nterms; j++) {
for (k=0; k<=j; k++) alpha[k][j] = alpha[j][k];
}
nloops = 0;
do {
param = 1;
for (j=0; j<nterms; j++) {
for (k=0; k<nterms; k++)
arry[j][k] = alpha[j][k] / sqrt(alpha[j][j]*alpha[k][k]);
arry[j][j] = flambda +1;
}
matrixinvert();
for (j=0; j<nterms; j++) {
b[j] = a[j];
for (k=0; k<nterms; k++)
b[j] += beta[k]*arry[j][k] / sqrt(alpha[j][j]*alpha[k][k]);
}
for (i=0; i<npoints; i++)	yfit[i] = func(i);
chisquare();
if((chisq1	-fchisq)<0)	flambda *= 10;
nloops++;
} while (fchisq > chisq1);
for (j=0; j<nterms; j++) {
a[j] = b[j];
sigmaa[j] = sqrt(arry[j][j] / alpha[j][j]);
}
flambda /= 10;
}

void update_data(void)
{
int i;[
for (i=0; i<npts; i++)weight[i] = 1 / (sigmay[i]*sigmay[i]);
}
}
``````

curve_fit_modi.c (5.62 KB)

You go first. Share your knowledge of what the problem is.

Why don’t you get the compile to check your code, then share the results

One problem seems to be a programmer who is unaware of the target environment he is coding for.

OK I will share a bit. Click on modify above your post. Select the code and hit the # icon, then save it. Then we can all see more clearly what your code is.

The problem is C++ code find the correct function, but Arduino is not.
No result came out on Arduino code.
I just want to check I’m using some wrong command on my code.

This is my first post the code, so it’s not familiar to me. Sorry about that.

This is the original C++ code,

``````#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define maxnpts 100	/*Maximum data pairs - increase if desired */

/* Change nterms to the number of parameters to be fit in your equation */
/*******************************************************/
#define nterms 3     /*Number of parameters to be fit*/
/*******************************************************/

int param, iteration, nloops, n, cycle, nfree;
int npts;	/* Number of data pairs */
long double x[maxnpts], y[maxnpts], sigmay[maxnpts]; /*x,y,y, uncertainty*/
long double weight[maxnpts];  /*Weighting factor*/
long double yfit[maxnpts]; /*calculated values of y*/
long double a[nterms], sigmaa[nterms], b[nterms]; /*a[i]=c[i] params*/
long double beta[nterms], c[nterms]; /*To be fit by program*/
long double finala[nterms], lastsigmaa[nterms];
long double alpha[nterms][nterms], arry[nterms][nterms];
long double aug[nterms][nterms*2];	/*For matrix inversion */
long double deriv[maxnpts][nterms];	/*Derivatives */
long double flambda; /*Proportion of gradient search(=0.001 at start)*/
long double chisq; /*Variance of residuals in curve fit*/
long double chisq1, fchisq, sy;
char errorchoice;
FILE *fp;

void calcderivative(void), matrixinvert(void);
void curvefit(int npoints), display(void), print_data(void);

int main(void)	/*the main routine*/
{
int i;
for (i=0; i<nterms; i++){
a[i] = 1;
}
flambda = 0.001; iteration = 0; cycle = 0;
curvefit(npts);
iteration++;
display();
return 0;
}

/******************************/
/*The function you are fitting*/
/******************************/
long double func(int i)
{
int loop;  long double value;
if (param==1){
for(loop=0; loop<nterms; loop++)	c[loop] = b[loop];
} else {
for(loop=0; loop<nterms; loop++)	c[loop] = a[loop];
}

value = c[2]*x[i] + c[1]/x[i] + c[0];	/*van deemter equation*/
return(value);
}

{
int n=0;
printf("\nPlease enter the name of the data file: ");
gets(filename);
printf("\n");
fp = fopen(filename, "rb");
for(n=0; !feof(fp); n++){
if (errorchoice == '1') sigmay[n] = 1.0;
}
fclose(fp);
npts = n - 1;
print_data();
}

void chisquare(void)
{
int i;
fchisq = 0;
for (i=0; i<npts; i++) {
fchisq += weight[i] * (y[i] - yfit[i])*(y[i]-yfit[i]);
}
fchisq /= nfree;
}

void calcderivative(void)
{
int i, m;	long double atemp, delta;
for (m=0; m<nterms; m++){
atemp = a[m]; delta = fabs(a[m]/100000); a[m] = atemp+delta;
for (i=0; i<npts; i++) deriv[i][m] = (func(i) - yfit[i]) / delta;
a[m] = atemp;
}
}

void matrixinvert(void)
{
int i,j,k,ik[nterms], jk[nterms];  long double rsave, amax;
for (k=0; k<nterms; k++) {
amax = 0.0;
for (i=k; i<nterms; i++) {
for (j=k; j<nterms; j++) {
if (abs(amax) <= abs(arry[i][j])) {
amax = arry[i][j];
ik[k] = i; jk[k] = j;
}
}
}
i = ik[k];
if (i>k) {
for (j=0; j<nterms; j++){
rsave = arry[k][j];
arry[k][j] = arry[i][j]; arry[i][j]=-1*rsave;
}
}
j = jk[k];
if (j>k) {
for (i=0; i<nterms; i++){
rsave = arry[i][k];
arry[i][k] = arry[i][j]; arry[i][j]=-1*rsave;
}
}
for (i=0; i<nterms; i++) {
if (i !=k) {
arry[i][k] = -1*arry[i][k] / amax;
}
}
for (i=0; i<nterms; i++) {
for (j=0; j<nterms; j++) {
if (j !=k && i !=k) {
arry[i][j] = arry[i][j] + arry[i][k]*arry[k][j];
}
}
}
for (j=0; j<nterms; j++) {
if (j !=k) {
arry[k][j] = arry[k][j] / amax;
}
}
arry[k][k] = 1/amax;
}
for (k=nterms-1; k>-1; k--) {
j = ik[k];
if (j>k) {
for (i=0; i<nterms; i++){
rsave = arry[i][k];
arry[i][k] = -1*arry[i][j];		arry[i][j] = rsave;
}
}
i = jk[k];
if (i>k) {
for (j=0; j<nterms; j++){
rsave = arry[k][j];
arry[k][j] = -1*arry[i][j];		arry[i][j] = rsave;
}
}
}
}

void curvefit(int npoints)
{
int i, j, k;
nfree = npoints - nterms;
for (j=0; j<nterms; j++){
b[j] = beta[j] = 0;
for (k=0;k<+j; k++)	alpha[j][k] = 0;
}
param = 0;
for (i=0; i<npoints; i++)		yfit[i] = func(i);
chisquare();
chisq1 = fchisq;
calcderivative();
for (i=0; i<npoints; i++) {
for (j=0; j<npoints; j++) {
beta[j] += weight[i]*(y[i]-yfit[i])*deriv[i][j];
for (k=0; k<=j; k++)
alpha[j][k] += (weight[i]*deriv[i][j]*deriv[i][k]);
}
}
for (j=0; j<nterms; j++) {
for (k=0; k<=j; k++) alpha[k][j] = alpha[j][k];
}
nloops = 0;
do {
param = 1;
for (j=0; j<nterms; j++) {
for (k=0; k<nterms; k++)
arry[j][k] = alpha[j][k] / sqrt(alpha[j][j]*alpha[k][k]);
arry[j][j] = flambda +1;
}
matrixinvert();
for (j=0; j<nterms; j++) {
b[j] = a[j];
for (k=0; k<nterms; k++)
b[j] += beta[k]*arry[j][k] / sqrt(alpha[j][j]*alpha[k][k]);
}
for (i=0; i<npoints; i++)	yfit[i] = func(i);
chisquare();
if((chisq1	-fchisq)<0)	flambda *= 10;
nloops++;
} while (fchisq > chisq1);
for (j=0; j<nterms; j++) {
a[j] = b[j];
sigmaa[j] = sqrt(arry[j][j] / alpha[j][j]);
}
flambda /= 10;
}

void print_data(void)
{
int i;
for (i=0; i<npts; i++) {
weight[i] = 1 / (sigmay[i]*sigmay[i]);
}
}

void display(void)
{
int i;
printf("\nIteration #%d\n", iteration);
for (i=0; i<nterms; i++) {
printf("A[%3d] = %- #12.8Lf\n", i, a[i]);
finala[i] = a[i];
}
printf("Sum of squares of residuals = %- #12.8Lf\n", fchisq*nfree);
sy = sqrt(fchisq);
}
``````

This is my Arduino Code.

``````#define maxnpts 13	/*Maximum data pairs - increase if desired */
#define nterms 3     /*Number of parameters to be fit*/

int param, iteration, nloops, n, cycle, nfree;
int npts;	/* Number of data pairs */
double x[maxnpts], y[maxnpts], sigmay[maxnpts]; /*x,y,y, uncertainty*/
double weight[maxnpts];  /*Weighting factor*/
double yfit[maxnpts]; /*calculated values of y*/
double a[nterms], sigmaa[nterms], b[nterms]; /*a[i]=c[i] params*/
double beta[nterms], c[nterms]; /*To be fit by program*/
double finala[nterms], lastsigmaa[nterms];
double alpha[nterms][nterms], arry[nterms][nterms];
double aug[nterms][nterms*2];	/*For matrix inversion */
double deriv[maxnpts][nterms];	/*Derivatives */
double flambda; /*Proportion of gradient search(=0.001 at start)*/
double chisq; /*Variance of residuals in curve fit*/
double chisq1, fchisq, sy;

void curve(void)	/*the main routine*/
{
int i;
float a1, a2, a3;
for (i=0; i<nterms; i++){
a[i] = 1;
}
flambda = 0.001; iteration = 0; cycle = 0;
curvefit(npts);
iteration++;
Serial.println("A1");
Serial.println(a[1]);
Serial.println("A2");
Serial.println(a[2]);
Serial.println("A3");
Serial.println(a[3]);
//      displays();
}

/******************************/
/*The function you are fitting*/
/******************************/
float func(int i)
{
int loops;  float value;
if (param==1){
for(loops=0; loops<nterms; loops++)	c[loops] = b[loops];
} else {
for(loops=0; loops<nterms; loops++)	c[loops] = a[loops];
}

value = c[2]*x[i] + c[1]/x[i] + c[0];	/*van deemter equation*/
return(value);
}

{
int n=0;
for(n=0; n<=maxnpts ; n++){
x[n] = Latti[n];
y[n] = Long[n];

Serial.println(Latti[n]);
Serial.println("Xxxxxxxxxxxx");
Serial.println(x[n]);
}
npts = n - 1;
update_data();
}

void chisquare(void)
{
int i;
fchisq = 0;
for (i=0; i<npts; i++) {
fchisq += weight[i] * (y[i] - yfit[i])*(y[i]-yfit[i]);
}
fchisq /= nfree;
}

void calcderivative(void)
{
int i, m;	float atemp, delta;
for (m=0; m<nterms; m++){
atemp = a[m]; delta = fabs(a[m]/100000); a[m] = atemp+delta;
for (i=0; i<npts; i++) deriv[i][m] = (func(i) - yfit[i]) / delta;
a[m] = atemp;
}
}

void matrixinvert(void)
{
int i,j,k,ik[nterms], jk[nterms];  float rsave, amax;
for (k=0; k<nterms; k++) {
amax = 0.0;
for (i=k; i<nterms; i++) {
for (j=k; j<nterms; j++) {
if (abs(amax) <= abs(arry[i][j])) {
amax = arry[i][j];
ik[k] = i; jk[k] = j;
}
}
}
i = ik[k];
if (i>k) {
for (j=0; j<nterms; j++){
rsave = arry[k][j];
arry[k][j] = arry[i][j]; arry[i][j]=-1*rsave;
}
}
j = jk[k];
if (j>k) {
for (i=0; i<nterms; i++){
rsave = arry[i][k];
arry[i][k] = arry[i][j]; arry[i][j]=-1*rsave;
}
}
for (i=0; i<nterms; i++) {
if (i !=k) {
arry[i][k] = -1*arry[i][k] / amax;
}
}
for (i=0; i<nterms; i++) {
for (j=0; j<nterms; j++) {
if (j !=k && i !=k) {
arry[i][j] = arry[i][j] + arry[i][k]*arry[k][j];
}
}
}
for (j=0; j<nterms; j++) {
if (j !=k) {
arry[k][j] = arry[k][j] / amax;
}
}
arry[k][k] = 1/amax;
}
for (k=nterms-1; k>-1; k--) {
j = ik[k];
if (j>k) {
for (i=0; i<nterms; i++){
rsave = arry[i][k];
arry[i][k] = -1*arry[i][j];		arry[i][j] = rsave;
}
}
i = jk[k];
if (i>k) {
for (j=0; j<nterms; j++){
rsave = arry[k][j];
arry[k][j] = -1*arry[i][j];		arry[i][j] = rsave;
}
}
}
}

void curvefit(int npoints)
{
int i, j, k;
nfree = npoints - nterms;
for (j=0; j<nterms; j++){
b[j] = beta[j] = 0;
for (k=0;k<+j; k++)	alpha[j][k] = 0;
}
param = 0;
for (i=0; i<npoints; i++)		yfit[i] = func(i);
chisquare();
chisq1 = fchisq;
calcderivative();
for (i=0; i<npoints; i++) {
for (j=0; j<npoints; j++) {
beta[j] += weight[i]*(y[i]-yfit[i])*deriv[i][j];
for (k=0; k<=j; k++)
alpha[j][k] += (weight[i]*deriv[i][j]*deriv[i][k]);
}
}
for (j=0; j<nterms; j++) {
for (k=0; k<=j; k++) alpha[k][j] = alpha[j][k];
}
nloops = 0;
do {
param = 1;
for (j=0; j<nterms; j++) {
for (k=0; k<nterms; k++)
arry[j][k] = alpha[j][k] / sqrt(alpha[j][j]*alpha[k][k]);
arry[j][j] = flambda +1;
}
matrixinvert();
for (j=0; j<nterms; j++) {
b[j] = a[j];
for (k=0; k<nterms; k++)
b[j] += beta[k]*arry[j][k] / sqrt(alpha[j][j]*alpha[k][k]);
}
for (i=0; i<npoints; i++)	yfit[i] = func(i);
chisquare();
if((chisq1	-fchisq)<0)	flambda *= 10;
nloops++;
} while (fchisq > chisq1);
for (j=0; j<nterms; j++) {
a[j] = b[j];
sigmaa[j] = sqrt(arry[j][j] / alpha[j][j]);
}
flambda /= 10;
}

void update_data(void)
{
int i;
for (i=0; i<npts; i++) {
weight[i] = 1 / (sigmay[i]*sigmay[i]);
}
}
``````

If you really want help, you'll make your code readable. Modify your posts. Select the code, and press the # button to put the text in a scroll box.

Where are Latti and Long defined?

You have a lot of arrays, and very little memory. I'm going to guess that you are running out of memory.

larslee: The problem is C++ code find the correct function, but Arduino is not. No result came out on Arduino code. I just want to check I'm using some wrong command on my code.

The problem sits just behind your keyboard. You have 2k of SRAM for both heap and stack, and -all- your variables (read: arrays) load at startup (since you don't use PROGMEM).

One problem seems to be a programmer who is unaware of the target environment he is coding for.

Here's a clue: you're not running this on a PC.

This is my Arduino Code. The problem is C++ code find the correct function, but Arduino is not.

There is no such thing as "Arduino Code." Arduino is coded in C++.

``````int param, iteration, nloops, n, cycle, nfree;
12 bytes

int npts;   /* Number of data pairs */
2 bytes

double x[maxnpts], y[maxnpts], sigmay[maxnpts]; /*x,y,y, uncertainty*/
156 Bytes

double weight[maxnpts];  /*Weighting factor*/
52 Bytes

double yfit[maxnpts]; /*calculated values of y*/
52 Bytes

double a[nterms], sigmaa[nterms], b[nterms]; /*a=c params*/
36 Bytes

double beta[nterms], c[nterms]; /*To be fit by program*/
24 Bytes

double finala[nterms], lastsigmaa[nterms];
24 bytes

double alpha[nterms][nterms], arry[nterms][nterms];
72 Bytes  // someone may need to check me on this one

double aug[nterms][nterms*2];   /*For matrix inversion */
72 Bytes

double deriv[maxnpts][nterms];   /*Derivatives */
36 Bytes

double flambda; /*Proportion of gradient search(=0.001 at start)*/
4 Bytes

double chisq; /*Variance of residuals in curve fit*/
4 Bytes

double chisq1, fchisq, sy;
12 Bytes
``````

Right out of the gate you are using 558 bytes of SRAM be mindful of that.

If you are compiling this code in the Arduino IDE you should have a void setup() function that does any one time operations (like Serial.begin()) and a void loop() which is much like a void main(). Neither of those appear in the code you posted. Additionally there are many places in your code where you refer to arrays like 'a' and 'x' but don't give any indexes. So the code you have posted doesn't even compile.

Sorry that I didn't mentioned this is just Sub-function of program. so Latti & Long are defined Initially, void loop() & void main() are defined as well. this function code go in to the ' void loop() '

I'm using 'Ardupilot Mega' (126kb) board, and I'm using only 15kb except this code. so I don't think this is the capacity problem.

As I told, I need to check that I put wrong command or unusual expression on the code.

larslee: Sorry that I didn't mentioned this is just Sub-function of program.

Posting only parts of code makes it difficult to help troubleshoot. You might have identified the part that is not working, but the cause could be elsewhere.

larslee: I'm using 'Ardupilot Mega' (126kb) board, and I'm using only 15kb except this code.so I don't think this is the capacity problem.

We are not suggesting a capacity problem. Code is stored in Flash Memory which is 128kb. Your program runs in SRAM which is only 8k... The complier does not report or check if you are using too much SRAM.

larslee: As I told, I need to check that I put wrong command or unusual expression on the code.

The code, as posted, has many syntax errors. Most members of this forum will not troubleshoot code that does not compi unless the question is "why doesn't this compile?".

I'm pretty sure the other code is fine, it's running well and I tested it.

How can I check the SRAM of my hardware? Is there any other way to reduce the array of my code to reduce SRAM?

About the syntax errors, that's the problem, I'm not familiar with Arduino languge, so I couldn't figure out what kind of error I made. Please let me know.

Thank you

If you're having problems with the error messages, why don't you post them, and help us to help you?

AWOL: If you're having problems with the error messages, why don't you post them, and help us to help you?

No error messages come out, I fixed the errors come up. That's the problem, I couldn't find which part is wrong.

As some guys said SRAM is one posibility. Is there any other way to simplify the array command? or is there any errors can not be catched from Arduino?

We're not psychic, we don't know what errors you're having.

larslee:
About the syntax errors, that’s the problem, I’m not familiar with Arduino languge, so I couldn’t figure out what kind of error I made.

Again, there is no such thing as “Arduino language.” The Arduino is programmed with C++. Your syntax errors are C++ errors.

``````   for (i=0; i<nterms; i++){
a = 1;
}
``````

a is an array. So what does a = 1 mean? You do this again later on in func():

``````value = c[2]*x + c[1]/x + c[0];   /*van deemter equation*/
``````

x is an array.

As AWOL points out, if you are getting errors you need to ask about the specific errors. Just posting code and saying “this doesn’t work” won’t you get any closer to a solution. You need to explain what isn’t working.

@larslee - you seem incapable of actually reading what we say and doing anything about it. Read Reply #4 , and the one from paulS. Incase you missed it I will repeat it here:-

If you really want help, you'll make your code readable. Modify your posts. Select the code, and press the # button to put the text in a scroll box.

If you prove yourself incapable of understanding this, there is little hope of you understanding much and we are all wasting our time.

Again, there is no such thing as “Arduino language.” The Arduino is programmed with C++. Your syntax errors are C++ errors.

As AWOL points out, if you are getting errors you need to ask about the specific errors. Just posting code and saying “this doesn’t work” won’t you get any closer to a solution. You need to explain what isn’t working.

[/quote]

I guess I had wrong approach to solve my problem like some of you said.
I will start over from C++ code.

I just want to make sure one thing.
If I have C++ code working well, exactly same code can be work in Arduino? (I know it sounds like silly, but it’s serious to me).

I tried some of C++ code, but most of them wouldn’t work and had error.
When I convert the C++code to Arduino, what should I be aware? like simplification?