Programim Tb6600 + stepper motor Crash Sensors (end and start course).

Hello people!

I have little knowledge of Arduino and Programming.

I’m trying to make a motor to move only when a push button is pressed. Move clockwise until it touches crash sensor (END).

When trigger this crash sensor, it should move in the opposite direction, until it touches the other cras sensor (HOME) and stand still until the button is pressed again.

By the time I managed to get the engine to move and change direction when it touches the end sensor, but as soon as he is released he already back to rotate clockwise.

My equipment …
01 Arduino Uno
01 TB6600
01 Stepper motor 19kgf cm (8 Leads) - Series connection)
02 Crash Sensor
01 Power Source 24v 5A - 120W.
01 Push buttom

My code …

//Arduino Sketch based on easy driver
int dirpin = 8;
int steppin = 9;
int enable = 7;

int sensorFinal = 3;
int sensorInicial = 5;

void setup()
pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
digitalWrite(enable, LOW);

pinMode(sensorFinal, INPUT);
pinMode(sensorInicial, INPUT);

void loop()
 int i;
 int sensor = digitalRead(sensorFinal);
if (sensor == HIGH) {
 digitalWrite(dirpin, HIGH); // Set the direction.

   digitalWrite(dirpin, LOW); // Set the direction.
 for (i = 0; i<1; i++) // Iterate for 4000 microsteps.
 digitalWrite(steppin, LOW); // This LOW to HIGH change is what creates the "Rising Edge" so the easydriver knows to when to step.
 digitalWrite(steppin, HIGH);
 delayMicroseconds(200); // This delay time is close to top speed for this
 } // particular motor. Any faster the motor stalls.

Thank you for attention.

I suspect you should not have the ELSE part in this code

if (sensor == HIGH) {
 digitalWrite(dirpin, HIGH); // Set the direction.

   digitalWrite(dirpin, LOW); // Set the direction.

As soon as it clears the sensor the direction changes - which is not what you want.

I think that setting the dirpin LOW should only be done by the other sensor.

Put some Serial.print()s into your code so you can see what is happening.

As a somewhat separate issue I see this comment "// Iterate for 4000 microsteps." At the moment you are just doing 1 step at a time (which is why the ELSE clause is biting you). But if you really do use a FOR loop to iterate for 4000 steps you will not be able to detect the crash sensor. Doing the steps one at a time is the correct way.