I am programming an automated table saw using a stepper motor and two NPN NO inductive sensors. Originally I had used E18-D80NK (IR) sensors and had the code working. Then I bought LJ12A3-4-Z/BX (NPN NO) inductive sensors to work better in a dusty environment. These sensors only require a 5v supply voltage so I wired them directly to 5V and GND on the Arduino Mega I am using. I have tried powering the board with the 9V "barrel jack" and the USB jack. Below is the code I am using. Right now I can get the LEDs on the sensors to respond while the Arduino is powered, but when I press the cycle start button the stepper will run and the sensors do not respond (no LED action). My best guess is that I need a larger power supply and a protected circuit to not damage the Arduino, but I was hoping to avoid that by using 5v sensors. Any help with code errors or troubleshooting methods would be greatly appreciated!
#include <AccelStepper.h>
#include <Bounce2.h>
#define STEPPER_WIRE_A 11 //first stepper pin
#define STEPPER_WIRE_B 10 //second stepper pin
#define CYCLESTART 7 //cycle start button digital pin
#define END_PROX 2 //end prox sensor digital pin
#define HOME_PROX 1 //start+home prox sensor digital pin
#define SAW_POWER 15 //saw start digital pin
#define jogSpeed 30000
#define jogAccel 5000
AccelStepper stepper(AccelStepper::FULL2WIRE, STEPPER_WIRE_A, STEPPER_WIRE_B); //Instantiate 2 wire stepper object
Bounce cycleStart = Bounce();
Bounce endProx = Bounce();
Bounce homeProx = Bounce();
int cStartStatus = HIGH;
int eProxStatus = HIGH;
int hProxStatus = HIGH;
void setup() {
pinMode(CYCLESTART, INPUT_PULLUP);
cycleStart.attach(CYCLESTART);
cycleStart.interval(50);
pinMode(END_PROX, INPUT_PULLUP);
endProx.attach(END_PROX);
endProx.interval(50);
pinMode(HOME_PROX, INPUT_PULLUP);
homeProx.attach(HOME_PROX);
homeProx.interval(50);
stepper.setMaxSpeed(jogSpeed);
stepper.setAcceleration(jogAccel);
Serial.begin(9600);
delay(500);
}
void loop() {
cycleStart.update();
cStartStatus = cycleStart.read();
endProx.update();
eProxStatus = endProx.read();
homeProx.update();
hProxStatus = homeProx.read();
if (cStartStatus == LOW) {
digitalWrite(SAW_POWER, HIGH);
delay(500);
while (eProxStatus == HIGH) {
endProx.update();
eProxStatus = endProx.read();
stepper.setSpeed(-jogSpeed);
stepper.run();
}
stepper.stop();
}
while (hProxStatus == HIGH) {
homeProx.update();
hProxStatus = homeProx.read();
stepper.setSpeed(jogSpeed);
stepper.run();
}
digitalWrite(SAW_POWER, LOW);
stepper.stop();
}