I want to reset (re-run) a program by accessing the reset pin from code. I can do it with a transistor with base connected to a digital pin but it would be better if I could set the pin directly from code. Is this possible?
you could tie an active low reset pin high with a resistor and to an I/O pin which can be set low and then configured as an OUTPUT
setting the Logig level of an IO-pin is always and only done by code
Do you mean resetting your microcontroller by executing a command that does directly resetting
as pseudo-code
if (my-reset-conditions-is-true) {
executeReset();
}
the better question is why do you really want to reset?
what is the final purpose of the resetting?
Code should be written and can be written that you never need to reset the microcontroller
You have written
In what circumstances do you want to re-run your code?
Most re-running code can be done without resetting
best regards Stefan
that sound like you might be referring to doing a 'soft reset'!
you can do that is code will the following line of code:
void(* resetFunc) (void) = 0; // Create a function pointer that points nothing
resetFunc(); //function to call in your code where you want to force the Arduino to reset.
hope that helps....
I don't understand how this should ever work
You have to explain in detail
or
post a fully working code where you use some easy to realise reset-condition
examples for the reset-conditions
- 5 seconds have passed by
- a certain character is reveived from Serial
best regards Stefan
So here is the demo-code
void(* resetFunc) (void) = 0; // set pointer to resetFunc at adress 0
void setup() {
Serial.begin(115200);
Serial.println("How to Reset Arduino Programmatically");
Serial.println("www.TheEngineeringProjects.com");
delay(200);
}
void loop() {
Serial.println("A");
delay(1000);
Serial.println("B");
delay(1000);
Serial.println("Now we are Resetting Arduino Programmatically");
Serial.println();
delay(1000);
resetFunc();
Serial.println("Arrduino will never reach there.");
}
You could enable the watchdog and let it time out.
No you can't.
The reset pin need to be held low for at least 2.5usec. You would an external reset IC with a timeout to do it.
Even if you could set the pin from code you still have the same problem
It depends if there is an external capacitor on the RESET pin and the size of the capacitor and pullup
Can you indeed?
Have you tried this?
I suspect it doesn't work.
The reason is that in order to reset the processor the reset has to be held down for a minimum amount of time. Before this time has passed however the processor has set all the pins to be inputs. Therefore the pin stops supplying power to the base of the transistor and turns it off. Therefore the processor has not had a long enough to fully reset and will be in an indeterminate state.
There are ways of slugging this operation by connecting a capacitor to output and ground with a resistor to the base to ensure the pulse is long enough.
No this is wrong, the pins are set to inputs (not tristate) happens before the reset is compleate.
The only proper way to RESET an AVR like the Mega is to enable the WDT, set a short timout and let the WDT do the reset
That's my opinion as well. In addition I'd try to simply set the Reset pin (PC6 on Uno) as output and make it LOW. All untested.
This suggestion seems not to apply to the Mega with a dedicated Reset pin (no port).
When I hearing this, the first what I think that the program is written so pourly so it required to restart regularily to continue.
If this is case, could it better to rewrite the code that get rid the need of restarting?
A WDT is not the same as pressing the reset button, it is a totally different process.
It might work even with only setting PORTC pin 6 low, as for some other software interrupts that work even on INPUT pins.
But the clean solution with the WDT seems to be the best one, even working on a Mega.
Thanks for testing
WOW! Such a lot of answers, questions and suggestions. Many thanks for your suggestions. Very much appreciated. To clarify, I am making a special chess clock that uses 2 Arduinino Mega's that each use 70 pins. It works perfectly except it does not reset the program when a game ends. Stefan is right, I should have considered reset when writing the code. I am new to Arduino and wondered if I could save rewriting some of the code by simply setting the reset pin low without wiring it to a button. I now know I can't do that, so I will modify my code.
Aha a special chess clock. No idea what this looks like.
I'm very sure that this could be reduced to use a 12-pin microcontroller in combination with using I2C-io-expanders. If it works with two arduino megas it works
There is nothing easier than - restarting a code after doing anything between a single step of code-execution and having executed 10 million steps of code-execution
The minimum and always working solution is to set a flag-variable to "gameHasNotYetEnded" to TRUE as soon as the game starts
and
to set this variable to false if the game has ended
Your code checks this variable beeing true or false.
If you would post your code or at least some more details of how it works
a pretty simple non-resetting solution can be suggested
best regards Stefan
From the manufacturer..
https://microchip.my.site.com/s/article/Software-Reset
Particularly the do not section
Just curious: does a hardware reset re-initialize SRAM? Can that startup function be called in code?