steppermotor zeroposition with ir sensor.

Hello,

I am writing a program for my school project. I build a device with 2 steppermotors. steppermotor 1 turns an object. steppermotor 2 is driving a spindle.

I have the following code. The code is working now but we need to include a IR sensor on one side of the spindel. every time i push the button, the program must check if the spindel stays on the "zero" position before the program starts. if it is not on the zero position, the spindel needs to move until it reaches the zero position and after that it has to start the program.

#define DIR_PIN 2 //2 + poort op arduino
#define STEP_PIN 3 //3 = poort op arduino
#define DIR_PIN1 4
#define STEP_PIN1 5
const int BUTTON = 8;
int buttonState = 0;

void setup() {
Serial.begin(9600);
digitalWrite(BUTTON, HIGH);

pinMode(BUTTON, INPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN1, OUTPUT);
pinMode(STEP_PIN1, OUTPUT);
}

void loop(){
buttonState = digitalRead(BUTTON);

if(buttonState == HIGH)
{
//// //rotate a specific number of degrees
// rotateDeg(3, .1);
// delay(1000);
//
// rotateDeg(-3, .1); //reverse
// delay(1000);

//rotate a specific number of microsteps (8 microsteps per step)
//a 200 step stepper would take 1600 micro steps for one full revolution
rotate1(10000, .05); // 10000 aantal micro stappen 1/32 , 0.05 snelheid
delay(2000);

rotate2(18000, .2);
delay(2000);

rotate1(10000, .05);
delay(10000);

rotate2(-18000, .2);
}
else { }

}

void rotate2(int steps, float speed){
//rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
//speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
int dir = (steps > 0)? HIGH:LOW;
steps = abs(steps);

digitalWrite(DIR_PIN1,dir);

float usDelay = (1/speed) * 150; //* groter getal is langzamer

for(int i=0; i < steps; i++){
digitalWrite(STEP_PIN1, HIGH);
delayMicroseconds(usDelay);

digitalWrite(STEP_PIN1, LOW);
delayMicroseconds(usDelay);
}
}

void rotate1(int steps, float speed){
//rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
//speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
int dir = (steps > 0)? HIGH:LOW;
steps = abs(steps);

digitalWrite(DIR_PIN,dir);

float usDelay = (1/speed) * 70;

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

digitalWrite(STEP_PIN, LOW);
delayMicroseconds(usDelay);
}
}

thanks in advance

If you get rid of those delay() calls you can turn the stepper and check the sensor at the same time, or so close to it, much less than a millisecond, as makes no real difference.

You have steppers, button and want to add a sensor or is the sensor the button?

At the bottom of my post are 3 links to Nick Gammon blogs. Check out the first one.
Learn that and you may end up with the least halty-lame project in your class.

Get the IR sensor and see if a red led can trigger it. It should as last time I checked red leds put out a good bit of near-IR. Why I suggest a red led is you can tell when it's lit by just looking.

thanks for your answer.

we would like to add a sensor which detects if the nut (on the spindel) is in the start position.

example:

if the power fails when program is running and the nut on the spindle is in the middle, in the next loop the nut wil get stuck at the top of the spindle because it wasn't in the zero position. We want make sure that the nut on the spindle is on the zero position when the loop start. We want to use an IR sensor which detects the loop. Maybe is there an other solution?

Printers do that all the time.

Put your zero the nut code in setup() and have it run to bring the nut down if the IR is not blocked at start. Any time power is cycled, setup() runs first.

Thank you,

Our question is, How do we insert this in our code?

In setup() after the pin modes it would look something like this

while ( IR is detected )
{
// turn the stepper 1 step in the direction to bring the nut down to zero
// delay( how long it takes to turn 1 step. I don't know, maybe 10 is enough )
}

while ( IR is detected ) may look like
while ( digitalRead( IRpin ) == HIGH )

That puts the read and the evaluation in the same expression, makes the whole thing cleaner.

Really though, this is a school assignment so you need to be working things out to better learn.
What course is this for?

do you have a picture of your spindle. I can't visualise what you mean by this nut.