I'm having an issue with serial data getting magically wiped after you close the serial monitor window. So if I open the serial monitor window and type in 2912312 the test value gets updated correctly. If I close the serial monitor window and reopen it... it magically gets swapped back to 12345.... why is my variable reverting back to it's original value on serial close? Below is the code for the arduino that is currently causing the issue. I'm unbelievably stumped on this. I assumed since I made my test variable a global that if at any point it gets updated it would keep it's data until the ardiuno is unplugged. However the arduino is NEVER unplugged. I simply just open and close the serial monitor. It's as if the serial monitor is causing the arduino to reset?
Code:
char inData[12];
long inInt;
int index;
long test = 12345;
long New;
void setup(){
Serial.begin(9600);
}
void loop(){
if(Serial.available()){
New = RX();
}
if( New > test ){
test = New;
}
Serial.println(test);
delay(100);
}
long RX(){
// Verify packet
while(Serial.available() > 2){
char aChar = Serial.read();
inData[index] = aChar;
index++;
inData[index] = '\0';
}
// Packet verified and now converting packet from ASCII to LONG
inInt = atol(inData);
index = 0;
inData[index] = '\0';
return inInt;
}