I don’t understand why my variable starts/display the value from 1 every time i start the serial monitor
i am not restarting my program just restarting serial monitor
most of the code is from Forum . i just added the tcount and cou to test
nano_sketch_mar05a_03-02.ino.ino (1.31 KB)
Welcome to the forum
You can post programs up to 9000 characters in length directly in the forum which removes the need to download it
Here is your program Auto formatted in the IDE to tidy it up a bit and posted here in code tags
unsigned long startMillis; //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000; //the value is a number of milliseconds
const byte ledPin = 13; //using the built in LED
int tcount = 0; // testing counts
unsigned long cou = 0; // testing another variable
Serial.begin(9600); //start Serial in case we need to print debugging info
startMillis = millis(); //initial start time
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (currentMillis - startMillis >= period) //test whether the period has elapsed
digitalWrite(ledPin, !digitalRead(ledPin)); //if so, change the state of the LED. Uses a neat trick to change the state
startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state.
cou = cou + 1;
Serial.print( " ");
Note how easy it is to scroll through it and copy it to be examined in an editor. Please post code like this in the future
It is possible to disable the auto-reset circuit that causes your board to reset when you open Serial Monitor, but the reason for that circuit is because the board needs to be reset to activate the bootloader when you do an upload. Without the auto-reset circuit, you would need to manually reset the board at just the right time during the upload process.
So unless it's really causing a problem for you, I would recommend leaving the auto-reset circuit working.
Thanks for the help
i now understand
The easy/temporary way to disable the autoreset in hardware is to use a ~10uF capacitor between reset and ground (negative side of cap to ground, if it's a polarized capacitor) - this can by removed when you want to upload a new sketch.
From the software side, some serial terminal tools (hTerm is one such tool - it's an incredibly full-featured serial terminal - though you may find the sheer number of options overwhelming) do not automatically toggle DTR when opening the port, which is what actually triggers the autoreset (the Arduino IDE's serial monitor and most basic serial terminals do). An awkward thing about using an external serial monitor, though, is that it won't automatically disconnect from the port to allow you to upload a new sketch, like the Arduino IDE's serial monitor will - you'd have to manually disconnect with that program.
It's worth noting that autoreset upon opening the serial monitor is often desirable - imagine you want to see the start of the output from your sketch - without autoreset on opening the port, you'd have to open the port, then reset the board with the reset button to start the sketch from the beginning...