Hi,
I'm trying to send 3 different signals over the serial port on my Arduino Duemillanove. My goal is to be able to send 3 ADC signals to my computer where I can plot them simultaneously and I want to be able to have them plotting in real-time.
I have written this arduino code so send 3 signals over the serial port and to also communicate via the serial input buffer but I am having some reliability issues (read after the code).
[code]
//Set of numbers to communicate thru serial
int x1[5] = {1,2,3,4,5};
int x2[5] = {6,7,8,9,10};
int data = 0;
int i = 0;
String inside = "inside";
// I = 73
// E = 69
// R = 82
// C = 67
void setup()
{
Serial.begin(9600);
pinMode(13,HIGH); //to turn LED on
}
void loop()
{
//Debugging block...
//delay(1500);
//Serial.print(Serial.read());
//Serial.flush();
//Serial.print('\n');
//if 'I' is sent to the serial input buffer than...
if(Serial.read() == 73)
{
Serial.println(inside);
Serial.println('\n');
go(); //call the go function which will send the numbers over the serial continuously....
}
//if 'R' is sent to the serial input buffer than...
if(Serial.read() == 82)
{
Serial.print(data); // print 'data', the variable that holds the count for number of data points sent...
Serial.flush(); // clear the buffer now that we've send over the count...
}
Serial.flush(); //clear the buffer from any miscillaneous junk now that none of the previous conditions have been met...
}
//this function sends sets of data via serial....
void go()
{
digitalWrite(13,HIGH); // turn on LED to indicate that data is being sent via serial...
//unless 'E' is sent to the serial input buffer, don't stop sending data over the serial line...
while( Serial.read() != 69)
{
Serial.flush(); // now that you've checked that E hasn't been pressed, clear the buffer for the heck of it (neatness sort of thing)...
//print first function
//print delimeter ',' (it's a coma)...
Serial.print(x1[i]);
Serial.print(',');
Serial.print(x2[i]);
Serial.print(',');
Serial.print(x1[i]);
Serial.print(',');
Serial.print('\n'); // print 3 in a line for arduino debugging....
data++; //keep count of numbers sent over the serial channel...
i++; // send the next number in the X arrays...
if(i ==5){i = 0;} //make sure you don't step out of the X array...
}
Serial.flush(); //clean this buffer just for the heck of it....
digitalWrite(13,LOW); //shut off the LED light, we are no longer sending data...
}
[/code]
The code works as follows:
-
When "I" sent over the serial Monitor the arduino sends back the string "inside" and follows by continously sending lines of 3 numbers (the array x1, x2)
-
The arduino will stop sending these numbers when you press "E"
-
If you send "R" after sending "E" it will print a count of data sets sent during the transmission phase. If you press "R" from the start without pressing "I" the arduino will send you a zero as nothing has been transmitted via the serial loop...
My problem is when I am using the Arduino Serial Monitor I have to send "R" or "I" multiple times in order to get the arduino to enter the "if" statements in the void loop()... This isn't very reliable, especially if I plan on using a program, MATLAB, to do this interfacing for me....
Does anyone know why I might have to send a string via serial 2 or more times in order to get the Arduino to read it ?? Is there something wrong with the Serial Monitor application?
I would truly appreciate any help