Stepper motor repeat/interupt code

I am using a Duemilanove with an Arduino R3 motor shield to control a bipolar stepper motor. I have the stepper working/rotating fine with basic code.

Firstly

this snippet taken from my code (full code below)

case 4: // Run Stepper Prog A
myStepper.step(1);
delay(6000);
mode = 6;
break;

How do I get the first two lines of this case to run 500 times before moving onto the next mode (mode 6)?

Secondly

Case 1 & 2, shown in the code below. I want to rotate the stepper until it hits the reed switch (reed switchA or B) then stop and move onto the next mode. I am stuck on how exactly what code to use to interupt the stepper rotation when it hits the switch?

Thanks in advance, this board is my saviour.

#include <Stepper.h>
 
 const int stepsPerRevolution = 200;  // Motor Resolution
 Stepper myStepper(stepsPerRevolution, 12,13);  // initialize the stepper library on the motor shield     
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //SETUP PINS
 
 // Stepper Pins
 const int pwmA = 3;
 const int pwmB = 11;
 const int brakeA = 9;
 const int brakeB = 8;
 const int dirA = 12;
 const int dirB = 13;
 int x = 0;
 
 // Switch Pins
 
 byte setupAswitch = 2;
 byte setupBswitch = 4;
 byte startAswitch = 5;
 byte startBswitch = 6;
 const int reedswitchA = 7;
 const int reedswitchB = 10;
 
 byte mode; // 0 = waiting to start, 1 = setupA, 2 = setupB, 3 = post setup, pre-startup, 4 = A code, 5 = Bcode, 6 = post A/B startup code
 
/////////////////////////////////////////////////////////////////////////////////////////////////// 
 
void setup() {
 Serial.begin(9600);

// Setup Stepper Pins
 pinMode(pwmA, OUTPUT);
 pinMode(pwmB, OUTPUT);
 pinMode(brakeA, OUTPUT);
 pinMode(brakeB, OUTPUT);
 digitalWrite(pwmA, HIGH);
 digitalWrite(pwmB, HIGH);
 digitalWrite(brakeA, LOW);
 digitalWrite(brakeB, LOW);
 
 // Setup switch pins
 
 pinMode (setupAswitch, INPUT); // set up the various push switches
 digitalWrite (setupAswitch, HIGH); // turn on internl pullup - check for a LOW input to indicate switch is closed to ground
 pinMode (setupBswitch, INPUT);
 digitalWrite (setupBswitch, HIGH);
 pinMode (startAswitch, INPUT);
 digitalWrite (startAswitch, HIGH);
 pinMode (startBswitch, INPUT);
 digitalWrite (startBswitch, HIGH);
 
 pinMode (reedswitchA, INPUT);  // set up the reed detector switches
 digitalWrite (reedswitchA HIGH);
 pinMode (reedswitchB, INPUT);
 digitalWrite (reedswitchB HIGH);
 
 
 Serial.begin(9600);  //initialise the serial port
 myStepper.setSpeed(1); // // set the motor speed:
 }

 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //VOID LOOP
 
 void loop() {
if (mode ==0 && digitalRead(setupAswitch) == LOW){ mode = 1;}
if (mode == 0 && digitalRead(setupBswitch_ == LOW) {mode = 2;}
if ( mode == 3 && digitalRead (startAswitch) == LOW){ mode = 4;}
if ( mode == 3 && digitalRead (startBswitch) == LOW){ mode = 5;}
switch (mode){

case 0:  // do nothing
break;

case 1:  // Set Stepper start position A
//Rotate until reed switchA is LOW
mode = 3;
break;

case 2:  // Set Stepper start position B
// Rotate until reed switchB is LOW
mode = 3;
break;

case 3:// do nothing
break;

case 4: // Run Stepper Prog A
 myStepper.step(1);
 delay(6000);
mode = 6;
break;

case 5: // Run Stepper Prog B
 myStepper.step(1);
 delay(2608);
mode = 6;
break;

case 6:
// whatever is done after setup & start
break;
} // end switch

} // end loop

The solution depends on if you need to run loop() during the initial sequence (f.ex. to blink a LED, move another motor or check a temperature meter)

If you only need to do this motion then something like

case 4: // Run Stepper Prog A
 for ( int c=0; i<500; i++) {
   myStepper.step(1);
   delay(6000);
   }
mode = 6;
break;

would work BUT this means that the machine does nothing else for 50 minutes.

If you need to monitor - fex. an quick stop switch - then

Define variable CountA, set it to 0

In case 4 fix the last line to be
if ( CountA++ >500 ) mode = 6 ;

This way the code will (as long as the first 4 lines do not change it) repeatedly do case 4 for each loop() pass, until it has executed 500 times.

thanks, sorry to be slow (newbie)

but is this what It should look like?

const int CountA = 0;

case 4: // Run Stepper Prog A
myStepper.step(1);
delay(6000);
if ( CountA++ >500 ) mode = 6 ;
break;

Is this all I need to add?

I also have another case that I want to loop for a different amount of time, can I just define another variable say CountB and do the same with a different amount in the other case?

Just realised my error there, should have been

int CountA = 0;

worked a charm.

Any ideas about my case 1 & 2 reed switch problem?

Thanks

Switches...

if (mode ==0 && digitalRead(setupAswitch) == LOW){ mode = 1;}

will read the reed :slight_smile: and only switch into mode 1, from the startup state. Nice code. That part works. And it will not be confused with button bounce.

case 1:  // Set Stepper start position A
//Rotate until reed switchA is LOW
mode = 3;
break;

I presume that is a place holder for now, 'cause mode 3 does nothing, exect allow us to move onto mode 4&5 on the next pass of the loop. (approx 10-odd microseconds later.)

Similar to the case 4, we test the condition for leaving mode 1, and if not do something in mode 1. (I really want you to find the solution yourself :slight_smile: )

The test will be digitalRead(reedswitchA), and on to mode 3 else do a Stepper.step and delay a while. (if it is +1 or -1 is your choice)