Go Down

### Topic: Need to stop my function (Read 670 times)previous topic - next topic

##### Nov 21, 2012, 05:26 am
Im trying to interrupt my current function. Currently, when I press a button, my stepper motor turns. But I cannot stop it in the middle of it turning. It completes the full rotation before i can press the button again to make it rotate.

Code: [Select]
`#include <IRremote.h>#define button1 16582903#define button2 16615543#define button3 16599223#define RECV_PIN  10IRrecv irrecv(RECV_PIN);decode_results results;long Received = 0 ;#define DIR_PIN 2#define STEP_PIN 3void setup() {  pinMode(DIR_PIN, OUTPUT);  pinMode(STEP_PIN, OUTPUT);  pinMode(RECV_PIN , INPUT);    Serial.begin(9600);  irrecv.enableIRIn(); // Start the receiver} void loop(){ if (irrecv.decode(&results)) {    Received = results.value ;      switch (Received) {    case button1:      digitalWrite (STEP_PIN, HIGH);      digitalWrite (DIR_PIN, HIGH);      //rotate a specific number of degrees       rotateDeg(3600, .5);      delay(1000);      break;    case button2:      digitalWrite( STEP_PIN, HIGH);      digitalWrite( DIR_PIN, LOW);      rotateDeg(-3600, .5);  //reverse      delay(1000);      break;      }    irrecv.resume(); // Receive the next value  }}void rotateDeg(float deg, float speed){  //rotate a specific number of degrees (negitive for reverse movement)  //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger  int dir = (deg > 0)? HIGH:LOW;  digitalWrite(DIR_PIN,dir);   int steps = abs(deg)*(1/0.225);  float usDelay = (1/speed) * 70;  for(int i=0; i < steps; i++){    digitalWrite(STEP_PIN, HIGH);    delayMicroseconds(usDelay);     digitalWrite(STEP_PIN, LOW);    delayMicroseconds(usDelay);        if (Received==button2){       digitalWrite(STEP_PIN, LOW);       digitalWrite( DIR_PIN, HIGH);      break;    }  }}`

For the last part, i thought that the break would jump it out of the for loop? Am I mistaken?

#### marco_c

#1
##### Nov 21, 2012, 07:47 amLast Edit: Nov 21, 2012, 12:46 pm by marco_c Reason: 1
You need to eliminate the delays from your code. These stop anything else from happening while they are active.

Look at the Blink without delays example on how to use the millis() function to determine what time has passed each time through the loop. You will need to remember what you were doing at each stage so that you can pick it up the next time through (look up Finite State Machine).
Arduino Libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com
Arduino++ blog https://arduinoplusplus.wordpress.com

#### pylon

#2
##### Nov 21, 2012, 11:04 am
Additionally to the delay issue marco_s already mentioned, you don't have an interrupt handler, so why do you think that the loop() or, even more important in your case, rotateDeg() will be interrupted? You check for a IR code reception only in the loop(), so that's the only point where you can react on it. If you wanna be able to interrupt the stepper while it's turning, you have to check in every step of the for loop in rotateDeg() function.

Go Up

Please enter a valid email to subscribe

To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy