Running motors when hit threshold

Hello there!
Quickly about my project,
I'm trying to do an automated wire cutter as school project, the idea is that one motor will feed the wire through encoder which is going to measure length (which is supposed to be shown on LCD), when desired length is going to be hit (in this scenario 23mm) the first motor will shut of and second one will do one full cycle as to cut off the desired length.

The issue I'm having at the moment is I cannot sort out how to keep the first motor running until it hits the threshold and then engaging the second one just for that one cycle.

I made it work that one of the motors works all the time but arduino doesn't record the inputs from encoder and displays nothing on LCD
The other scenario is that when the threshold is hit given by encoder the other motor spins all the time, which is supposed to do only one full spin.

#include <MultiStepper.h>
#include <AccelStepper.h>
#include <Stepper.h>
 #include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C  lcd(0x27,2,1,0,4,5,6,7,3,POSITIVE);

 const int stepPin1 = 3; 
 const int dirPin1 = 4;
 const int stepPin2 = 5;
 const int dirPin2 = 6;
 #define inputA 9
 #define inputB 8
 int counter = 0;
 int angle = 0; 
 int aState;
 int aLastState;  

 
void setup() {
  

  pinMode (inputA,INPUT);
  pinMode (inputB,INPUT);
  pinMode(stepPin1,OUTPUT); 
  pinMode(dirPin1,OUTPUT);
  pinMode(stepPin2, OUTPUT);
  pinMode(dirPin2, OUTPUT);
  
  aLastState = digitalRead(inputA);
  lcd.begin(16,2);
}
void loop() {

  aState = digitalRead(inputA);
  
  if (aState != aLastState){     
     if (digitalRead(inputB) != aState) { 
       counter ++;
       angle ++;
    
     }
     else {
       counter--;
       angle --;
    
   
     }
     
     lcd.clear();
     lcd.print("Length: ");
     lcd.print(int(counter*(1.15)));
     lcd.print("mm"); 
     lcd.setCursor(0,0);
     
   }
  aLastState = aState;
  
  if (angle == 20) {
       digitalWrite(dirPin1,HIGH);
  for(int x = 0; x < 10; x++) {
    digitalWrite(stepPin1,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin1,LOW); 
    delayMicroseconds(500); 
  }

   
  if (angle != 20) {
           digitalWrite(dirPin2,HIGH); 
  for(int x = 0; x < 10; x++) {
    digitalWrite(stepPin2,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin2,LOW); 
    delayMicroseconds(500); 
  }

    
  } 
}
}

Thanks!

PS: I'm fairly new to the arduino scene, sorry if any of this is not up to standard or question seems stupid, I tried to google couple of solutions but couldn't find much.

PPS: Im using Pololua4988 drivers for motors.

Nathrezen:
Hello there!
Quickly about my project,
I'm trying to do an automated wire cutter as school project, the idea is that one motor will feed the wire through encoder which is going to measure length (which is supposed to be shown on LCD), when desired length is going to be hit (in this scenario 23mm) the first motor will shut of and second one will do one full cycle as to cut off the desired length.

The issue I'm having at the moment is I cannot sort out how to keep the first motor running until it hits the threshold and then engaging the second one just for that one cycle.

I made it work that one of the motors works all the time but arduino doesn't record the inputs from encoder and displays nothing on LCD
The other scenario is that when the threshold is hit given by encoder the other motor spins all the time, which is supposed to do only one full spin.

#include <MultiStepper.h>

#include <AccelStepper.h>
#include <Stepper.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C  lcd(0x27,2,1,0,4,5,6,7,3,POSITIVE);

const int stepPin1 = 3;
const int dirPin1 = 4;
const int stepPin2 = 5;
const int dirPin2 = 6;
#define inputA 9
#define inputB 8
int counter = 0;
int angle = 0;
int aState;
int aLastState;

void setup() {

pinMode (inputA,INPUT);
  pinMode (inputB,INPUT);
  pinMode(stepPin1,OUTPUT);
  pinMode(dirPin1,OUTPUT);
  pinMode(stepPin2, OUTPUT);
  pinMode(dirPin2, OUTPUT);
 
  aLastState = digitalRead(inputA);
  lcd.begin(16,2);
}
void loop() {

aState = digitalRead(inputA);
 
  if (aState != aLastState){   
    if (digitalRead(inputB) != aState) {
      counter ++;
      angle ++;
   
    }
    else {
      counter--;
      angle --;
   
 
    }
   
    lcd.clear();
    lcd.print("Length: ");
    lcd.print(int(counter*(1.15)));
    lcd.print("mm");
    lcd.setCursor(0,0);
   
  }
  aLastState = aState;
 
  if (angle == 20) {
      digitalWrite(dirPin1,HIGH);
  for(int x = 0; x < 10; x++) {
    digitalWrite(stepPin1,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin1,LOW);
    delayMicroseconds(500);
  }

if (angle != 20) {
          digitalWrite(dirPin2,HIGH);
  for(int x = 0; x < 10; x++) {
    digitalWrite(stepPin2,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin2,LOW);
    delayMicroseconds(500);
  }

}
}
}




Thanks!

PS: I'm fairly new to the arduino scene, sorry if any of this is not up to standard or question seems stupid, I tried to google couple of solutions but couldn't find much.

PPS: Im using Pololua4988 drivers for motors.

I am going to bite holes in my tongue to keep from exploding about the way you have not been taught to program. You should get only one thing working at a time and understand how to control it before moving on to the next. Also did not teach you to use meaningful names for variables, so I can follow you logic.

But back to your stated problem. Your motor doesn't run to hit a threshold. What does that mean? Is there some kind of a pin or block to stop the motor? Or do you mean there is a value somewhere that a second value is supposed to match? Where do those values come from? Have you used the Serial.println() to display those values and actually see if they are correct?

Paul

Yes, my code is pretty poor but I'm trying to learn as I go (with guides and tutorials), sorry for it to be so sloppy.
I tried most of the stuff so far on it's own (from couple of guides) but when I try to do it at the same time it falls apart.

By threshold I meant that if if encoder hits 20 (counts).
That's why i tried command:

if (angle == 20)/code]
To engage at that moment stepper motor, which works, but cannot make it work for only one cycle. As long as I keep encoder at same position (not moving it at all) the motor keeps on spinning.

Nathrezen:
Yes, my code is pretty poor but I'm trying to learn as I go (with guides and tutorials), sorry for it to be so sloppy.
I tried most of the stuff so far on it's own (from couple of guides) but when I try to do it at the same time it falls apart.

By threshold I meant that if if encoder hits 20 (counts).
That's why i tried command:

if (angle == 20)/code]

To engage at that moment stepper motor, which works, but cannot make it work for only one cycle. As long as I keep encoder at same position (not moving it at all) the motor keeps on spinning.

So print the values of "angle" and "code" . What does "/code" produce? Do you mean ((angle/code) == 20)?

Paul

I'm sorry, my post was supposed to look like this:

if (angle == 20){
       digitalWrite(dirPin1,HIGH);
  for(int x = 0; x < 200; x++) {
    digitalWrite(stepPin1,HIGH); 
    delayMicroseconds(500); 
    digitalWrite(stepPin1,LOW); 
    delayMicroseconds(500);

To engage at that moment stepper motor, which works, but cannot make it work for only one cycle. As long as I keep encoder at same position (not moving it at all) the motor keeps on spinning.

my mistake, the /code is tot supposed to be there, only for purpose of post formatting.

I'm sorry, could you elaborate on "print"? Because I used those command for lcd which works great, after hitting the angle "20" the motor engages but it just won't stop. Could you maybe tell me if the "if" command is suitable for function like that?[/code]

Look at the IDE example programs. Almost all use the IDE screen for output of messages. Duplicate how that is done into your program.

Paul