Homing a Cut Off Blade

I'm totally at a loss on getting a cutoff blade to home.
The blade is driven by a cam on the shaft of a stepper motor.
An IR sensor is uncovered and covered to provide position.

Here is a drawing of the cam:

The cam rotates clockwise. Figure 1 shows it in the home position. As it moves from Fig. 1 to Fig. 2, the sensor goes from LOW to HIGH. Fig. 2 to Fig. 3 changes from HIGH to LOW.

Assuming I'm starting in a lower quadrant, I would like to single step until the change at Fig. 2, then move 150 steps to put me in the home position.

If starting in a upper quadrant, I would single step to Fig. 3 then move 50 steps to the home position.

This bit of code will move from a lower quadrant to Fig. 2. where the sensor changes.

//Home Cutter Blade.
void HomeBlade(){
  const int IRSensor = 53;
  pinMode(IRSensor,INPUT);
 
if(digitalRead(IRSensor) == LOW) 
    {
    CutStepper.step(1);
    }
  }

And, that is as much as I have managed. As soon as I try more code, it either does nothing or goes into a continuous loop.

Do you need to use INPUT_PULLUP?

CORRECTION: The stepper rotates counterclockwise. Sorry for any confusion.

Wildbill: A pullup isn't needed for the ir sensor. It's one of these: https://create.arduino.cc/projecthub/Raushancpr/arduino-with-ir-sensor-1579b6 Pretty basic.

All my hardware is working fine. I have a logic and/or syntax problem that I just can't seem to get a grip on.

Thanks for looking at it.

Post the problem code. That snippet looks ok.

wildbill:
Post the problem code. That snippet looks ok.

Yes, that code moves the blade exactly as you would expect.
I have reams of 'problem code'.
Here's the code I thought should work when I started:

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

//Home Cutter Blade.
void HomeBlade(){
  const int IRSensor = 53;
  pinMode(IRSensor,INPUT);
 
if(digitalRead(IRSensor) == LOW)  CutStepper.step(1);
        CutStepper.step(150);
        return;
  }

Thing is, it doesn't return so it just goes into a constant loop. >:(

Neither setup() nor loop() ever calls HomeBlade().

This isn't the actual entire program. It's just the homing function. The rest of the code is fine so posting that would just muddy the waters.

Perhaps. But since what you posted does absolutely nothing, it's hard to say what the problem is.

Here you go:

const int LED = 13;
const int MainAuto = 51;
const int MainManual =50;
const int stepsPerRevolution = 200;
 
int i = 0;

#include <Stepper.h>

Stepper FeedStepper(stepsPerRevolution, 2, 3, 4, 5);
Stepper CutStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(MainAuto, INPUT_PULLUP);
  pinMode(MainManual, INPUT_PULLUP);
  digitalWrite(LED, LOW);
  FeedStepper.setSpeed(120);
  CutStepper.setSpeed(150);
  Serial.begin(9600);
}

//Main Program.
void loop() {
  if (digitalRead(MainAuto) == LOW){
  Auto();
  }
  else if (digitalRead(MainManual) == LOW){
  Manual();
  }
  else {
  digitalWrite(LED,LOW);
  i = 0;
  }
}
//End Main Program


//Manual Control
void Manual() {
  HomeBlade();
  const int FeedButton = 49;
  const int CutButton = 48;
  pinMode(FeedButton, INPUT_PULLUP);
  pinMode(CutButton, INPUT_PULLUP);

  if (digitalRead(MainManual) == HIGH){
  return;
  }
  if (digitalRead(FeedButton) == LOW){
  digitalWrite(LED, HIGH);
  FeedStepper.step(10);
  }
  else {
  digitalWrite(LED,LOW);
  digitalWrite(2,LOW);
  digitalWrite(3,LOW);
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);
  }

  if (digitalRead(CutButton) == LOW){
  digitalWrite(LED, HIGH);
  CutStepper.step(200);
  delay(500);
  digitalWrite(LED,LOW);
  digitalWrite(8,LOW);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
  digitalWrite(11,LOW);
  }
}
//End ManualControl

//Run/Auto
void Auto() {
  HomeBlade();
  int NumberOfParts(int Q);
  int PartSize(int L);
  const int StockOut = 52;
  int L = 4;
  int Q = 3;
  pinMode(StockOut, INPUT_PULLUP);
  L = PartSize(L);
  Q = NumberOfParts(Q);
  while (i < Q) {
  if (digitalRead(StockOut) == HIGH)
  i = Q; 
  if (digitalRead(MainAuto) == HIGH)
  i = Q;
  
  FeedStepper.step(L);
  delay (5);
  CutStepper.step(200);
  delay (5);
  i++;}
  
  digitalWrite(LED,LOW);
  digitalWrite(2,LOW);
  digitalWrite(3,LOW);
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);
  
  digitalWrite(8,LOW);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
  digitalWrite(11,LOW);
  
  if (digitalRead(MainAuto) == HIGH){
  return;
  } 
}
//End Auto

//Home Cutter Blade.
void HomeBlade(){
  const int IRSensor = 53;
  pinMode(IRSensor,INPUT);
 
if(digitalRead(IRSensor) == LOW)  CutStepper.step(1);
        CutStepper.step(150);
        return;
}
//End Home Cutter Blade

//Read rotary switch one to get length of part.
int PartSize(int L){
  int rs1p1 = 22;
  int rs1p2 = 23;
  int rs1p3 = 24;
  int rs1p4 = 25;
  int rs1p5 = 26;
  int rs1p6 = 27;
  int rs1p7 = 28;
  int rs1p8 = 29;
  int rs1p9 = 30;
  int rs1p10 = 31;
  int rs1p11 = 32;
  int rs1p12 = 33;
  pinMode(rs1p1, INPUT_PULLUP);
  pinMode(rs1p2, INPUT_PULLUP);
  pinMode(rs1p3, INPUT_PULLUP);
  pinMode(rs1p4, INPUT_PULLUP);
  pinMode(rs1p5, INPUT_PULLUP);
  pinMode(rs1p6, INPUT_PULLUP);
  pinMode(rs1p7, INPUT_PULLUP);
  pinMode(rs1p8, INPUT_PULLUP);
  pinMode(rs1p9, INPUT_PULLUP);
  pinMode(rs1p10, INPUT_PULLUP);
  pinMode(rs1p11, INPUT_PULLUP);
  pinMode(rs1p12, INPUT_PULLUP);
   
  if (digitalRead(rs1p1) == LOW){
  return 9;
  }
  else if (digitalRead(rs1p2) == LOW){
  return 11;
  }
  else if (digitalRead(rs1p3) == LOW){
  return 13;
  }
  else if (digitalRead(rs1p4) == LOW){
  return 15;
  }
  else if (digitalRead(rs1p5) == LOW){
  return 17;
  }
  else if (digitalRead(rs1p6) == LOW){
  return 20;
  }
  else if (digitalRead(rs1p7) == LOW){
  return 22;
  }
  else if (digitalRead(rs1p8) == LOW){
  return 24;
  }
  else if (digitalRead(rs1p9) == LOW){
  return 26;
  }
  else if (digitalRead(rs1p10) == LOW){
  return 28;
  }
  else if (digitalRead(rs1p11) == LOW){
  return 30;
  }
  else if (digitalRead(rs1p12) == LOW){
  return 32;
  }
//End of read rotory switch one.
}
//Read rotary switch two to get number of parts.
int NumberOfParts(int Q){
  int rs2p1 = 34;
  int rs2p2 = 35;
  int rs2p3 = 36;
  int rs2p4 = 37;
  int rs2p5 = 38;
  int rs2p6 = 39;
  int rs2p7 = 40;
  int rs2p8 = 41;
  int rs2p9 = 42;
  int rs2p10 = 43;
  int rs2p11 = 44;
  int rs2p12 = 45;
  pinMode(rs2p1, INPUT_PULLUP);
  pinMode(rs2p2, INPUT_PULLUP);
  pinMode(rs2p3, INPUT_PULLUP);
  pinMode(rs2p4, INPUT_PULLUP);
  pinMode(rs2p5, INPUT_PULLUP);
  pinMode(rs2p6, INPUT_PULLUP);
  pinMode(rs2p7, INPUT_PULLUP);
  pinMode(rs2p8, INPUT_PULLUP);
  pinMode(rs2p9, INPUT_PULLUP);
  pinMode(rs2p10, INPUT_PULLUP);
  pinMode(rs2p11, INPUT_PULLUP);
  pinMode(rs2p12, INPUT_PULLUP);
   
  if (digitalRead(rs2p1) == LOW){
  return 10;
  }
  else if (digitalRead(rs2p2) == LOW){
  return 50;
  }
  else if (digitalRead(rs2p3) == LOW){
  return 100;
  }
  else if (digitalRead(rs2p4) == LOW){
  return 200;
  }
  else if (digitalRead(rs2p5) == LOW){
  return 300;
  }
  else if (digitalRead(rs2p6) == LOW){
  return 400;
  }
  else if (digitalRead(rs2p7) == LOW){
  return 500;
  }
  else if (digitalRead(rs2p8) == LOW){
  return 1000;
  }
  else if (digitalRead(rs2p9) == LOW){
  return 2000;
  }
  else if (digitalRead(rs2p10) == LOW){
  return 3000;
  }
  else if (digitalRead(rs2p11) == LOW){
  return 5000;
  }
  else if (digitalRead(rs2p12) == LOW){
  return 10000;
  }
//End of read rotory switch two.
}

The if in HomeBlade should be a while. As it is, it steps once and assumes it made it to the point where 150 unconditional steps are required.

There may still be problems calling it in manual, but that change should allow you to home properly. It may happen more often than you had in mind though.

As I said in my first post, this works fine:

if(digitalRead(IRSensor) == LOW)
{
CutStepper.step(1);
}

It moves the stepper from anywhere in the two lower quadrants to where the sensor changes state - i.e. Fig. 2.
It then returns control to wherever the function was called from.
Changing LOW to HIGH also works to move the stepper from anywhere in the upper quadrants to Fig. 3.

When I add the CutStepper.step(150); line, it goes into a continuous loop even though it's out of the if statement.

I've tried the while statement and get exactly the same results.

I was particularly hopeful for this bit of failed test code:

//Home Cutter Blade.
void HomeBlade(){
  const int IRSensor = 53;
  pinMode(IRSensor,INPUT);
  j=0;


if(digitalRead(IRSensor) == LOW && (j==0))
  { 
  j=1;
  }
if(digitalRead(IRSensor) == HIGH && (j==0))
  { 
  j=2;
  }

switch(j){
  case 1:
    CutStepper.step(150);   
    break;
  case 2:
    CutStepper.step(50);
    break;
//  default:
//    return;
//  break;
  }
  if (j!=0) return;
}

It also goes into a continuous loop.

I believe I've found my problem.

I'll get it coded up tomorrow and report back. :slight_smile:

It always looks so simple once it's sorted out. :slight_smile:

const int LED = 13;
const int MainAuto = 51;
const int MainManual =50;
const int stepsPerRevolution = 200;
const int IRSensor = 53;
int j = 0;
int i = 0;
  
  #include <Stepper.h>

Stepper FeedStepper(stepsPerRevolution, 2, 3, 4, 5);
Stepper CutStepper(stepsPerRevolution, 8, 9, 10, 11);

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(MainAuto, INPUT_PULLUP);
  pinMode(MainManual, INPUT_PULLUP);
  pinMode(IRSensor,INPUT);
  digitalWrite(LED, LOW);
  FeedStepper.setSpeed(120);
  CutStepper.setSpeed(150);
  Serial.begin(9600);
}

//Main Program.
void loop() {
  if (digitalRead(MainAuto) == LOW){
  Auto();
  }
  else if (digitalRead(MainManual) == LOW){
  Manual();
  }
  else {
  digitalWrite(LED,LOW);
  j = 0;
  i = 0;
  }
}
//End Main Program


//Manual Control
void Manual() {
  if (j==0) HomeBlade();
  const int FeedButton = 49;
  const int CutButton = 48;
  pinMode(FeedButton, INPUT_PULLUP);
  pinMode(CutButton, INPUT_PULLUP);

  if (digitalRead(MainManual) == HIGH){
  return;
  }
  if (digitalRead(FeedButton) == LOW){
  digitalWrite(LED, HIGH);
  FeedStepper.step(10);
  }
  else {
  digitalWrite(LED,LOW);
  digitalWrite(2,LOW);
  digitalWrite(3,LOW);
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);
  }

  if (digitalRead(CutButton) == LOW){
  digitalWrite(LED, HIGH);
  CutStepper.step(200);
  delay(500);
  digitalWrite(LED,LOW);
  digitalWrite(8,LOW);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
  digitalWrite(11,LOW);
  }
}
//End ManualControl

//Run/Auto
void Auto() {
  if (j==0) HomeBlade();
  int NumberOfParts(int Q);
  int PartSize(int L);
  const int StockOut = 52;
  int L = 4;
  int Q = 3;
  pinMode(StockOut, INPUT_PULLUP);
  L = PartSize(L);
  Q = NumberOfParts(Q);
  while (i < Q) {
  if (digitalRead(StockOut) == HIGH)
  i = Q; 
  if (digitalRead(MainAuto) == HIGH)
  i = Q;
  
  FeedStepper.step(L);
  delay (50);
  CutStepper.step(200);
  delay (50);
  i++;}
  
  digitalWrite(LED,LOW);
  digitalWrite(2,LOW);
  digitalWrite(3,LOW);
  digitalWrite(4,LOW);
  digitalWrite(5,LOW);
  
  digitalWrite(8,LOW);
  digitalWrite(9,LOW);
  digitalWrite(10,LOW);
  digitalWrite(11,LOW);
  
  if (digitalRead(MainAuto) == HIGH){
  return;
  } 
}
//End Auto

//Home Cutter Blade.
void HomeBlade(){
    while(digitalRead(IRSensor) == LOW)  CutStepper.step(1);
    while(digitalRead(IRSensor) == HIGH) CutStepper.step(1);
    CutStepper.step(50);
    j=1;
    digitalWrite(8,LOW);
    digitalWrite(9,LOW);
    digitalWrite(10,LOW);
    digitalWrite(11,LOW);    
}
//End Home Cutter Blade

//Read rotary switch one to get length of part.
int PartSize(int L){
  int rs1p1 = 22;
  int rs1p2 = 23;
  int rs1p3 = 24;
  int rs1p4 = 25;
  int rs1p5 = 26;
  int rs1p6 = 27;
  int rs1p7 = 28;
  int rs1p8 = 29;
  int rs1p9 = 30;
  int rs1p10 = 31;
  int rs1p11 = 32;
  int rs1p12 = 33;
  pinMode(rs1p1, INPUT_PULLUP);
  pinMode(rs1p2, INPUT_PULLUP);
  pinMode(rs1p3, INPUT_PULLUP);
  pinMode(rs1p4, INPUT_PULLUP);
  pinMode(rs1p5, INPUT_PULLUP);
  pinMode(rs1p6, INPUT_PULLUP);
  pinMode(rs1p7, INPUT_PULLUP);
  pinMode(rs1p8, INPUT_PULLUP);
  pinMode(rs1p9, INPUT_PULLUP);
  pinMode(rs1p10, INPUT_PULLUP);
  pinMode(rs1p11, INPUT_PULLUP);
  pinMode(rs1p12, INPUT_PULLUP);
   
  if (digitalRead(rs1p1) == LOW){
  return 9;
  }
  else if (digitalRead(rs1p2) == LOW){
  return 11;
  }
  else if (digitalRead(rs1p3) == LOW){
  return 13;
  }
  else if (digitalRead(rs1p4) == LOW){
  return 15;
  }
  else if (digitalRead(rs1p5) == LOW){
  return 17;
  }
  else if (digitalRead(rs1p6) == LOW){
  return 20;
  }
  else if (digitalRead(rs1p7) == LOW){
  return 22;
  }
  else if (digitalRead(rs1p8) == LOW){
  return 24;
  }
  else if (digitalRead(rs1p9) == LOW){
  return 26;
  }
  else if (digitalRead(rs1p10) == LOW){
  return 28;
  }
  else if (digitalRead(rs1p11) == LOW){
  return 30;
  }
  else if (digitalRead(rs1p12) == LOW){
  return 32;
  }
//End of read rotory switch one.
}
//Read rotary switch two to get number of parts.
int NumberOfParts(int Q){
  int rs2p1 = 34;
  int rs2p2 = 35;
  int rs2p3 = 36;
  int rs2p4 = 37;
  int rs2p5 = 38;
  int rs2p6 = 39;
  int rs2p7 = 40;
  int rs2p8 = 41;
  int rs2p9 = 42;
  int rs2p10 = 43;
  int rs2p11 = 44;
  int rs2p12 = 45;
  pinMode(rs2p1, INPUT_PULLUP);
  pinMode(rs2p2, INPUT_PULLUP);
  pinMode(rs2p3, INPUT_PULLUP);
  pinMode(rs2p4, INPUT_PULLUP);
  pinMode(rs2p5, INPUT_PULLUP);
  pinMode(rs2p6, INPUT_PULLUP);
  pinMode(rs2p7, INPUT_PULLUP);
  pinMode(rs2p8, INPUT_PULLUP);
  pinMode(rs2p9, INPUT_PULLUP);
  pinMode(rs2p10, INPUT_PULLUP);
  pinMode(rs2p11, INPUT_PULLUP);
  pinMode(rs2p12, INPUT_PULLUP);
   
  if (digitalRead(rs2p1) == LOW){
  return 10;
  }
  else if (digitalRead(rs2p2) == LOW){
  return 50;
  }
  else if (digitalRead(rs2p3) == LOW){
  return 100;
  }
  else if (digitalRead(rs2p4) == LOW){
  return 200;
  }
  else if (digitalRead(rs2p5) == LOW){
  return 300;
  }
  else if (digitalRead(rs2p6) == LOW){
  return 400;
  }
  else if (digitalRead(rs2p7) == LOW){
  return 500;
  }
  else if (digitalRead(rs2p8) == LOW){
  return 1000;
  }
  else if (digitalRead(rs2p9) == LOW){
  return 2000;
  }
  else if (digitalRead(rs2p10) == LOW){
  return 3000;
  }
  else if (digitalRead(rs2p11) == LOW){
  return 5000;
  }
  else if (digitalRead(rs2p12) == LOW){
  return 10000;
  }
//End of read rotory switch two.
}

A big thank you to wildbill for the help!