Check if arduino was reseted

Hi,

Is there anyway to know if the user reseted the arduino pressing the RESET button instead of just powering up arduino? Is it possible to be sure that when setup() runs it can know that arduino was previously powered off or was just reseted?

Is there anyway to know if the user reseted the arduino pressing the RESET button instead of just powering up arduino?

No.

Is it possible to be sure that when setup() runs it can know that arduino was previously powered off or was just reseted?

No.

As far as I know a power off/power on is the same as a reset, but I'm posting just to join the thread and see the answers.

Isnt any bit set in the register when the user presses the reset button?

From http://www.atmel.com/Images/Atmel-42735-8-bit-AVR-Microcontroller-ATmega328-328P_Datasheet.pdf:

15.9.1. MCU Status Register To make use of the Reset Flags to identify a reset condition, the user should read and then Reset the MCUSR as early as possible in the program. If the register is cleared before another reset occurs, the source of the reset can be found by examining the Reset Flags. When addressing I/O Registers as data space using LD and ST instructions, the provided offset must be used. When using the I/O specific commands IN and OUT, the offset is reduced by 0x20, resulting in an I/O address offset within 0x00 - 0x3F. Name: MCUSR Offset: 0x54 Reset: 0x00 Property: When addressing as I/O Register: address offset is 0x34 Bit 3 – WDRF: Watchdog System Reset Flag This bit is set if a Watchdog System Reset occurs. The bit is reset by a Power-on Reset, or by writing a '0' to it. Bit 2 – BORF: Brown-out Reset Flag This bit is set if a Brown-out Reset occurs. The bit is reset by a Power-on Reset, or by writing a '0' to it. Bit 1 – EXTRF: External Reset Flag This bit is set if an External Reset occurs. The bit is reset by a Power-on Reset, or by writing a '0' to it. Bit 0 – PORF: Power-on Reset Flag This bit is set if a Power-on Reset occurs. The bit is reset only by writing a '0' to it.

However, my understanding is the bootloader causes at least some of that information to be lost. There is a bunch of discussion of ways to fix this here (and in the linked issues/PRs): https://github.com/Optiboot/optiboot/pull/125 If you're not using a bootloader that shouldn't be an issue.

P.S. that register is called MCUCSR on some AVRs just to make things extra confusing.

Or you can use some small external circuit. When it gains power it is 0, Arduino toggles it to 1 and it stays 1 until power is removed. Or something like that.

Thank you, I think I am not using a bootloader, I just bought my arduino nano and never changed anything in it, I just upload sketches and nothing more. So, is it possible to read the bit EXTRF so I can know if my arduino was reseted?

You are using a bootloader. Without a bootloader, it's a little more complex to upload code to the Arduino. It's one of the reasons why Arduino is so great: the bootloader makes it easy to load code.

Ok, so if I am using a bootloader it's impossible to know if arduino was reseted?

Smajdalf: Or you can use some small external circuit. When it gains power it is 0, Arduino toggles it to 1 and it stays 1 until power is removed. Or something like that.

I.e. Vcc - diode - Arduino pin - small capacitor - GND.

The diode is here to let the cap discharge into Vcc when power is removed.

When power is applied the cap is discharged (Arduino pin reads 0). Then you charge cap. When Arduino is reset by button the cap is already charged and read as 1.

I guess I have to ask what difference it makes? What do you intend to do differently if the Arduino was reset vs. having the power disconnected and reconnected?

I am using arduino in an atmosferic baloon and I can spare at most 6g to my circuit (arduino). I already removed everything I dont need from arduino and I managed to get exactly 6g BUT I still need to know if some problem happens in the air (power disconnection and reconnection due to vibration when baloon pops) or if the user just presses the reset button.

Anyway, I really cant afford any other electric component no matter how light it can be. If I get over 6g I will have to pay an extra fee which is too high to me since I am doing this as a hobbist. So it would be nice to know if arduino power was disconnected/reconnected or if the reset button was just pressed.

You have not answered the question. What would you do different in the code, if you knew that the reset switch had been pressed?

When the Arduino is in the balloon, and off the ground, how is someone going to press the reset button?

batata004: if the user just presses the reset button.

Well you can save another gram or so by removing the button, problem solved.

No button at all, when I need to reset I short the 2 terminals with a small piece of wire. It's very easy my situation: when I press RESET button it's intentional, it should not be treated as an error, when power down/power up occurs, it should be treated as a problem and logged in EPPROM. I can easily use EPPROM.write to write some number in memory if arduino was powered down/up so later I can know it happened. And everytime I RESET arduino (intentionally of course) I can clear this EPPROM byte so if any problem happens later I can know it happened reading EPPROM.

Well then we have the question of why you need to intentionally reset the Arduino? If you have a spare pin you could just poll it and connect it to ground to trigger whatever you're doing the intentional reset for.

FYI, a Pro Mini would be lighter than a Nano because it doesn't have the CH340 and USB jack and the board is smaller.

I already removed the "ftdi" chip and the usb jack, so my nano is lighter than a mini. I need to be able to press the RESET button cause pressing the RESET button is the way I will tell my algorithm to start cleanly. When I power my arduino I dont want it to start, I want it to do nothing till I press the reset button. Using epprom I can easily check if previously I was reseted or just powered up.

PLEASE UNDERSTAND THIS: I need to know if arduino was disconnected/reconnected to power supply during the flight, wheter it was caused by vibrations or most likely to some problem with the power suplly itself. When I recover my arduino from the flight I can check the epprom and know if there was any power interruption during flight.

How do you interface (program, read data) from Arduino when the FTDI chip is removed? You can probably use ISP witch little more effort, removing bootloader and then be able to use the register uninterrupted. Also do you need reset do do what you want? After pulling another pin low a part of EEPROM is written and your program starts. When Arduino wakes up from power down it checks if the EEPROM is written - if yes power glitch occured. If no it is not time to start. After mission you clear the EEPROM somehow.