Hi all,
This is my first post on the forum, but I've been reading a fair bit on here for a while. And I've been avoiding posting anything until I've gotten desperate, but I'm pretty much there now.
So here's the problem I'm having:
For context the project is a camera slider. Please find attached an image of the wiring diagram, and below is a copy of the full code used with as many comments as I could add to help with understanding the madness.
// libraries
#include <AccelStepper.h>
/*Control Buttons*/
//declare all push buttons to pins.
const int btn1 = 8; // moves motor in one direction - see case 1
const int btn2 = 9; // moves motor in opposite direction to case 1 - see case 2
const int btn3 = 10; // logs current position value to start position - see case 3
const int btn4 = 11; // logs current position to end position - see case 4
const int btn5 = 12; // activates continuous movement of motor between start and end position - see case 5 and "the simplest button switch" code
//create containers to track button state (low or high). Not constant so value can change at the push and release of a button.
int btn1State = 0;
int btn2State = 0;
int btn3State = 0;
int btn4State = 0;
int btn5State = 0; // combines with "the simplest push button switch later
// declare 'mode' to contain and trigger switch.case functions
int mode = 0;
/*Speed Control - Pot*/
//Speed Control
int pot = 0; // declaring the linear slider
long highSpeed; // used to hold the high speed value of the stepper motor
long lowSpeed; // used to hold the high speed value of the stepper motor
/**********Stepper Setup**********/
//declare all STEPPER motor wires to pins.
const int PUL = 2;
const int DIR = 3;
const int ENBL = 4;
AccelStepper STEPPER (1, PUL, DIR); // Defining Stepper Motor 1 - Driver, PUL - Pulse, DIR - Direction
long motorSpeed = 0; // stores the stepping frequency of the motor driven by the potentiometer
long CURRENTpos = 0; // keeps track of the current position of the slider
long STARTpos = 0; // used to log the desired start position with btn3
long ENDpos = 0; // used to log the desired end position with btn4
void setup() {
Serial.begin (9600); // starts up serial com to track code state
/*Control Buttons*/
//declare push buttons are inputs.
pinMode (btn1, INPUT);
pinMode (btn2, INPUT);
pinMode (btn3, INPUT);
pinMode (btn4, INPUT);
pinMode (btn5, INPUT);
/*Speed Control - Pot*/
// declaring A0 (pot) as an input
pinMode (A0, INPUT);
}
void loop() {
/*Pot Speed Control*/
highSpeed = 25600; // high speed value
lowSpeed = 2000; // low speed value
/*Stepper Setup*/
pot = analogRead (A0);
//Stepper settings refering back to pre-void setup data
motorSpeed = map(pot, 0, 1023, lowSpeed, highSpeed); // mapping pot to desired motor speeds
/* below was creating a "deadzone" for the potentiometer, but this was removed for simplicity. It did work well enough though.
// if (motorSpeed > (highSpeed * 0.99)) { // high speed deadzone
// motorSpeed = highSpeed ;
// }
// if (motorSpeed < (highSpeed * 0.03)) { // low speed deadzone
// motorSpeed = lowSpeed ;
// }
*/
STEPPER.setSpeed (motorSpeed); // initiating stepper speed
Serial.print ("Motor Speed = "); // keeping track of stepper speed
Serial.println (motorSpeed);
//preparing button pins to be monitored
btn1State = digitalRead (btn1);
btn2State = digitalRead (btn2);
btn3State = digitalRead (btn3);
btn4State = digitalRead (btn4);
//btn5State
if (digitalRead(btn5) == true) { // the simplest button switch. Allows btn5State to switch between HIGH and LOW at the push/toggle of a button.
btn5State = !btn5State;
digitalWrite (btn5, btn5State);
} while (digitalRead (btn5) == true);
Serial.print ("btn5State = ");
Serial.println (btn5State);
// checking the state of each button to determine what 'mode' should be.
if (btn1State == HIGH) {
mode = 1;
}
else if (btn2State == HIGH) {
mode = 2;
}
else if (btn3State == HIGH) {
mode = 3;
}
else if (btn4State == HIGH) {
mode = 4;
}
else if (btn5State == HIGH) {
mode = 5;
} else {
mode = 0;
}
Serial.print ("CURRENTpos = "); // keeping track of current position before a case is activated
Serial.println (CURRENTpos);
/**********Modes - functions**********/
// switch.case action, depending on 'mode' an action will execute
switch (mode) {
break;
case 0: // no instruction the motor to ensure there's no accidental movement. Not really necassary...
Serial.println ("CASE = 0");
break;
case 1:
// Serial.println ("CASE = 1"); // MANUAL CW rotation - moves the motor clockwise a set number of steps
STEPPER.setSpeed (motorSpeed);
STEPPER.setAcceleration (motorSpeed);
CURRENTpos = (CURRENTpos + 1000);
STEPPER.runToNewPosition (CURRENTpos);
STEPPER.runSpeed();
Serial.print ("Case 1 CURRENTpos = ");
Serial.println (CURRENTpos);
break;
case 2:
// Serial.println ("CASE = 2"); // MANUAL CCW rotation - moves the motor counter clockwise a set number of steps
STEPPER.setSpeed (motorSpeed);
STEPPER.setAcceleration (motorSpeed);
CURRENTpos = (CURRENTpos - 1000);
STEPPER.runToNewPosition (CURRENTpos);
STEPPER.runSpeed();
Serial.print ("Case 2 CURRENTpos = ");
Serial.println (CURRENTpos);
break;
case 3:
// Serial.println ("CASE = 3"); // MANUAL start position - logs the current position to start position
STARTpos = CURRENTpos;
Serial.print ("Case 3 STARTpos = ");
Serial.println (STARTpos);
STEPPER.run();
break;
case 4:
// Serial.println ("CASE = 4"); // MANUAL end position - logs the current position to end position
ENDpos = CURRENTpos;
Serial.print ("Case 4 ENDpos = ");
Serial.println (ENDpos);
STEPPER.run();
break;
case 5:
// Serial.println ("CASE = 5"); // Backwards and Forwards - moves the motor back and forward between the start and end postitions
Serial.print ("Case 5 STARTpos = ");
Serial.println (STARTpos);
STEPPER.setSpeed (motorSpeed);
STEPPER.setAcceleration (motorSpeed);
STEPPER.runToNewPosition (STARTpos);
STEPPER.run();
Serial.print ("Case 5 ENDpos = ");
Serial.println (ENDpos);
STEPPER.runToNewPosition (ENDpos);
STEPPER.run();
break;
}
// FIN
}
So onto the issue.
I've setup a NEMA 34 Stepper Motor (85BYGH450C-060) with a DQ86OMA Driver powered by a 60V PSU (connected only to the driver). I've connected the PUL, DIR, and ENBL pins to an Arduino UNO (and later an Elegoo Mega 2560 - budget was waning at this point). And there's a set of 5 push buttons with pull down resistors along with a button to hard reset the Arduino. A potentiometer is is also included to control the speed of the stepper. The UNO and MEGA have been powered through a USB cable to my PC.
This code was working flawlessly when everything was assembled on a breadboard. When a button was pressed the motor responded as expected. But after soldering all the circuits together on a prototyping PCB and linking all the components with longer wires to fit into the body of the slider, an intermittent problem has developed (not immediately however).
Initially after all the final wiring was completed the setup was working really well, as well as it did with the breadboard setup. But now only days later it only works 90% (or less) of the time. Everything in the serial monitor shows up as it used to, and everything triggers as expected in the monitor, but for some reason there's no response from the motor at all now.
see message below for further info - I ran out of characters
So the question is, does it sound I'm looking at a driver malfunction here, or is there something else wrong? It sounds to me like a hardware failure between the Driver and the Motor (my money is, and probably will be on the Driver), but I wanted to call on the experience of many before I throw more money at this problem.
I'd really appreciate any assistance, thanks for checking out this post, and sorry for the long and drawn out text!
![2019-07-09 20_20_23-Camera Slider Wiring.fzz_ - Fritzing - [Breadboard View].png](https://europe1.discourse-cdn.com/arduino/original/3X/a/5/a56ba76850773bfaea9b4b18312e314cf52ecabb.png)