0

enter image description here I am working on Arduino Mega 2560 which is controlling four steppers with BT6560 Driver, six pushbuttons and four limitswitches.

I want to code Arduino such that when I press RESET button (not talking about RESET button on Arduino), Arduino should run the code again from beginning.

And when I press RESUME button, Arduino should run the code from the moment I pressed the RESET button (like any song is resumed).

How can I be able to do this?

Below is a code I am working on:

int dirH_T1 = 3;
int steppin_T1 = 4;
int dirpin_F1 = 5;
int dirH_F1 = 6;
int steppin_F1 = 7;
int dirpin_T2 = 8;
int dirH_T2 = 9;
int steppin_T2 =10;
int dirpin_F2 = 11;
int dirH_F2 = 12;
int steppin_F2 =13;
int PB17ACW=23;
int PB17ACCW=22;
int PB17BCW=25;
int PB17BCCW=24;
int S01 = 34; // SENSOR INPUTS
int S02 = 35;
int S03 = 36;
int S04 = 37;

void setup()
{
    pinMode(dirpin_T1, OUTPUT);
    pinMode(dirH_T1, OUTPUT);
    pinMode(steppin_T1, OUTPUT);
    pinMode(dirpin_F1, OUTPUT);
    pinMode(dirH_F1 , OUTPUT);
    pinMode(steppin_F1, OUTPUT);

    pinMode(dirpin_T2, OUTPUT);
    pinMode(dirH_T2, OUTPUT);
    pinMode(steppin_T2, OUTPUT);
    pinMode(dirpin_F2, OUTPUT);
    pinMode(dirH_F2 , OUTPUT);
    pinMode(steppin_F2, OUTPUT);

    pinMode(PB17ACW,INPUT_PULLUP);
    pinMode(PB17ACCW,INPUT_PULLUP);
    pinMode(PB17BCW,INPUT_PULLUP);
    pinMode(PB17BCCW,INPUT_PULLUP);

    pinMode(S01,INPUT);
    pinMode(S02,INPUT);
    pinMode(S03,INPUT);
    pinMode(S04,INPUT);

}
void loop()
{
    if(digitalRead(PB17ACW)==LOW &&  digitalRead(PB17ACCW)==LOW) {
// STOP MOTOR WHEN NO KEY PRESSED
    }

// FOR FILAMENT 1 FORWARD FEEDER
    if(digitalRead(PB17ACW)==HIGH &&  digitalRead(PB17ACCW)==LOW) {
        digitalWrite(dirpin_T1, LOW);      // Set the direction.
        digitalWrite(dirH_T1, LOW);
        delay(1000);

        for(int i = 0; i<32767; i++) {      // Iterate for 4000 microsteps.(32767)
            digitalWrite(steppin_T1, LOW);  // This LOW to HIGH change is what creates the
            digitalWrite(steppin_T1, HIGH); // "Rising Edge" so the easydriver knows to
            when to step.
            delayMicroseconds(50);
        }

        digitalWrite(dirpin_F1, LOW);      // Set the direction.
        digitalWrite(dirH_F1, LOW);
        delay(1000);

        do {
            digitalWrite(steppin_F1, LOW);  // This LOW to HIGH change is what creates the

            digitalWrite(steppin_F1, HIGH); // "Rising Edge" so the easydriver knows to when to step.
            delayMicroseconds(50);

        } while(digitalRead(S01)==LOW);

        delay(1000);

        digitalWrite(dirpin_T1, LOW);      // Set the direction.
        digitalWrite(dirH_T1, HIGH);
        delay(1000);

        for(int i = 0; i<32767; i++) {      // Iterate for 4000 microsteps.(32767)
            digitalWrite(steppin_T1, LOW);  // This LOW to HIGH change is what creates the
            digitalWrite(steppin_T1, HIGH); // "Rising Edge" so the easydriver knows to
            when to step.
            delayMicroseconds(50);

        }
    }

// FOR FILAMENT 1 BACKWARD FEEDER
    if(digitalRead(PB17ACW)==LOW &&  digitalRead(PB17ACCW)==HIGH) {
        digitalWrite(dirpin_T1, LOW);      // Set the direction.
        digitalWrite(dirH_T1, LOW);
        delay(1000);

        for(int i = 0; i<32767; i++) {      // Iterate for 4000 microsteps.(32767)
            digitalWrite(steppin_T1, LOW);  // This LOW to HIGH change is what creates the
            digitalWrite(steppin_T1, HIGH); // "Rising Edge" so the easydriver knows to when to step.
            delayMicroseconds(50);

        }

        digitalWrite(dirpin_F1, LOW);      // Set the direction.
        digitalWrite(dirH_F1, HIGH);
        delay(1000);

        do {
            digitalWrite(steppin_F1, LOW);  // This LOW to HIGH change is what creates the
            digitalWrite(steppin_F1, HIGH); // "Rising Edge" so the easydriver knows to when to step.
            delayMicroseconds(50);
        } while(digitalRead(S02)==LOW);

        delay(1000);
        digitalWrite(dirpin_T1, LOW);      // Set the direction.
        digitalWrite(dirH_T1, HIGH);
        delay(1000);

        for(int i = 0; i<32767; i++) {      // Iterate for 4000 microsteps.(32767)
            digitalWrite(steppin_T1, LOW);  // This LOW to HIGH change is what creates the
            digitalWrite(steppin_T1, HIGH); // "Rising Edge" so the easydriver knows to when to step.
            delayMicroseconds(50);
        }
    }

    // FOR FILAMENT 2 FORWARD FEEDER
    if(digitalRead(PB17BCW)==HIGH &&  digitalRead(PB17BCCW)==LOW) {
        digitalWrite(dirpin_T2, LOW);      // Set the direction.
        digitalWrite(dirH_T2, LOW);
        delay(1000);

        for(int i = 0; i<32767; i++) {      // Iterate for 4000 microsteps.(32767)
            digitalWrite(steppin_T2, LOW);  // This LOW to HIGH change is what creates the
            digitalWrite(steppin_T2, HIGH); // "Rising Edge" so the easydriver knows to when to step.
            delayMicroseconds(50);
        }

        digitalWrite(dirpin_F2, LOW);      // Set the direction.
        digitalWrite(dirH_F2, LOW);
        delay(1000);

        do {
            digitalWrite(steppin_F2, LOW);  // This LOW to HIGH change is what creates the
            digitalWrite(steppin_F2, HIGH); // "Rising Edge" so the easydriver knows to when to step.
            delayMicroseconds(50);
        } while(digitalRead(S03)==LOW);

        delay(1000);

        digitalWrite(dirpin_T2, LOW);      // Set the direction.
        digitalWrite(dirH_T2, HIGH);
        delay(1000);

        for(int i = 0; i<32767; i++) {      // Iterate for 4000 microsteps.(32767)
            digitalWrite(steppin_T2, LOW);  // This LOW to HIGH change is what creates the
            digitalWrite(steppin_T2, HIGH); // "Rising Edge" so the easydriver knows to when to step.
            delayMicroseconds(50);

        }
    }

// FOR FILAMENT 2 BACKWARD FEEDER
    if(digitalRead(PB17BCW)==LOW &&  digitalRead(PB17BCCW)==HIGH) {
        digitalWrite(dirpin_T2, LOW);      // Set the direction.
        digitalWrite(dirH_T2, LOW);
        delay(1000);

        for(int i = 0; i<32767; i++) {      // Iterate for 4000 microsteps.(32767)
            digitalWrite(steppin_T2, LOW);  // This LOW to HIGH change is what creates the
            digitalWrite(steppin_T2, HIGH); // "Rising Edge" so the easydriver knows to when to step.
            delayMicroseconds(50);
        }

        digitalWrite(dirpin_F2, LOW);      // Set the direction.
        digitalWrite(dirH_F2, HIGH);
        delay(1000);

        do {
            digitalWrite(steppin_F2, LOW);  // This LOW to HIGH change is what creates the
            digitalWrite(steppin_F2, HIGH); // "Rising Edge" so the easydriver knows to when to step.
            delayMicroseconds(50);

        } while(digitalRead(S04)==LOW);

        delay(1000);

        digitalWrite(dirpin_T2, LOW);      // Set the direction.
        digitalWrite(dirH_T2, HIGH);
        delay(1000);

        for(int i = 0; i<32767; i++) {      // Iterate for 4000 microsteps.(32767)
            digitalWrite(steppin_T2, LOW);  // This LOW to HIGH change is what creates the
            digitalWrite(steppin_T2, HIGH); // "Rising Edge" so the easydriver knows to when to step.
            delayMicroseconds(50);
        }
    }
}
  • It looks like you want something like the "hibernate" option on a desktop computer. Am I right? – user31481 Nov 7 '17 at 10:53
  • 1
    I think you want to implement your code as a Finite State Machine where "reset" is just one of many states the machine can be in. – Majenko Nov 7 '17 at 11:48
  • Let me see if I understand you: 1) Your code is happily running; 2) You preset your own-reset-button, the Arduino start your code from the begining (like a real reset); 3) Your code is happily running again; 4) You press your own-resume-button and; 5) Arduino stop the running code and; 6) resume running the same code as you left it in step 2). Is that right? – user31481 Nov 17 '17 at 12:35
  • yes and currently i am focussing on a simple pushbutton but it has debouncing problem so if any other switch you can suggest if that would make things perfect and efficient. – Sonali_B Nov 17 '17 at 12:55
1

Doing the "hibernate" thing in a general fashion you need to:

  1. Capture the relevant status of the whole system (Arduino + modules). That include registers, the stack and all variables in your program.
  2. Write it to some storage medium (EEPROM, SD, whatever).
  3. Upon resume, restore registers, stack, variable and reinicialize all modules (sensors, relays, ...)

Arduino can't do that.

Your only alternative is to use, as @Majenko says, a finite state machine and take care yourself to restore execution to the point where button was pressed.

  • Actually, the Arduino can do these things, and arguably much better and more simply than your typical desktop computer can. But it would probably be more realistic to keep the RAM powered to retain contents, and merely suspend most clocks and dynamic logic, de-power internal and external peripherals, etc. In fact many laptops have an MCU not unlike an Arduino that handles key parts of the hibernation and power logic. – Chris Stratton Nov 17 '17 at 17:03
  • @ChrisStratton. Your proposition is way beyond the OP's possibilities. It's objetable from a design perspective. It involve lot of hardware details that may change in the next Arduino model, it's hard to write and hard to debug. Meanwhile, any competent Android programmer, for example, had to deal with the fact that his dialog (and the activity under it) will die when the user rotate the phone. Funny. And then he had to restart his activity and re-display the dialog as the user last saw it. And he do it by hand. – user31481 Nov 17 '17 at 17:26

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.