Fast sensoring for scientific purpose

Hello everyone!

I'm trying to create a very fast sensor that will send the data to a computer. I'm doing it on windows 7 since there was an example of how to connect Arduino through c++ (by serial port). On my board (Arduino Uno) I have a script:

const int SENSOR = A1; // select the input pin for the
// sensor resistor
int val = 0; // variable to store the value coming
// from the sensor
void setup() {
Serial.begin(9600); // open the serial port to send
// data back to the computer at
// 9600 bits per second
}
void loop() {
val = analogRead(SENSOR); // read the value from
// the sensor
if(Serial.read()>1)
{
Serial.println(val);
}
delay(1);
}

And on my PC I'm sending in a loop {2} and then I listen for response.
Part of the code is here:

	while(SP->IsConnected())
	{
      
     SP->WriteData(sendData,1);  // In the future,  you'll expand on this
                       Sleep(1);         // to write your custom data to the board
      
		readResult = SP->ReadData(incomingData,dataLength);
		printf("Bytes read: (-1 means no data available) %i\n",readResult);
		if(readResult>-1)
		{
		myfile<<time*i<<","<<incomingData<<endl;
		i++;
        }
		
         if (kbhit()) {
                      std::cout<<std::endl<<"Not recording"<<std::endl;
                      myfile.close();
                      SP->~Serial();
                      system("PAUSE");
              } 
		Sleep(time-1);

(Please don't worry about comments I didn't write them, it was already in the example which I've rewritten...)
At the beginning of the program I chose the delay between readings and here comes the problem:
Once it is lower then 15ms the output (which is being saved as a .txt file is "corrupted"- EXAMPLE(time 2ms):

2,4,6,8,57
10,57
12,
56
14,
56
16,
56
18,
56
20,
56
22,
56
24,
57
26,
57
28,
57
30,
57
32,
56
34,
56
36,
56
38,
56
40,
56
42,
56
44,

5746,

5748,

I need to know if the problem is in the board or in the program or in the datawriting? Do you think that there is a way to read data out of a sensor faster than 2ms? If yes what should I improve?
I've already tried gobetwino and other programs, but they don't work at this speed.

Thanks for any response
Jan

At 9600 baud, it takes about 1 millisecond to send a single character, and Serial.println() sends several.

Increase the baud rate on the serial port of the Arduino and the PC to as fast as it will go -- you might try 115200.

Thank you very much!
However it still does some wierd stuff:
2,56

56

4,56

56

that's the output :frowning: and if I use clocks even though the computer sleeps for 2ms it can't read faster than 32times per second :frowning:

Then I deleted the first Sleep(1) and I overwrote the second Sleep(time) to Sleep(1) and then it reads out 65 times per second...
Do you think that this is the maximum?

Update: with no sleep I can get 536 times per second....
But the data have some errors..... see attachement for 2 second reading

blackout.png

this discussion might be interesting - Experiment - split up analog read in 3 steps - Libraries - Arduino Forum -

The new problem is probably with the PC.