Page 1 of 1

Multi-choice without a multi-choice switch :-)

Posted: 15 Nov 2022, 14:09
by Phil_G
I've not seen this idea used before so for what its worth, here goes:
Take a Nano for example. Normally when you press its reset button, all variables would be cleared by the compilers initialization code.
But there is a way to tell it not to reset a variable, to make it non-volatile through a reset.
This means you can choose from a few options by simply pressing the reset button on the Nano, with no external selection controls.

Simple usage examples could be a servo with (say) four different positions, selected by pressing reset, or a two-option on/off 'switch'.
Another use might be to give a random number generator a different seed after every reset, rather than the same 'zero' seed every time.
Maybe your project has used all the available I/O but you need just one more option to choose the red pill or the blue.
I used it for a 'demo loader' for a recent Retro Computer Festival:




Code: Select all

// use reset button to select option 1 to 4,   Phil_G

int mode __attribute__ ((section(".noinit")));
void setup() {
  Serial.begin(9600);
  Serial.println("Press reset to change mode");
  mode++;
  if (mode > 4 || mode < 1) mode = 1;
  Serial.print("Current mode: "); Serial.println(mode);
}

void loop() {
  switch (mode) {
    case 1: // do mode 1 stuff, say "servo neutral"
      break;
    case 2: // do mode 2 stuff, say "servo right"
      break;
    case 3: // do mode 3 stuff, say "servo neutral"
      break;
    case 4: // do mode 4 stuff, say "servo left"
      break;
  }
}

Of course since nothing has been saved to eeprom, power-off doesnt save the variable. To retain a value during power-off, you would need to write it to eeprom, but what if your project does fast, sequential writes? the eeprom write limit could easily be exceeded. This could be a means to avoid that, it all depends on your project and circumstances. I think this is just another interesting option :)
Or maybe I'm late to the party again and everyone else has been doing this for years! :)

Cheers
Phil

Re: Multi-choice without a multi-choice switch :-)

Posted: 15 Nov 2022, 18:44
by Martin
Interesting. I didn't know about that feature.

Re: Multi-choice without a multi-choice switch :-)

Posted: 21 Nov 2022, 13:14
by Stew
As I was reading this, in particular Phil's reference to the 'red pill or blue pill' there was a piece on the radio playing a clip from the matrix film....
:shock: :shock:

Re: Multi-choice without a multi-choice switch :-)

Posted: 21 Nov 2022, 17:58
by Stew
Phil_G wrote: 21 Nov 2022, 14:30 As I was trying to compose the explanation, that was the first "two option" choice that came into my head - we're a weird bunch if nerds, eh :D
Wouldn't have it any other way Phil! :D

Re: Multi-choice without a multi-choice switch :-)

Posted: 10 Jan 2023, 13:36
by FBMinis
I had no idea this was possible, thank you