I have a problem with my homemade fan speed controller powered by an Arduino Nano, which reads a value from Serial (Arduino serial monitor, or Processing sketch) and uses the 0~255 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, 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 value of fanspeed1
should 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~255), but I am guessing there is a simple solution to this unfindable with my googling.
Help please, these fans need controlling...
-
1Showing us your simplified sketch code would be useful here. For now, try to protect 'fanspeed1' from changing when serial communication is not active.smajli– smajli2017年11月13日 10:40:26 +00:00Commented Nov 13, 2017 at 10:40
-
2Disable the auto-reset functionality of the board?Majenko– Majenko2017年11月13日 11:07:31 +00:00Commented Nov 13, 2017 at 11:07
-
It's 0~255, not 256.dda– dda2017年12月14日 05:39:37 +00:00Commented Dec 14, 2017 at 5:39
1 Answer 1
You could consider writing/reading the value to/from the EEPROM.
http://arduino.cc/en/Reference/EEPROM
This allows the board to be reset, or even turned off, and retain the value previously set. What you would be doing is saving the value to the internal memory of the Arduino. This is resettable and persistent over a "reboot".
Granted, there is a limited number of writes you can do, but that number is high enough (~100k) that you will probably wear out the fan before the EEPROM sector dies.
Syntax
EEPROM.write(address, value)
EEPROM.read(address)
EEPROM.update(address, value)
For objects:
EEPROM.put(address, data)
EEPROM.get(address, data)