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

Arduino projects on the go
Post Reply
User avatar
Phil_G
Posts: 521
Joined: 15 Feb 2018, 23:32
Contact:

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

Post 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
Martin
Posts: 711
Joined: 16 Feb 2018, 14:11
Location: Warwickshire

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

Post by Martin »

Interesting. I didn't know about that feature.
Stew
Posts: 449
Joined: 02 Mar 2018, 10:21
Location: Cockfield, County Durham

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

Post 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:
Stew
Posts: 449
Joined: 02 Mar 2018, 10:21
Location: Cockfield, County Durham

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

Post 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
FBMinis
Posts: 55
Joined: 25 Feb 2018, 17:59

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

Post by FBMinis »

I had no idea this was possible, thank you
Post Reply