interfacing arduino mega adk with vc++

Hi,

I want to read analog sensor using arduino mega adk and with Visual Studio C++

but the result is not correct, look like in the attachment:
sensor 0:023
1
sensor1:023
1
sensor2:023
9
sensor3:21

etc

my arduino program is:

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
}

void loop() {
  char request[1];
  if(Serial.available()){
    request[0]=Serial.read();
    Serial.print(analogRead(atoi(request)));
    Serial.print("\n\r");
  };
  delay(2);                     
}

and my vc++ program is:

#include <windows.h>
#include "stdafx.h"
#include "SerialClass.h"

char buffer[20];
char buf0[1000], buf1[1000], buf2[1000], buf3[1000],buf4[1000], buf5[1000],buf6[1000], buf7[1000], buf8[1000], buf9[1000],buf10[1000], buf11[1000],buf12[1000], buf13[1000],buf14[1000], buf15[1000];


int _tmain(int argc, _TCHAR* argv[])
{
   Serial oSerial("COM5:");//arduino uno com 6
   
   while(1){
	sprintf_s(buffer,"0");    
    oSerial.WriteData(buffer,1); 
	Sleep(100);
	oSerial.ReadData(buf0,6);
	printf("Sensor 0: %s \n",buf0);
	Sleep(100);

	
   sprintf_s(buffer,"1");    
    oSerial.WriteData(buffer,1); 
	Sleep(100);
	oSerial.ReadData(buf1,6);
	printf("Sensor 1: %s\n",buf1);
	Sleep(100);
	
	sprintf_s(buffer,"2");    
    oSerial.WriteData(buffer,1); 
	Sleep(100);
	oSerial.ReadData(buf2,6);
	printf("Sensor 2: %s \n",buf2);
	Sleep(100);

	
   sprintf_s(buffer,"3");    
    oSerial.WriteData(buffer,1); 
	Sleep(100);
	oSerial.ReadData(buf3,6);
	printf("Sensor 3: %s\n",buf3);
	Sleep(100);
	
   sprintf_s(buffer,"4");    
    oSerial.WriteData(buffer,1); 
	Sleep(100);
	oSerial.ReadData(buf4,6);
	printf("Sensor 4: %s \n",buf4);
	Sleep(100);

	
   sprintf_s(buffer,"5");    
    oSerial.WriteData(buffer,1); 
	Sleep(100);
	oSerial.ReadData(buf5,6);
	printf("Sensor 5: %s\n",buf5);
	Sleep(100);
	
	sprintf_s(buffer,"6");    
    oSerial.WriteData(buffer,1); 
	Sleep(100);
	oSerial.ReadData(buf6,6);
	printf("Sensor 6: %s \n",buf6);
	Sleep(100);

	
   sprintf_s(buffer,"7");    
    oSerial.WriteData(buffer,1); 
	Sleep(100);
	oSerial.ReadData(buf7,6);
	printf("Sensor 7: %s\n",buf7);
	Sleep(100);
   
    sprintf_s(buffer,"8");    
    oSerial.WriteData(buffer,1); 
	Sleep(100);
	oSerial.ReadData(buf8,6);
	printf("Sensor 8: %s \n",buf8);
	Sleep(100);

	
   sprintf_s(buffer,"9");    
    oSerial.WriteData(buffer,1); 
	Sleep(100);
	oSerial.ReadData(buf9,6);
	printf("Sensor 9: %s\n",buf9);
	Sleep(100);
	
	sprintf_s(buffer,"10");    
    oSerial.WriteData(buffer,2); 
	Sleep(100);
	oSerial.ReadData(buf10,6);
	printf("Sensor 10: %s \n",buf10);
	Sleep(100);

	
   sprintf_s(buffer,"11");    
    oSerial.WriteData(buffer,2); 
	Sleep(100);
	oSerial.ReadData(buf11,6);
	printf("Sensor 11: %s\n",buf11);
	Sleep(100);
	
   sprintf_s(buffer,"12");    
    oSerial.WriteData(buffer,2); 
	Sleep(100);
	oSerial.ReadData(buf12,6);
	printf("Sensor 12 %s \n",buf12);
	Sleep(100);

	
   sprintf_s(buffer,"13");    
    oSerial.WriteData(buffer,2); 
	Sleep(100);
	oSerial.ReadData(buf13,6);
	printf("Sensor 13: %s\n",buf13);
	Sleep(100);
	
	sprintf_s(buffer,"14");    
    oSerial.WriteData(buffer,2); 
	Sleep(100);
	oSerial.ReadData(buf14,6);
	printf("Sensor 14: %s \n",buf14);
	Sleep(100);

	
   sprintf_s(buffer,"15");    
    oSerial.WriteData(buffer,2); 
	Sleep(100);
	oSerial.ReadData(buf15,6);
	printf("Sensor 15: %s\n",buf15);
	Sleep(100);    
   }
}

If I use 9 sensors/input the output is fine, but when more than 9 say 10 sensors the error is begin. :disappointed_relieved:

  char request[1];

What advantage does a one element array offer over a scalar variable?

Why are you passing a non-NULL terminated array of chars to a function (atoi()) that expects a NULL terminated array of chars?

    Serial.print("\n\r");

You don't like Serial.println()? It prints the carriage return and line feed in the proper order. Is that what you don't like?

If I use 9 sensors/input the output is fine, but when more than 9 say 10 sensors the error is begin.

How are you passing 10 as a single character?

char buf0[1000], buf1[1000], buf2[1000], buf3[1000],buf4[1000], buf5[1000],buf6[1000], buf7[1000], buf8[1000], buf9[1000],buf10[1000], buf11[1000],buf12[1000], buf13[1000],buf14[1000], buf15[1000];

You think that there's a snowball's chance in hell of the Arduino sending 1000 characters to represent the value read from ONE analog pin? Get real.

Thanks for the quick answers,
for:

char buf0[1000], buf1[1000], buf2[1000], buf3[1000],buf4[1000], buf5[1000],buf6[1000], buf7[1000], buf8[1000], buf9[1000],buf10[1000], buf11[1000],buf12[1000], buf13[1000],buf14[1000], buf15[1000];

yes I was to afraid that the buffer will not enough :blush:

so do you recommend to change
Serial.print("\n\r");
with

Serial.println()

and
for more than 9 the

char request[1];
with 
[code]char request[2];

but when I try with the serial monitor in arduino is fine I write 10 and the result is fine. but not working with the vc++

actually for 10, 11, etc I use program:

sprintf_s(buffer,"10");    
    oSerial.WriteData(buffer,2);

do I right?[/code]

yes I was to afraid that the buffer will not enough

I'm sure it will be. Barely.

so do you recommend to change

Yes.

and
for more than 9 the

For two digit pin numbers, the array needs to be at least three elements AND you have to read more than one character from the serial port.

And, you'll discover that you need to somehow distinguish pin 1 from pin 10. How will you know when to stop reading?

do I right?

For sending a two digit number, yes. But, how is the Arduino to know that some numbers are one digit and some are two digits? You need some kind of end of packet marker so the Arduino knows when the end of the packet arrives, so it can stop reading.

Thank you,

I understand what you mean, I have to send character as marker that is the end of the data.

But for implementation in arduino and c++ coding I do not understand, I am newbie in programming.

On the C++ side:

int pinNum = 10;
sprintf(buffer,"%d;", pinNum);

The ; is then the end of packet marker.

On the Arduino side, keep reading, and appending to request, if there is room, until the character read is a ;. Then, append a NULL, and call atoi().