Stepper motor sweetness...

Ok so for the last day or so I have been working non stop to get my stepper motor working with the easy driver and perfecting the code to get it to run. I got it to run without any problems in about an hour but then I started making serial control code. I have posted here many times asking how to send an integer over serial and have the arduino read the integer for controlling a servo (not a stepper) not the ascii value. So in this project I figured out how its done and made one hell of a control program. This code allows %100 percent control of the stepper motor down to a single microstep as well as direction. I have made a video of this code and the hardware in action but it won't be on youtube for at least 8 more hours (very high quality video). This code should help you guys if your looking for full serial control of your stepper motors.

//VERY VERY VERY VERY IMPORTANT!!!!!
//in order to use this code for serial communications and motor control you must send the correct data sequence
//at 115200 bps, it is formated as.....1....how many characters are in the value (1 - 5).......direction (1-2).........value (DEPENDENT ON THE FIRST NUMBER)
//so if I wanted to do 3200 microsteps forward i would send 413200 over serial the first number says there are 4 characters in the value (3200) the second number is 1 which
//tells the motor to go forward (2 would go backward), and the last 4 numbers are the value.
//if I wanted to do 32000 microsteps backward for example I would send 5232000 over serial
//if I wanted to do 400 microsteps forward I would send 31400
//if I wanted to do 80 microsteps forward I would send 2180
//and finally if I wanted to do 8 microsteps backward I would send 128
//also when the program subtracts 48 from the incoming serial value its because the arduino reads the ascii value of the number not its real value so by
//subtracting 48 it converts the ascii value back into its integer value than it multiplys it by its value depending on if its tens of thousands thousands hundreds ect...
//then it adds it up and it has the value that was originally sent from the computer
//make sure the correct data is sent or else the arduino will crash and will require a reset, error handling can be programmed to suit your needs
//THE MAXIMUM INTEGER VALUE IS 65,535 AND HAS BEEN TESTED IF YOU GO OVER ERRORS WILL OCCUR BTW THIS IS ALMOST 41 FULL REVLOUTIONS
//THE MOTOR I AM USING IS THE SPARKFUN STEPPER MOTOR AND THE DRIVER IS THE EASY DRIVER V3
//1600 microsteps is equal to one revolution 

unsigned int val; //pin for storing the value
int info; //variable that stores the latest serial bit from the serial port
int dirPN; //variable that stores the direction that the stepper motor will spin in
int i; //variable for the counter in the for loops
int dirpin = 3; //variable that holds the pin number of the direction pin for the easy driver
int steppin = 12; //variable that holds the pin number of the stepping pin for the easy driver
//you must connect the GND pin on the easy driver to the arduino GND as well

void setup(){
  
Serial.begin(115200); //opens a serial connection at 115200 bps

pinMode(dirpin, OUTPUT); //declares pin 3 as an output for direction control
pinMode(steppin, OUTPUT); //declares pin 12 as an output for stepping control

}

 void loop() {
   

   if (Serial.available() > 0) { //if serial data is sent this if statement will execute
     
   delay(25); //VERY IMPORTANT delay, it can be reduced but if it is to low the arduino will not have receieved all of the serial data so reduce it at your own risk
   
   info = Serial.read(); //reads the first serial bit
   //THIS IS VERY IMPORTANT READ THE BEGINNING INTO   
   info = info - 48; //!!!!!!!!!! The arduino reads the ascii value so to convert it to an integer value you subtract 48      
    
    if (info == 1) { //enters if info is 1 meaning that the value is only 1 character long
      
     dirPN = Serial.read(); //gets the next character which is the direction of the motor 1 is forward, 2 is backward
     dirPN = dirPN - 48;
     
     info = Serial.read(); //reads the single digit then converts it back to an integer and stores it in val for later use
     info = info - 48;
     val = info;
     
    }
    
    if (info == 2) { //enters if info is 2 meaning that the value is only 2 characters long
      
     dirPN = Serial.read(); //gets the next character which is the direction of the motor 1 is forward, 2 is backward
     dirPN = dirPN - 48;
     
     info = Serial.read(); //reads the first digit then converts it and multiples it by 10 because the first digit in a two digit number would
     info = info - 48    ; //be in the 10s
     val = (info * 10);
     info = Serial.read(); //reads the second number then converts it and adds it to the first number it does not need to be multiplied because
     info = info - 48    ; //the second number in a two digit number is a single digit 1 - 9 once added to the first number the original
     val = val + info;     //value that was sent is now in the arduino and will be used at the end of the loop
     
    }
    
    //the below if statments follow the same pattern as the above loops just with higher multiplying values because they are after all
    
    if (info == 3) { //enters if info is 3 meaning that the value is only 3 characters long
      
     dirPN = Serial.read(); //gets the next character which is the direction of the motor 1 is forward, 2 is backward
     dirPN = dirPN - 48; 
     
     info = Serial.read();
     info = info - 48    ; 
     val = (info * 100);
     info = Serial.read();
     info = info - 48     ;
     val = val + (info * 10);
     info = Serial.read();
     info = info - 48   ;  
     val = val + info;
      
    }
        
    if (info == 4) { //enters if info is 4 meaning that the value is only 4 characters long
      
     dirPN = Serial.read(); //gets the next character which is the direction of the motor 1 is forward, 2 is backward
     dirPN = dirPN - 48;
     
     info = Serial.read(); 
     info = info - 48;
     val = (info * 1000);
     info = Serial.read();
     info = info - 48;
     val = val + (info * 100);
     info = Serial.read();
     info = info - 48;
     val = val + (info * 10);
     info = Serial.read();
     info = info - 48;
     val = val + info;
      
    }
    
    if (info == 5) { //enters if info is 5 meaning that the value is only 5 characters long
      
     dirPN = Serial.read(); //gets the next character which is the direction of the motor 1 is forward, 2 is backward
     dirPN = dirPN - 48;
     
     info = Serial.read();
     info = info - 48;
     val = (info * 10000);
     info = Serial.read();
     info = info - 48;
     val = val + (info * 1000);
     info = Serial.read();
     info = info - 48;
     val = val + (info * 100);
     info = Serial.read();
     info = info - 48;
     val = val + (info * 10);
     info = Serial.read();
     info = info - 48;
     val = val + info;
     
    }
       
    if (dirPN == 1) { //if dirPN is 1 then this if statement executes, the value 1 means it goes forward
      
    digitalWrite(dirpin, HIGH); 
    
               for (i = 0; i<val; i++)    //this is the start of the loop and val the value we had to work so hard to get is the number of microsteps or in other words the number
                                          //of times it will loop (each time it loops the easy driver microsteps) so 1 loop is 1 microstep   
  {
    
    digitalWrite(steppin, LOW);  //toggles the stepping pin on and off then delays 200 microseconds
    digitalWrite(steppin, HIGH); 
    delayMicroseconds(200);     
    
  }       

    }
    
        if (dirPN == 2) { //if dirPN is 2 then this if statement executes, the value 2 means it goes backwards
      
    digitalWrite(dirpin, LOW); 
    
               for (i = 0; i<val; i++)    //this is the start of the loop and val the value we had to work so hard to get is the number of microsteps or in other words the number
  {                                       //of times it will loop (each time it loops the easy driver microsteps) so 1 loop is 1 microstep
    
    digitalWrite(steppin, LOW);  //toggles the stepping pin on and off then delays 200 microseconds
    digitalWrite(steppin, HIGH); 
    delayMicroseconds(200);     
    
  }       

    }
     
  } //the serial loop ends and exits
   
 } //the main loop runs again and awaits serial data to repeat this insane cycle all over again.....

I hope that this code can help you guys! DOES ANYONE KNOW WHERE I CAN PERMANENTLY POST THIS CODE??? I looked on arduino playground but could not figure out how to post code.

code.google.com

Here is the youtube link to the video!

This is excellent. Thanks for making this available. I have the Easy Driver so i'll give this a go some time.

I am uploading another video of the stepper motor in action but with the new v2 code it allows 100% control of the motor speed, position, and direction plus I beefed up the gui in visual studio to have way more features.

Here is the version 2 arduino code if anyone is interested!

I know I could have used sub routines and switch statements to reduce the size of the code but I kept it in blocks of code to prevent weird problems from occurring, I tried the switch case but it didn't work for some reason. There is a link to a text file so you can copy and paste easily as I edited this code to make it more readable and I could have made an error ect....

Download the text file here

http://code.google.com/p/stepperserialcontrol/downloads/list

I cannot post it here errrr max 9500 character limit!!! so you have to go to sparkfun to view it

http://forum.sparkfun.com/viewtopic.php?p=77326#77326

I am going to put it on google code soon then it will have a nice permanent home.