How to implement a soft-reset buttom ?

Hello all,

Im trying to get a reset button working, tried enclosing routines in ‘while’ and ‘while do’ in conjunction with ‘break’ but still no deal.

Being clear in my question I reproduced the target sequence from the original sketch, that´s a new sketch:

const int selecpin = 3;
const int navpin = 2;
const int resetpin = 5;
int c1 = 0;

void setup() {
 Serial.begin(9600);
  pinMode(selecpin, INPUT);
   pinMode(navpin, INPUT);  
   pinMode(resetpin, INPUT); 
}


void loop() { /* //// NORMAL ACTIONS /// */   navigation(); navaction(); 
  
}

void navigation() { 
if (digitalRead(navpin) == HIGH) { 
  if ((c1 >= 0) && (c1 < 4)) {
  c1 == c1++;
  if (c1 > 3) {c1 = 1; }
  if (c1 == 1) {  Serial.println("OPT1"); }
  if (c1 == 2) {   Serial.println("OPT2");  }
  if (c1 == 3) {  Serial.println("OPT3");  } }
  
  if ((c1 >= 4 ) && (c1 < 6)) {
    c1 == c1++;
    if (c1 > 5) { c1 = 4; }
    if (c1 == 4) {  Serial.println("OPT3 sub1");  }
    if (c1 == 5) {  Serial.println("OPT3 sub2");  } } 
    delay (150);  

 }  }
 
 void navaction() { if (digitalRead(selecpin) == HIGH) {
  if (c1 == 1)  { Serial.println("action OPT1 taken"); c1 =0; }
  if (c1 == 2)  {Serial.println("action OPT2 taken"); c1 =0; }
  if (c1 == 5)  {Serial.println("action OPT3 sub2 taken"); c1 =0; }
  if (c1 == 4)  {Serial.println("action OPT3 sub1 taken"); c1 =0; }
 if (c1 == 3)  { c1 = 4; Serial.println("OPT3 sub1"); }
     delay(300);
    
 }  }

So in the code user can navigate trough the options using the ‘navpin’ and choose any option using the ‘selecpin’.
Option ‘OPT3’ have 2 sub-menu options.

I want that user at any point of the options menu have the choice of pressing a reset buttom ‘resetpin’ that will break anything menu is showing and return to the normal execution /* //// NORMAL ACTIONS /// */.

Was considering the possibility to have the reset buttom attached directly to the hardware reset pin, but this is gross and no more a “soft-reset”.

It´s possible to do it?

thanks,

Rodrigo

If I understand you, you want to add a 3rd switch. When that third switch is pressed, you want to immediately exit the navigation function. Is that correct?

If so, I guess I fail to see the problem. Just add a digitalRead for that pin in navigation, set a global variable to true, and return if the switch is pressed. Do the same in navaction. Don't call navaction if the variable is true. Reset it in navigation.

What happens in the (not shown) normal actions section? loop is called in an infinite loop. The navigation and navaction functions are called over and over again. Most times they are called, they do nothing, because the switches on navPin and selecPin are not pressed.

Hello PaulS,

It´s not you that fail, problem was me that failed to paste the right code representing my question.

The right code is:

const int selecpin = 3;
const int navpin = 2;
const int changep = 4;
const int resetpin = 5;
int c1 = 0;

void setup() {
 Serial.begin(9600);
  pinMode(selecpin, INPUT);
   pinMode(navpin, INPUT);  
   pinMode(resetpin, INPUT);
   pinMode(changep, INPUT);
}


void loop() { navigation(); navaction();
  
}


void navigation() { 
if (digitalRead(navpin) == HIGH) { 
  if ((c1 >= 0) && (c1 < 4)) {
  c1 == c1++;
  if (c1 > 3) {c1 = 1; }
  if (c1 == 1) { Serial.println("OPT1"); }
  if (c1 == 2) {   Serial.println("OPT2");  }
  if (c1 == 3) {  Serial.println("OPT3");  } }
  
  if ((c1 >= 4 ) && (c1 < 6)) {
    c1 == c1++;
    if (c1 > 5) { c1 = 4; }
    if (c1 == 4) {  Serial.println("OPT3 sub1");  }
    if (c1 == 5) {  Serial.println("OPT3 sub2");  } } 
    delay (150);  

 }  }
 
 void navaction() { if (digitalRead(selecpin) == HIGH) {
  if (c1 == 1)  change();
  if (c1 == 2)  {Serial.println("action OPT2 taken"); c1 =0; }
  if (c1 == 5)  {Serial.println("action OPT3 sub2 taken"); c1 =0; }
  if (c1 == 4)  {Serial.println("action OPT3 sub1 taken"); c1 =0; }
 if (c1 == 3)  { c1 = 4; Serial.println("OPT3 sub1"); }
     delay(300);
    
 }  }

void change() { int y =0; 
while ( y < 20) {  
if (digitalRead(changep) == HIGH) { Serial.println(y); y++; delay(200); }  
    } 
 c1 = 0;
 }

So let´s say a user choose ‘OPT1’ so every press of ‘changep’ it increments value by 1 until it reaches 20.
Question is: Supposing that user is in the middle of the while statement he decides to abort before reaching the 20th number, pressing the ‘resetpin’ that will make sketch returning to the beginning of the process.

Is this possible?

Thanks

Sure. Just add a digitalRead of the reset pin in the change function. You currently read whether or not the changep pin is HIGH (i.e. the switch is being pressed). After that if, add another one that tests whether the reset switch has been pressed. If it has, return.

if(digitalRead(resetPin) == HIGH)
   return;

Very thanks PaulS,

It worked as

const int selecpin = 3;
const int navpin = 2;
const int changep = 4;
const int resetpin = 5;
int c1 = 0;

void setup() {
 Serial.begin(9600);
  pinMode(selecpin, INPUT);
   pinMode(navpin, INPUT);  
   pinMode(resetpin, INPUT);
   pinMode(changep, INPUT);
}


void loop() { navigation(); navaction();
  
}


void navigation() {
if (digitalRead(navpin) == HIGH) {
  if ((c1 >= 0) && (c1 < 4)) {
  c1 == c1++;
  if (c1 > 3) {c1 = 1; }
  if (c1 == 1) { Serial.println("OPT1"); }
  if (c1 == 2) {   Serial.println("OPT2");  }
  if (c1 == 3) {  Serial.println("OPT3");  } }
  
  if ((c1 >= 4 ) && (c1 < 6)) {
    c1 == c1++;
    if (c1 > 5) { c1 = 4; }
    if (c1 == 4) {  Serial.println("OPT3 sub1");  }
    if (c1 == 5) {  Serial.println("OPT3 sub2");  } }
    delay (150);  

 }  }

 void navaction() { if (digitalRead(selecpin) == HIGH) {
  if (c1 == 1)  change();
  if (c1 == 2)  {Serial.println("action OPT2 taken"); c1 =0; }
  if (c1 == 5)  {Serial.println("action OPT3 sub2 taken"); c1 =0; }
  if (c1 == 4)  {Serial.println("action OPT3 sub1 taken"); c1 =0; }
 if (c1 == 3)  { c1 = 4; Serial.println("OPT3 sub1"); }
     delay(300);
    
 }  }

void change() {  int y =0;
while ( y < 20) {  if(digitalRead(resetpin) == HIGH) { c1 = 0; return; }
if (digitalRead(changep) == HIGH) { Serial.println(y); y++; delay(200); }  
    }
 c1 = 0;
 }

So basically return breaks anything it´s going on the loop and go ahead?
I was trying to understand http://www.arduino.cc/en/Reference/Return and really cant understand how it breaks the code from the reference page (maybe language gap).
Now i realized that can achieve same result using:

void change() {  int y =0;
while ( y < 20) {  if(digitalRead(resetpin) == HIGH) { c1 = 0; break; }
if (digitalRead(changep) == HIGH) { Serial.println(y); y++; delay(200); }  
    }
 c1 = 0;
 }

Thanks,

Rodrigo

The compiler converts the text you type in into machine code. The function has a start address, and the code follows that address. When the function gets called, some data is pushed onto stacks (the function arguments and the location in code that follows the function caller's location).

The return statement simply causes the code to jump to the address specified as the return location. There is an return statement required at the end of every function that returns a value, followed by the value to return.

A return can occur anywhere within a function. Purists insist that there should only be one return for any given function. While that is almost always possible, it often makes for more readable code to have more than one return statement.

The break that you are using in the 2nd example causes a jump out of the while loop. If there were more code following the while loop, that code would be executed. In the first example, nothing else in the change function would be executed.