How to COMPLETELY STOP my step motor

Hello,
how could I make a sub routine to stop all the secuence? like a emergency button?
I'm a freshman `

#include "IRremote.h"// libreria IR
#define BOTON_1 0xFF6897
#define BOTON_2 0xFF9867
#define BOTON_3 0xFFB04F

#define DERECHA 0xFFC23D
#define IZQUIERDA 0xFF22DD



int recibeIR= 11; // detecta control
int driverPUL=7;  // pulsos
int driverDIR=6;  // direccion
int v;

int LEDVERDE =2;
int LEDNARANJA=3;
int LEDAMARILLO=4;

 IRrecv irrecv(recibeIR);
 decode_results codigo;

 void setup (){
    Serial.begin(9600);
    irrecv.enableIRIn(); //recibe info

    pinMode(driverPUL,OUTPUT);
    pinMode(driverDIR,OUTPUT);

    pinMode(LEDVERDE,OUTPUT);
    pinMode(LEDNARANJA,OUTPUT);
    pinMode(LEDAMARILLO,OUTPUT);
  
  }

  void loop (){
   if (irrecv.decode(&codigo)){
    v=codigo.value; // lectura
    Serial.print("Boton: ");
    Serial.println(codigo.value);
    irrecv.resume();
    }

   switch(v){
    // 5 VECES
    case BOTON_1: 
 
        digitalWrite(LEDVERDE,HIGH);
      for(int x=0;x<=2400;x++)
      {
        
         digitalWrite(driverDIR, HIGH); //derecha
         digitalWrite(driverPUL,HIGH);
         delayMicroseconds(600);
         digitalWrite(driverPUL,LOW); // tren de pulsos para activarlo
         delayMicroseconds(600);
        
      }
        digitalWrite(LEDVERDE,LOW);
     break;

     case BOTON_2:
     digitalWrite(LEDNARANJA,HIGH);
      for(int x=0;x<=600;x++)
      {
         digitalWrite(driverDIR, HIGH); //derecha
         digitalWrite(driverPUL,HIGH);
         delayMicroseconds(600);
         digitalWrite(driverPUL,LOW); // tren de pulsos para activarlo
         delayMicroseconds(600);
      }
        digitalWrite(LEDNARANJA,LOW);
      break;

      case BOTON_3:
        digitalWrite(LEDAMARILLO,HIGH);
      for(int x=0;x<=200;x++)
      {
         digitalWrite(driverDIR, LOW); //derecha
         digitalWrite(driverPUL,HIGH);
         delayMicroseconds (1000);
         digitalWrite(driverPUL,LOW); // tren de pulsos para activarlo
         delayMicroseconds(1000);
        
      }
        digitalWrite(LEDAMARILLO,LOW);
      break;

      case DERECHA:
        digitalWrite(LEDAMARILLO,HIGH);
        for(int x=0;x<=20;x++)
      {
         digitalWrite(driverDIR,HIGH); //derecha
         digitalWrite(driverPUL,HIGH);
         delay(10);
         digitalWrite(driverPUL,LOW); // tren de pulsos para activarlo
         delay(10);
        
      }
          digitalWrite(LEDAMARILLO,LOW);
        break;

       case IZQUIERDA:
        digitalWrite(LEDVERDE,HIGH);
        for(int x=0;x<=20;x++)
        {
          digitalWrite(driverDIR,LOW);//IZQUIERDA
          digitalWrite(driverPUL,HIGH);
          delay(10);
          digitalWrite(driverPUL,LOW);
          delay( 10);
        }
        digitalWrite(LEDVERDE,LOW);
        break;
    
   }
    
  }

For informed help, please read and follow the instructions in the "How to get the best out of this forum" post.

Edit your post to add code tags, describe the problem you are having, what you have tried, etc.

1 Like

Edit your post and remove the < and > at the beginning and end of the code.

Next select all code and click the </> button to apply code tags. Lastly, save your post.

In OP's defence, it looks like he tried :wink:

1 Like

Well in C there are no subroutines only functions. So you want to write a function to stop all your functions. Only you don't.

If you are going to stop a sequence then you need the ability to stop it when a logical variable ( known as a boolean variable ) becomes true. So this ability to stop a sequence must be built into the sequence itself.

This is not beginners stuff, and maybe it is beyond you at the moment but it is not too difficult to write when you know how to write code. To that end I have recently put up a tutorial
Commutable Delay
This shows you how to write the sort of delay function that you can stop. The variable that shows a stop request has been made is called "commuted".

In addition to this you will need to also stop any for loops, so your for loops need to be written as

if( commuted ) { // don't even enter this loop if we have been told to stop
     for(int i =0 ; i<max_Iterations ; i++) {  // the start of your for loop
          if( commuted ) i =max_Iterations;  // end the loop if told to stop
         // now check if the stop button has just been pressed 
         if ( digitalRead(changePin) == commuteState)  commuted = true; 
    // now get on with what ever you want to do in this loop
} // now the for loop is finished
} // now the if statement that sees if you want to run the loop at all is finished

This will get you up to the commutableCatchPin function where you can sort out what to do with your code post emergency stop.

1 Like

A real emergency button does not rely on code, but cuts all power to a machine.
Leo..

1 Like

thank you!

thank your so much for your prompt response!
I've been thinking and I belive I've two solutions

  1. use the "sleep mode", but as far as i know I'm not completely sure if it'll work
  2. use millis(); and make in every CASE and after use an IF asking for the emergency button

Well it gets things stoped but you have to think what happens when you take it out of sleep. I would thing this would require a processor reset.

I don't see where millis comes into it. If you want something simpler than I describe in my code above, but not as good, then consider a check of the stop button before issuing the move pulse.

Other options might include disabling the stepping motor driver when the stop button is pressed.

But I think the best answer is given in post#5

Thank you very much!
if it's not too much, could you please tell me the topics that I've to know?
i know the basics of programming in C but maybe with some specific information could do this FASTER, I've a project in mind before the year end.
thank you again!

Well the best things you can learn is how to write code in a state machine way.
Start with the blink without delay example in the IDE, and then go on to study more about it.
See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0

1 Like

Moderator:
Please don´t cross post threads.
The following thread has been closed.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.