Hello.
I have created a project that uses an easy driver, a pro micro and a simple power supply.
I have some experience in programming, mostly basic and c#, and a fair amount in electronics.
I used the wonderful demo for the ED as a framework to create a program that uses 2 switches to control a stepper and turns it at about 5.4mS per pulse. I know the electronics is OK because with the ED demo it works perfectly. Oh sorry, I use the internal pullups of the 32u4 for the switches, and they are connected to the star point of the circuit.
At least that's what I thought I had created...after some minor bug hunting I got a clean compile and uploaded it. Uploaded effortlessly... and just sits there. No action when I press a button, no movement when it should be running normally. I have already tried changing the delay times for the step commands, but as the ED has a fmax of 500kHz I didn't think that was the issue.
The ED and stepper motor are both getting very warm over the space of 30 odd seconds, but like I said with the demo running it works like a charm...so it's not a short.
If I had to guess I'd wonder if it's some kind of 'race' condition, trying to jump between forwards and backwards really quickly perhaps, but I can't figure out why it's happening. I'd be grateful for any advice, or recommendations...it's probably staring me in the face.
I've removed the preamble from the top of the source, but will acknowledge authors as necessary in the final build.
#define stp 2 // ed connection
#define dir 3 // ed connection
#define MS1 4 // ed connection
#define MS2 5 // ed connection
#define EN 6 // ed connection
#define pk 7 // "park" switch, stops the mount when fully rewound.
#define Rst 8 // "Reset" switch, allows the user to send the mount back to the start of its travel.
//Declare variables for functions
int x;
void setup() {
pinMode(stp, OUTPUT); // All these ports are outputs...
pinMode(dir, OUTPUT);
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
pinMode(EN, OUTPUT); // ...Down to this one.
pinMode(pk, INPUT_PULLUP); // set pin 7 as input, use internal pullup resistor so only switch needed to ground.
pinMode(Rst, INPUT_PULLUP); // set pin 8 as input, use internal pullup resistor so only switch needed to ground.
resetEDPins(); //Set step, direction, microstep and enable pins to default states
Serial.begin(9600); //Open Serial connection for debugging. May remove this once functional.
}
//Main loop
void loop() {
int Park = digitalRead(7);
int Reset = digitalRead(8);
digitalWrite(EN, LOW); //Pull enable pin low to allow motor control
if (Park == LOW && Reset == HIGH) // If parked, start tracking
{
StepForwardDefault();
}
else if (Park == HIGH && Reset == LOW) // If Reset pushed, rewind
{
delay (500); // half second pause. Debounce coding is so last year.
ReverseStepFast();
}
else if (Park == HIGH && Reset == HIGH) // normal running.
{
StepForwardDefault();
}
else if (Park == LOW && Reset == LOW) // Unusual condition
{
Serial.println("I'm already Reset and Parked.");
}
else // Nothing seems to fit, error message...
{
Serial.println("Something Is Wrong.");
}
resetEDPins();
}
//Reset Easy Driver pins to default states
void resetEDPins()
{
digitalWrite(stp, LOW);
digitalWrite(dir, LOW);
digitalWrite(MS1, LOW);
digitalWrite(MS2, LOW);
digitalWrite(EN, HIGH);
}
//Forward function
void StepForwardDefault()
{
int Reset = digitalRead(8);
if (Reset == HIGH)
{
digitalWrite(dir, LOW); //Pull direction pin low to move "forward"
for ( x = 0; x <= 4000000; x++) // count for 6 hours.
delayMicroseconds(3400); // Takes one step every 5.4mS to work. Your results will vary depending on the stepper motor.
digitalWrite(stp, HIGH); //Trigger one step forward
delay(1); // taken as 1000uS for timing purposes.
digitalWrite(stp, LOW); //Pull step pin low so it can be triggered again
delay(1); // taken as 1000uS for timing purposes.
}
else if (Reset == LOW)
{
delay (500); // Half second pause. Debounce coding is so last year.
ReverseStepFast();
}
}
//Reverse function
void ReverseStepFast()
{
int Park = digitalRead(7);
digitalWrite(dir, HIGH); //Pull direction pin high to move in "reverse"
if (Park == HIGH) //Loop until parked and ready to start again
{
digitalWrite(stp, HIGH); //Trigger one step
delay (1); // Stepper controller can function @ 500kHz, no delay needed.
digitalWrite(stp, LOW); //Pull step pin low so it can be triggered again
delay (1);
}
else if (Park == LOW) // Parked, time to start again.
{
StepForwardDefault();
}
}