Hi every one, I'm very green to Arduino and coding, been a bit frustrated trying to get the code to do what I want, gone through a lot of examples to get a slight idea on how to write this , basically I want the code to turn the motor CW then stop then CCW with one single button, I've tried the accelstepper library and stepper.h and went back to the normal Arduino code because I found it easier to understand, here is the attached code
const int buttonPin = 2;
int buttonState = 0;
int onoroffState = 0;
int Speed = 500; // how fast the motor will "step"
int Distance = 0; // how far traveled
int NumSteps = 5000; // steps
void setup() {
pinMode(2, INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
void loop(){
buttonState = digitalRead(2);
if (buttonState == HIGH){
onoroffState = 1;}
while (onoroffState == 1){
digitalWrite(9, HIGH);
delayMicroseconds(Speed);
digitalWrite(9, LOW);
delayMicroseconds(Speed);
Distance = Distance +1;
if (Distance == NumSteps)
onoroffState = 0;
}
if (digitalRead(8) == LOW){
digitalWrite(8, HIGH);
}
else
{
digitalWrite(8,LOW);
}
Distance = 0;
delay(5);
}
its wired up all ok with the button getting pulled to ground with a 10k , I'm not to sure if I've used the {} in the right way as I've said I'm really new to this and don't understand a lot, so the code works but at the end when it runs though it all and I press the button some times it runs CW then CW again then maybe CCW I cant predict what its going to do some times it does as I want other times it does not. thanks
Your main problem is that you are switching directions every time through loop() even when the button has not been pressed. Change the sketch to reverse direction only when you reach the end of a movement.
Here is how I would write it. Compare this to your original sketch to get some style hints and shortcuts.
const int buttonPin = 2;
const int directionPin = 8;
const int stepPin = 9;
const int NumSteps = 5000; // steps
const int Speed = 500; // how fast the motor will "step"
boolean running = false;
int Distance = 0; // how far traveled
void setup() {
pinMode(buttonPin, INPUT);
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
digitalWrite(directionPin, LOW);
digitalWrite(stepPin, LOW);
}
void loop() {
if (digitalRead(buttonPin)) {
running = true;
}
// Move in one direction
while (running) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(Speed);
digitalWrite(stepPin, LOW);
delayMicroseconds(Speed);
Distance++;
if (Distance == NumSteps) {
running = false;
// Reverse direction
digitalWrite(directionPin, !digitalRead(directionPin));
// Start over at 0
Distance = 0;
}
}
delay(5);
}
Hi johnwasser , thanks for your quick reply this has saved me many hours of frustration , I take it you have many years programming experience , is there some basic tutorials you could recommend that I can do to get better with coding and understanding it because I still struggle with how it all works , thanks mate
Hi , I used the last sketch gived by JohnWASSER for a Nema 23 motor and M542T controller and all work fine , but ... when I reduce the "SPEED" variable ( less than 200 ) to increase the motor speed , the motor stop turning and only make a noise .
As the M542T datasheet give max 300Khz as Pulse input frequency , I don't understand why the motor stop as 300Khz equal +/- 3 microseconds ... much more less than 200 microseconds ...
Martip00:
As the M542T datasheet give max 300Khz as Pulse input frequency , I don't understand why the motor stop as 300Khz equal +/- 3 microseconds ... much more less than 200 microseconds ...
There is a huge difference between the step rate that a driver can produce and the step rate that a motor is capable of. Very high driver step rates are usually used with high levels of micro-stepping and do not necessarily mean high motor speeds.
Also, if you want high motor speeds you must accelerate the motor from a lower speed - as with any type of motor. You might like to try this simple acceleration code
Finally, the speed at which a stepper motor can move is also influenced by the supply voltage - higher voltages facilitate higher speeds.
I've try to decrease the delay 10 unit by 10 unit to simulate the acceleration and in fact , I may decrease the delay to 100 µs instead of 200 µs ... ( 24V DC 400µ steps )
I've also modify the µstep ( 12800 insted of 400 ) and with this , I can suppress the delay ... the motor is turning .
Right now I make all tests with a 24V 10A as I only have this . I'm waiting for a 48V DC supply.
Is there a "formula" to find the best speed/torque ? or all must be find "on the run" .
Can you also explain the impact of the current table on the motor ?
My project : I've a milling lathe without automatic feed ... and I would like to place the NEMA to have one .
Martip00:
Is there a "formula" to find the best speed/torque ? or all must be find "on the run" .
You need to experiment
Can you also explain the impact of the current table on the motor ?
Are you referring to the range of current limits that the driver can be set to? If so, you should match the driver to the current permitted for your motor to protect the motor from over-current. If you select a lower current you will not get the full torque from the motor.