Skip to main content
2 of 4
Format code, capitalize Arduino
user avatar
user avatar

RESET and RESUME case in Arduino mega 2560 code

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);
        }
    }
}
Sonali_B
  • 443
  • 4
  • 19