Hello!
I am new to using this forum so I will start with apologizing for any mistakes.
I am currently setting up a way to rotate a stepper motor using a sensor switch. I have code written that rotates the motor how I like, but when it comes to implementing the switch, I cannot seem to figure out where to connect the wires to get the two connected. I also have a sample code written that should record the switches motion but when I click the switch nothing happens.
If anyone could offer code to add that would make sure that when the button is pushed, the motor will rotate, and when it is lifted the motor stays still. Also, if anyone could offer pictures / an idea of how to connect the switch to the CNC shield it is much appreciated. As of now, the switch is plugged into the "Y+" pin on the shield, and the driver and motor are also plugged into the "Y" section of the shield.
Devices Used:
- Arduino Uno
- CNC Shield (V3)
- Driver
- Nema 17 (42mm) motor (1.8 degree)
- X-Stop switch (Normally used in 3D printers)
#include <Stepper.h>
#define EN 8
//Direction pin
#define X_DIR 5
#define Y_DIR 6
#define Z_DIR 7
//Step pin
#define X_STP 2
#define Y_STP 3
#define Z_STP 4
int endstop_y;
//DRV8825
int delayTime=30; //Delay between each pause (uS)
int stps=6400;// Steps to move
int endStop = 0;
int endstopPin = 10;
void step(boolean dir, byte dirPin, byte stepperPin, int steps)
{
digitalWrite(dirPin, dir);
delay(100);
for (int i = 0; i < steps; i++) {
digitalWrite(stepperPin, HIGH);
delayMicroseconds(delayTime);
digitalWrite(stepperPin, LOW);
delayMicroseconds(delayTime);
}
}
void setup(){
Serial.begin(9600);
pinMode(X_DIR, OUTPUT); pinMode(X_STP, OUTPUT);
pinMode(Y_DIR, OUTPUT); pinMode(Y_STP, OUTPUT);
pinMode(Z_DIR, OUTPUT); pinMode(Z_STP, OUTPUT);
pinMode(EN, OUTPUT);
digitalWrite(EN, LOW);
}
void loop()
{
endStop = digitalRead(endstopPin);
Serial.print("End Stop = ");
Serial.println(endStop);
delay(1000);
if(endStop == 1)
{
step(false, X_DIR, X_STP, stps); //X, Clockwise
step(false, Y_DIR, Y_STP, stps); //Y, Clockwise
step(false, Z_DIR, Z_STP, stps); //Z, Clockwise
delay(100);
endStop--;
}
}
}