I am not sure why this is not working, but I am looping the Serial buffer and it is getting written over. I will post snippets of my code with comments explaining the rest because it is very long. If people could see if I am doing something wrong/stupid, and also tell me the difference in Serial buffer size between the Arduino Uno R3 and the Arduino Mega 2650. Also how do you wipe the incoming Serial buffer? Thanks in advance. CODE BELOW!
That is what I am doing, but I am getting overlap from the last call, do I have to worry about the loop running too quickly or something, because message2 is suppose to be seperate of message 1, but they sometimes in the same order and other times message2 is first, and sometimes one does not complete. This is from printing the array I saved made up from the different messages.
edit: and can you explain what Serial.flush() does, what buffer does it wipe? Should I even use it?
The serial.flush() is just clearing the serial ports of any data.
The past couple of days I was learning how to send my data in the serial and I Found that if I am buffering data in an array, I need to do likewise when I send it. Use a For Loop to send data individually. Whether or not this is necessary I do not know, it's just what I did to solve the issue. I also used two buffers so that data could continue to be stored in the other buffer (via interrupt) and continue to send. Note that Serial.write will only send one byte. My values are floats turned unsigned ints and one byte is not enough. So I am sending two bytes of data.
else if (BufferReady2) { //Is Buffer2 ready for data transfer
BufferReady2 = false; //Set the flag to false because data will be sent
for (int i = 0; i < DATASAMPS; i++) { //Array to be sent
for ( int j = 1; j >= 0; j--) { //Sends two bytes
tempVal = (unsigned int)(Vms2[i]*1000); //Turn Float into sendable unsigned int
Serial.write(tempVal >> (j*8)); //Send the bytes via the serial with MSB on "Left"
}
Thank you for the help. No more Serial.flush() in my code anymore. JHawk88, I thought you might like to know you don't have to loop like that. Serial.write() can take two parameters. Serial.write(byte) will only write on byte, but Serial.write(array,sizeof(array)) will send the whole array. Also I would post for future readers that it seems as if both the Arduino Mega 2650 and Arduino Uno R3 buffers are both 63 bytes long (Yes 63, not 64).
How many buffers do you think you can have with those interrupts, because I am 1 byte over the limit right now, and I feel like I will run over the buffer limit again. I would also appreciate it if you could add the rest of your if / elseif, because I am not familiar with interrupts. Thanks.