Stepper motor and serial monitor

Hi,

I am currently working on a third year university group project that involves a stepper motor, an arduino uno board and a big easy driver. The issue i am facing is that i need total control over the motor so i can change direction instantaneously. I have been looking at tutorials using the serial monitor, which can be found here (http://garagelab.com/profiles/blogs/tutorial-stepper-motor-easy-driver-arduino). I have set the motor up before this and it is working perfectly. However, using the code, it verifies and uploads fine, however the motor vibrates and does not spin. Entering 'z' or 'a' into the serial monitor does nothing either. Any help would be appreciated as i am very stuck and stressing out! If anyone has any other suggestions on how to instantly change the direction of the motor when i please i would be greateful.

int dir = LOW;
int stepp = LOW;
long previousMillis = 0;
long currentMillis = millis();
long steptime = 500;
int x = 0;
char c = 0;

void setup()
{
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
Serial.begin(9600);
}

void loop()
{
if(Serial.available()>0)
{
c=Serial.read();
if(c=='a')
{
dir = HIGH;
}
if(c=='z')
{
dir = LOW;
}
}
if(currentMillis - previousMillis > steptime)
{
previousMillis = currentMillis;
if(stepp==LOW)
{
stepp = HIGH;
}
else
{
stepp = LOW;
}
digitalWrite(2,dir);
digitalWrite(3,stepp);
}
}

The

I presume that you intend that currentMillis contains a value that's pretty close to the current value of millis(). Where does currentMillis get updated?

You are turning the step pin on for a relatively long time. The step pin should be toggled very briefly, to make the motor step. The pause between steps should be (much) longer.

Look at this simple stepper code - the second example uses millis().

The significant difference from your code is that it produces a HIGH-LOW pulse for each step whereas yours only produces HIGH then later LOW then later HIGH etc.

Get the motor to work properly ina fixed direction before introducing code for changing direction.

...R
Stepper Motor Basics

I have only started using arduino for the last two days, because one of the members of my group in charge of this element of the project unexpectedly dropped out university. Therfore i am a complete novice, and i don’t really understand any of the code, however i would like to but i am struggling for time. The code i have used for the motor to work in a fixed direction is as follows

void setup() {
pinMode(3, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(3, LOW);
digitalWrite(12, LOW);
}

void loop() {
digitalWrite(12, HIGH);
delay(1);
digitalWrite(12, LOW);
delay(1);
}

I also have a code to change the direction which is shown below

int dirpin = 3;
int steppin = 12;

void setup() {
Serial.begin(9600);

pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
}
void loop()
{

int i;

digitalWrite(dirpin, LOW); // Set the direction.
delay(100);

Serial.println(">>");
for (i = 0; i<4000; i++) // Iterate for 4000 microsteps.
{
digitalWrite(steppin, LOW); // This LOW to HIGH change is what creates the
digitalWrite(steppin, HIGH); // “Rising Edge” so the easydriver knows to when to step.
delayMicroseconds(200); // This delay time is close to top speed for this
} // particular motor. Any faster the motor stalls.

digitalWrite(dirpin, HIGH); // Change direction.
delay(100);

Serial.println("<<");
for (i = 0; i<4000; i++) // Iterate for 4000 microsteps
{
digitalWrite(steppin, LOW); // This LOW to HIGH change is what creates the
digitalWrite(steppin, HIGH); // “Rising Edge” so the easydriver knows to when to step.
delayMicroseconds(200); // This delay time is close to top speed for this
} // particular motor. Any faster the motor stalls.

}

This changes the direction after 4000 microsteps and then it spins in the opposite direction for 4000 microsteps.

I do realize the forums are here so i can learn to better understand my problem myself, but time is of the essence and i don’t think ill be able to learn how produce a code to use the serial monitor in time.

I do realize it is a lot to ask, but could you please show how i can edit any of the codes above, or the original code posted so i can use the serial monitor, typing z in to go one direction, and a to go in the opposite.

Anyway, thanks for taking the time to reply i do appreciate it.

Cheers, Robbie.

Robin2:
Look at this simple stepper code - the second example uses millis().

The significant difference from your code is that it produces a HIGH-LOW pulse for each step whereas yours only produces HIGH then later LOW then later HIGH etc.

Get the motor to work properly ina fixed direction before introducing code for changing direction.

…R
Stepper Motor Basics

I have only started using arduino for the last two days, because one of the members of my group in charge of this element of the project unexpectedly dropped out university. Therfore i am a complete novice, and i don’t really understand any of the code, however i would like to but i am struggling for time. The code i have used for the motor to work in a fixed direction is as follows

void setup() {
pinMode(3, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(3, LOW);
digitalWrite(12, LOW);
}

void loop() {
digitalWrite(12, HIGH);
delay(1);
digitalWrite(12, LOW);
delay(1);
}

I also have a code to change the direction which is shown below

int dirpin = 3;
int steppin = 12;

void setup() {
Serial.begin(9600);

pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
}
void loop()
{

int i;

digitalWrite(dirpin, LOW); // Set the direction.
delay(100);

Serial.println(">>");
for (i = 0; i<4000; i++) // Iterate for 4000 microsteps.
{
digitalWrite(steppin, LOW); // This LOW to HIGH change is what creates the
digitalWrite(steppin, HIGH); // “Rising Edge” so the easydriver knows to when to step.
delayMicroseconds(200); // This delay time is close to top speed for this
} // particular motor. Any faster the motor stalls.

digitalWrite(dirpin, HIGH); // Change direction.
delay(100);

Serial.println("<<");
for (i = 0; i<4000; i++) // Iterate for 4000 microsteps
{
digitalWrite(steppin, LOW); // This LOW to HIGH change is what creates the
digitalWrite(steppin, HIGH); // “Rising Edge” so the easydriver knows to when to step.
delayMicroseconds(200); // This delay time is close to top speed for this
} // particular motor. Any faster the motor stalls.

}

This changes the direction after 4000 microsteps and then it spins in the opposite direction for 4000 microsteps.

I do realize the forums are here so i can learn to better understand my problem myself, but time is of the essence and i don’t think ill be able to learn how produce a code to use the serial monitor in time.

I do realize it is a lot to ask, but could you please show how i can edit any of the codes above, or the original code posted so i can use the serial monitor, typing z in to go one direction, and a to go in the opposite.

Anyway, thanks for taking the time to reply i do appreciate it.

Cheers, Robbie.

raaaaabmcc:
I do realize it is a lot to ask, but could you please show how i can edit any of the codes above

Have you studied the simple stepper code I linked to earlier. Use that as a starting point. I know it works.

When you have that working we can move forward.

Also read through stepper motor basics

...R

Robin2:
Have you studied the simple stepper code I linked to earlier. Use that as a starting point. I know it works.

When you have that working we can move forward.

Also read through stepper motor basics

...R

I studied it, attempted to use it and it wouldn't work, same thing happened with original code. i must not be setting it up properly as i have no idea where to wire pins 10 and 11 and i dont have an led for pin 13.

raaaaabmcc:
I studied it, attempted to use it and it wouldn’t work, same thing happened with original code. i must not be setting it up properly as i have no idea where to wire pins 10 and 11 and i dont have an led for pin 13.

Why are you using those pins at all? I don’t think ther are part of my code?

Try to get my code working without any changes to it.

…R

I'm not willing to do students' academic assignments for them, no matter how good their stories are. I'm willing to work with you on this, and I think that others are, too, but you'll have to do most of the work. So far, it looks like you've copied other people's code, and told us what they say it does.

Here's a resource that can help you understand the code: http://arduino.cc/en/Reference/HomePage. You might also want to consult your professor, or the other members of your group. I'd be surprised if none of them knew how to program in C++, and were in a technical curriculum.

Hi,

Can you please post a copy of your sketch, using code tags?
Please use code tags.. See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

It makes it easier to read, and stops the editor from misinterpreting c++ content.

Thanks Tom.... :slight_smile: