Hi all
I try to read sensor analog from serial using Visual studio C++ 2010.
my arduino program :
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);
}
my Serial C++ program :
#include <windows.h>
#include "stdafx.h"
#include "SerialClass.h"
char buffer[20];
char buf0[200];
int _tmain(int argc, _TCHAR* argv[])
{
Serial oSerial("COM6:");
while(1){
sprintf_s(buffer,"0");
oSerial.WriteData(buffer,1);
Sleep(1000);
oSerial.ReadData(buf0,4);
printf("Sensor 0: %s \n",buf0);
Sleep(1000); }
}
I put 5V on sensor 0 but the result is not stable:
sensor 0 :
sensor 0 : 10230
sensor 0:
100
sensor 0: 23
0
sensor 0: 10230
and so on,
I think there is no wrong with arduino side because when I use serial monitor on arduino it seems alright.
Any suggestion?