1

I would like to program the arduino such that if I reset it by disconnecting and then reconnecting the usb.. A particular led glows BUT If I press the manual reset button on my nano... I want a different led to glow. How can I achieve this. I have 2 leds and my nano... Along with the laptop and any other necessary components.

2

The MCUSR register (page 79 of the datasheet) tells you what kind of reset happened most recently. As long as the USB interface chip on your nano isn't controlling the RESET pin without the port being opened (i.e., toggling it at power up regardless of the USB state) then you could query that register to see how it reset:

if (MCUSR & (1 << 0)) { // POR
    digitalWrite(4, HIGH);
} else if (MCUSR & (1 << 1)) { // External Reset
    digitalWrite(5, HIGH);
}

However, if the USB chip does toggle the RESET pin then there is no way you can distinguish between the two resets since they are bot the same "external" reset using the RESET pin.

  • 1
    Note that the information in MCUSR is lost on boards running optiboot, such as Uno. Luckily the standard bootloader on the Nano doesn't do this so it likely won't be an issue in this case but it's worth mentioning because it's common for people to burn the optiboot bootloader to Nanos as it's otherwise far superior to the stock one. – per1234 Feb 7 '17 at 6:50
  • If the usb-to-serial is actually resetting the MCU right after power-up, you could try using the PORF bit in the MCUSR. When first power-on, this bit is set. If you clear this bit, and then press the reset button, this bit will still be 0 when the sketch start again. – Gerben Feb 7 '17 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.