Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
added image
Source Link
Sonali_B
  • 443
  • 4
  • 19

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

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

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

edited title
Link
Sonali_B
  • 443
  • 4
  • 19

RESET and RESUME case in Arduino mega 2560 code (PUSHBUTTON AS INTERRUPT)

Format code, capitalize Arduino
Source Link
user31481
user31481

I am working on Arduino megaMega 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 dirpin_T1 = 2;

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

digitalWriteI want to code Arduino such that when I press RESET button (steppin_T2, HIGHnot talking about RESET button on Arduino); // "Rising Edge" so, Arduino should run the easydriver knows to when to stepcode again from beginning.

delayMicrosecondsAnd when I press RESUME button, Arduino should run the code from the moment I pressed the RESET button (50like 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);
        }
    }
}

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 dirpin_T1 = 2;

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);

}

}

}

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