Stepper instant stop

Hi to everyone,I am doing for a school project a foil tensioner and i’m a little stuck. The machine will put a signal when a object it’s welded, this part i’m simulating with a button. When arduino recive a signal he spins the stepper until the other sensor (ir diode and ir reciver) gives him a signal that he moved the foil enough and the spinning must be stoped. So,i made the easy part → spinning the stepper but i don’t know how to stop it. Tried with an if statment but it does not respond because stepper library it’s blocking?. I tried even with a interrupt but i’m not sure if i write it correctly. The board it’s arduino uno. Does anyone have a clever solution?

#include <Stepper.h>
#include "U8glib.h"

//------------------------------------------------
#define motorSteps 200     // change this depending on the number of steps
const int StrojSignal = 0;  
const int IrSenzor = 9;
const int Tipka1 = 7;  
const int Tipka2 = 6;
#define motorPin1 13
#define motorPin2 12
#define motorPin3 11
#define motorPin4 10

 int val;

int dolzina = 0;
int buttonState = 0;  
int state1 = 0;  
// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1,motorPin2, motorPin3,motorPin4); 
//Stepper myStepper2(motorSteps, motorPin12,motorPin22, motorPin32,motorPin42); 


void setup() {
attachInterrupt(1, stop1, RISING);
  
  // set the motor speed at 60 RPMS:
  myStepper.setSpeed(100);
  pinMode(StrojSignal, INPUT);
    pinMode(IrSenzor, INPUT);
      pinMode(Tipka1, INPUT);
  pinMode(Tipka2, INPUT);
  // Initialize the Serial port:
  Serial.begin(9600);
 
}

void loop() {

   buttonState = digitalRead(Tipka1);
if (buttonState == LOW) {
    // Step forward 100 steps:
  Serial.println("Forward");
  myStepper.step(800);

 state1 = digitalRead(Tipka2);
if (state1 == HIGH) {
digitalWrite(13,LOW);
 digitalWrite(12,LOW);
 digitalWrite(11,LOW);
 digitalWrite(10,LOW); 
}
}
}

  void stop1()
{

digitalWrite(13,LOW);
 digitalWrite(12,LOW);
 digitalWrite(11,LOW);
 digitalWrite(10,LOW); 
}

Hi,
looking at the stepper library, you could do myStepper.step(small number of steps) to advance step by step as long as Tipka2 is LOW (the number of steps could be 10 or something).
Because 800 steps means 4 full revolutions and this takes a while; durnig this time your program does nothing but wait the end of the step(...) function, so even if your sensor turns HIGH in the mean, you will not read it and hence not act accordingly...
HTH
Manu

I would go further than @manufwi. I would check the state of the switch after every step.

...R
Stepper Motor Basics
Simple Stepper Code

manufwi:
Hi,
looking at the stepper library, you could do myStepper.step(small number of steps) to advance step by step as long as Tipka2 is LOW (the number of steps could be 10 or something).
Because 800 steps means 4 full revolutions and this takes a while; durnig this time your program does nothing but wait the end of the step(...) function, so even if your sensor turns HIGH in the mean, you will not read it and hence not act accordingly...
HTH
Manu

Yes, 800 it's definitly too much,but even if it makes 10 steps i wont be able to stop it at 7. step because it's bloking library?

drimer525:
,but even if it makes 10 steps i wont be able to stop it at 7. step because it's bloking library?

So don't use the library? Or just use it to move 1 step at a time?

Post a link to the datasheet for your stepper motor and tell us what stepper motor driver you are using.

...R

This is of stepper motor, i'm not using stepper driver instead i'm driving the coils with mosfet transistors IRLZ44N

I did try without library but the motor seems to have a lot less power to tense the foil.

drimer525:
This is of stepper motor, i'm not using stepper driver instead i'm driving the coils with mosfet transistors IRLZ44N

That sounds like hard work. It also means you can't drive the motor with a high voltage for better performance - unless you make very sophisticated circuits with over-current protection.

I did try without library but the motor seems to have a lot less power to tense the foil.

Then there was something wrong with your program. Maybe have a look at the source code for the library to see how they do it. Anyway, you can still use the library to move 1 step at a time.

...R

void loop() {
  
tipka_stanje= digitalRead(Tipka);
while(tipka_stanje == LOW){
 stepper();

tipka_stanje1= digitalRead(Tipka1);
 if (tipka_stanje1 == LOW) {
  break;
  stepperStop();
  }
  }
  } 


void stepperStop()
{
digitalWrite(13,LOW);
digitalWrite(12,LOW);
digitalWrite(11,LOW);
digitalWrite(10,LOW); 
}


void stepper()
{
myStepper.step(100);
}

Why the break in if statment does not stop it ?

Because break stops at that point. It does not execute stepperStop() because that is after the break.

tipka_stanje= digitalRead(Tipka);

while(tipka_stanje == LOW){

You don't update tipka_stanje inside the loop, so the only way of exiting the loop is the break statement. Is this what you intended? Usually you would put the digitalRead() inside the loop.

MorganS:
You don't update tipka_stanje inside the loop, so the only way of exiting the loop is the break statement. Is this what you intended? Usually you would put the digitalRead() inside the loop.

in fact no...i want when i click just one time that the stepper will rotate,and then with stop it with tipka_stane1 whenever i want

  1. steppers do not stop instantly
  2. AccelStepper library is better for this sort of thing (driving steppers flexibly).