broken serial connection corrupting arduino values (need Help)

I have a problem with my homemade fan speed controller powered by a arduino nano, that, reads a value from Serial (arduino serial monitor,or processing sketch) & uses the 0~256 value to set a variable "fanspeed1" to set a PWM pin to control the fan speed.

The problem is when I close the processing sketch, or the arduino serial monitor, the "fanspeed1" is changed to max value(the fan speeds up to full speed).

I would like to send serial to set the value and then close the serial monitor, or processing sketch and the "fanspeed1" value not change.

If I need to post some code I can simplify my sketch down to a send serial,receive serial,make pin pwm = serial( 0~256), but I am guessing there is a simple solution to this unfindable with my googling.

Help please, these fans need controlling.........

Opening and closing the serial monitor causes the Arduino to restart. And you probably initialize it to full speed :wink:

Easiest way to stop this, place a jumper between reset and 5V. But DON'T press the reset with that jumper in place because it will create a dead short. You need to remove the jumper if you want to program.

Alternatively remove C4 (might be called something else on clones). But than you'll need to press reset yourself if you want to program.

PS, I hope you send values 0 to 255, not 256 :wink:

Thanks new it was something easy, and ugh 256ish,,,

So if I was to set/save the fanspeed1 value to eeprom could I call it out during setup (yes)
https://www.arduino.cc/en/Reference/EEPROMGet
I will try it out both ways and see I quess.

Thanks for the "arduino resets when serial connection closes" knowledge.

You can read from EEPROM whenever you want.

BUT, you only have limited write cycles to EEPROM. Aka, storing the setting every time you change it via Serial may get you through those limited cycles pretty quick :wink:

So in my opinion it better to stop the behavior you don't want instead of making a workaround. Aka, stop the auto-reset instead of letting the Arduino reset and try and recover from it.

You could make it a jumper on your board (into which the Nano plugs) or you could make a dummy connector to put on the ISP-header that connects pin 2 and 5. Once your project is finished you just place it an leave it there.

Alternative for a link between 5V and reset is a "big" capacitor between 5V and reset. I think 1uF or bigger will work fine. Advantage is that you can still press the reset button without causing a dead short. But again, once the project is finished there should be no need to reset it manually :wink: