Pages: [1]   Go Down
Author Topic: Need to stop my function  (Read 440 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


#include <IRremote.h>

#define button1 16582903
#define button2 16615543
#define button3 16599223
#define RECV_PIN  10

IRrecv irrecv(RECV_PIN);
decode_results results;
long Received = 0 ;

#define DIR_PIN 2
#define STEP_PIN 3

void setup() {
  pinMode(DIR_PIN, OUTPUT);
  pinMode(STEP_PIN, OUTPUT);
  pinMode(RECV_PIN , INPUT);
  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);
    case button2:
      digitalWrite( STEP_PIN, HIGH);
      digitalWrite( DIR_PIN, LOW);
      rotateDeg(-3600, .5);  //reverse
    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;

  int steps = abs(deg)*(1/0.225);
  float usDelay = (1/speed) * 70;

  for(int i=0; i < steps; i++){
    digitalWrite(STEP_PIN, HIGH);

    digitalWrite(STEP_PIN, LOW);
    if (Received==button2){
       digitalWrite(STEP_PIN, LOW);
       digitalWrite( DIR_PIN, HIGH);

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

Sydney, Australia
Offline Offline
Edison Member
Karma: 33
Posts: 1285
Big things come in large packages
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
« Last Edit: November 21, 2012, 06:46:49 am by marco_c » Logged

Arduino libraries
Parola hardware & library

Offline Offline
Faraday Member
Karma: 111
Posts: 5242
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Pages: [1]   Go Up
Jump to: