What I am trying to do is enter 3 words via serial monitor at one time, separate the 3 words and print them back to the screen individually. Before data is entered I want the printed values to be what they start with(in this case the string 'EMPTY') After data is entered I want the data to continue to print the entered values until new values are entered via the serial port and then it should print those newer values.
Seems simple, but I can't seem to quite get it to work.
Actually I am seeing 3 sections where the code is doing things other than what I think it should be doing and I don't understand why.
This is after Googling this a lot. This is after watching videos of similar concepts working. This is after reading many many examples of reading serial input. Often with poor/missing comments or weird variable names that make things confusing as to what is going on or working with variable types that work differently than what I am doing here so it doesn't work.
So here is the code..
#define LED 9 //Have a LED on pin 9
#define bufferLimit 50 //setting a limit on my buffer I dump serial data into(50 seems common in examples)
#define breakInputPoint ",.| " //things that help break the buffer into chunks
char * A = "empty"; //Basically to have something in the 3 arrays/pointers before starting
char * B = "empty"; //So if I comment out checkSerial() and splitBufferIntoParts(inputBuffer)
char * C = "empty"; //in void loop() it will print 'EMPTY' for A,B and C when program runs
char inputBuffer[bufferLimit]; //sets the size of where I dump the serial data buffer
int bufferReadingPosition = 0; //keeps track of where I am reading the buffer
void setup() {
Serial.begin(9600); //allows sending and receiving of data over the serial monitor
pinMode(LED, OUTPUT); //So my LED will light up when called
Serial.println("<Arduino Active>"); //So I know on serial monitor that program has started
}
void loop() {
digitalWrite(LED, LOW); //turns LED off when program starts
checkSerial(); //looks at the serial port for incomming data and if data is there stuff data into inputBuffer
splitBufferIntoParts(inputBuffer); //take the big chunk of data from serial and break it into 3 smaller chunks
printParts(); //print the 3 smaller chunks on the serial monitor
}
void checkSerial() {
while (Serial.available() > 0) //so while data is in the serial buffer waiting to be read do this below
{
digitalWrite(LED, HIGH); //turn on the LED so can visually see stuff is in the serial buffer
char charBuffer = Serial.read(); //read the first character in the serial port buffer
if (charBuffer == '/n') { //check to see if it is a line terminator and if so reset everything and we are done reading
inputBuffer[bufferReadingPosition] = 0;
bufferReadingPosition = 0;
}
else if (bufferReadingPosition < bufferLimit) { //if I have room in my buffer for additional input from serial
inputBuffer[bufferReadingPosition++] = charBuffer; //stuff the next character into my buffer at the next position in my buffer
}
}
}
void splitBufferIntoParts(char * processingBuffer) {
char* first; //always sending 3 'things' via serial port to Arduino so splitting them into 3 chunks
char* second;
char* third;
first = strtok(processingBuffer, breakInputPoint); //read my buffer until hitting a ,.| or <space> (break point)
second = strtok(NULL, breakInputPoint); //read buffer from first break point to second break point
third = strtok(NULL, breakInputPoint); //read buffer from second break point to third break point (if there)
A = first; //stuff what was read before the first break point into A
B = second;//stuff the middle bit into B
C = third;//stuff the rest into C
}
void printParts(){
Serial.print ("A= ");
Serial.println (A); //print what I stuffed into A
Serial.print ("B= ");
Serial.println (B); //print what I stuffed into B
Serial.print ("C= ");
Serial.println (C);//print what I stuffed into C
delay(5000); //wait 5 seconds so stuff doesn't scroll by so fast on serial monitor
}
Now first issue is when program first runs. Before any serial input has arrived to my Arduino my values for A,B and C have all changed because I get this on serial monitor
A= B= C=
But if I comment out my checkSerial and splitBufferIntoParts functions in void loop..
void loop() {
digitalWrite(LED, LOW); //turns LED off when program starts
//checkSerial(); //looks at the serial port for incomming data and if data is there stuff data into inputBuffer
//splitBufferIntoParts(inputBuffer); //take the big chunk of data from serial and break it into 3 smaller chunks
printParts(); //print the 3 smaller chunks on the serial monitor
}
..the serial monitor displays the values for A,B and C that they start with.
A= empty B= empty C= empty
Original values are being overwritten with 'nothingness'. Which ok, I can somewhat see how that might happen. I need to somehow prevent splitBufferIntoParts from running before I get any serial input so it doesn't overwrite my initial values for A,B and C.
So first question would be how do I accomplish that?
Now the second issue(and this is what really confuses me) is when I input data in the serial monitor.
For example I type in Arduino,Raspberry,BeagleBone into the serial input textbox. I get the following output on the serial monitor.
A=
B=
C=
A= Arduino
B= Raspberry
C= BeagleBone
So I think 'yippie this part is working'. It took my 3 words inputted together, split them up and printed them. But....
After my 5 second delay I get this :
A= Arduino
B= Raspberry
C= BeagleBone
<5 second delay>
A= Arduino
B=
C=
A= Arduino
B=
C=
Where did the values of B and C go? And if they were overwritten with 'nothingness' again why wasn't A overwritten?
Also at this point the third issue of the code appears.
It ignores any more serial input.
That is if I say now type Alpha,Beta,Delta into the serial input textbox the serial monitor still keeps repeating
A= Arduino
B=
C=
A= Arduino
B=
C=
A= Arduino
B=
C=
So please explain using the variables I am using how to get my code to do what I am wanting to do.