Hi, I'm having trouble with connecting and disconnecting from a serial port effecting the operation of my code.
I have created a simple example based on the blink code to show the problem.
In this example I'm incrementing a variable on each loop but if I connect and disconnect (close/open) the serial monitor or even an independent serial monitor program. It seems that the code completely resets to the start loosing my count value.
I am producing a more complex program where I only connect the serial port occasionally to download collected data but connecting looses the data I have collected.
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
*/
int led = 13;
int count=0;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
count++; //this count should keep going even if I stop/start serial comms
// add very basic serial operation
if (Serial) {
Serial.print(count++, DEC);
Serial.println(" keep going");
}
}
Thanks AWOL, I don't want to change things so it upsets the development code environment. The strength of Arduino is the easy to use development tool.
Is there another way? I don't want to use the built-in serial monitor could I allocate some other pins for serial? or perhaps there is another better way.
The idea of the project to collect temperature information from a local river at a low data rate and download and look at the temperature history before kayaking/swimming in the river using a Bluetooth link.
If you can't create your own program, there are advanced serial monitors programs which have a setting to enable or disable DTR, for example with Termite, in the Flow Control dropdown menu (or if you enable the Status Led plugin, you can then click on the DTR led).
Another solution is to use another Serial port on the arduino (hardware like on the Arduino Mega, or using the SoftwareSerial library).
If you use a TTL-USB cable to connect the Arduino to the PC it gives you the option of not connecting DTR to the reset pin. And you can connect it to the regular Rx and Tx pins and GND. If the cable has a 5v output you could power the Arduino from that by connecting it to the Arduino 5v pin. Or you could power the Arduino independently.