Serial communication

Hello I have a problem. The problem is as follows. I am currently using an Arduino mini with the easy motor driver to get the stepper motor to work. It did work. Now I want to be able to control the direction of the stepper motor by keying in the direction (+ = clockwise) and (- = counter clockwise) followed by a value of how many steps to take. I am not able to figure this out as I am really poor with coding. Please help me some body.

which easy motor driver are you talking about? do you have a link?

Yes Thank you so much for responding.
here is the easy driver link

This is the tutorial I used to check if my motor was working fine.

http://danthompsonsblog.blogspot.com/2008/09/easydriver-v31-tutorial.html

the code is right on the website if you scroll a bit down

You could try something like this:

// code for receiving a step value followed '+' or '-'

int dirPin = 3;
int stepperPin = 12;

void setup() {
  pinMode(dirPin, OUTPUT);
  pinMode(stepperPin, OUTPUT);
  Serial.begin(9600);
}

void step(boolean dir,int steps){
  digitalWrite(dirPin,dir);
  delay(50);
  for(int i=0;i<steps;i++){
    digitalWrite(stepperPin, HIGH);
    delayMicroseconds(100);
    digitalWrite(stepperPin, LOW);
    delayMicroseconds(100);
  }
}

void loop()
{
  serviceSerial();
}

// serviceSerial receives data in the form:
// "100+"  - rotates the motor forward 100 steps
// "200-"  - rotates the motor backwards 200 steps

void serviceSerial()
{
  static int val = 0;

  if ( Serial.available()) {
    char ch = Serial.read();

    if(ch >= '0' && ch <= '9')              // is ch a number?  
      val = val * 10 + ch - '0';            // yes, accumulate the value
    else if(ch == '+'){
      step(true, val);
      val = 0;
    }   
    else if(ch == '-'){
      step(false, val);
      val = 0;
    }
  }
}

I have no experience with that stepper board and have not run the code but I hope that gets you going in the right direction

Have fun!

Hi, I have updated the example code on my blog with comments (in green) now. Hopefully this will help you figure out what you need to change in the code to get the results you are after.

try this link again http://danthompsonsblog.blogspot.com/2008/09/easydriver-v31-tutorial.html

Good Luck!

Hi Dan, I don't know if you can fix the formatting, but although the code cuts and pastes ok from firefox, it all ends up on a single line if Internet Explorer is used.

Thanks mem,

I was not aware of this till now. I use a blogger widget to do the formatting and firefox to view it. Just checked it in safari and explorer and yep your right, no scroll bar at the bottom and and the line does not continue when it hits the edge of the frame, instead it just goes to the next line. Bugger!

Not sure of a way around this. But I will look into it. Thanks for the heads up and thanks for that awesome serial example!

Dan.

Dan, the Sparkfun thread has code that has a 50ms delay between direction changes, your example uses 100ms. How did you choose this value.

its a simple css issue. for a pre element to properly render in IE, you have to explicitly state its width property and change overflow to scroll. More information here: http://www.w3schools.com/css/pr_pos_overflow.asp

Thank you SOOOooo muchhh the code worked. I just had a few questions. after I key in say "100+" I have to change my baud rate from 19200 to 9600 to make the motor move . Do you know why this happens. Thank you.

srini, Dans' code and my post use 9600, if you want to use 19200 then you can change the baud rate in setup.

BTW, if you add the two lines below to the beginning of step function you can get an echo back of the commands

in the beginning of the step function, add these lines

  Serial.print(steps,DEC);
  Serial.println(dir ? '+' : '-' ); // print the direction

just before the line: digitalWrite(dirPin,dir);