Pages: [1]   Go Down
Author Topic: Interrupting switch (mode)  (Read 286 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 46
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

I have my code running well for a stepper motor/weird clock I am working on.

I am using switch/mode to trigger different timings on the motor.

I want to be able to interupt case 2 when it is running by pressing startBswitch and have it immediatly go to case 0, stopping the sequence.

Currently it works but only if I hold down the switchB for more than 6 seconds, which I assume is due to it needing to clear the
6545ms delay I have running for every sequence in the mode.  The delay is required for adjusting my timings on the stepper
and I cant really touch it.

Is there a way of having the button press immediately interrupt case 2, regardless of whether it is in the delay period?

Here is the relevent part of the code:

Code:
void loop() {
   
if ( mode == 0 && digitalRead (setupAswitch) == LOW){ mode = 1;}
if ( mode == 0 && digitalRead (startAswitch) == LOW){ mode = 2;}
if ( mode == 2 && digitalRead (startBswitch) == LOW){ mode = 0;}


switch (mode){

 
case 0:  // do nothing and reset all counters
motor.release();
CountA = 0;
CountB = 0;
CountC = 0;
break;
///
///
case 1:  // Set Clock(Stepper) start position A
 motor.setSpeed(100);
 motor.step(1, BACKWARD, DOUBLE);
 if (digitalRead(reedswitchA) == LOW) mode = 8; // Stop shen it hits reedswitch go to mode 8 (REED discrepancy)
break;
///
/// 
case 2: // Run Stepper Prog A
 motor.setSpeed(100);
 motor.step(2, BACKWARD, SINGLE);
if ( CountA++ >776 ) mode = 0 ;
delay (6545); //6545ms delay
break;

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46029
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is there a way of having the button press immediately interrupt case 2, regardless of whether it is in the delay period?
No, so don't use long delay()s like that. Look at the Blink Without Delay example.
Logged

Pages: [1]   Go Up
Jump to: