v2 code.txt

Upload: wolverinep

Post on 04-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 V2 CODE.txt

    1/8

    //VERY VERY VERY VERY IMPORTANT!!!!!//in order to use this code for serial communications and motor control you mustsend the correct data sequence//at 115200 bps, it is formated as.....1....how many characters are in the value(1 - 5).....direction (1-2)..... number of characters in the speed value (3 or4).....speed(100 - 999 lower is faster)......value (DEPENDENT ON THE FIRST NUMBER)//so if I wanted to do 3200 microsteps forward at regular speed i would send 4132003200 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), the next num tells it that the speed val is 3 digits, the next three numbers is the delay value for motorspeed, and the last 4 numbers are the value.//if I wanted to do 32000 microsteps backward at standard speed for example I would send 52320032000 over serial//if I wanted to do 400 microsteps forward at fast speed I would send 313150400//if I wanted to do 80 microsteps forward at SUPER slow speed I would send 214250080//and finally if I wanted to do 8 microsteps backward at standard speed I wouldsend 122008//also when the program subtracts 48 from the incoming serial value its becausethe 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 itmultiplys it by its value depending on if its tens of thousands thousands hundr

    eds 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 ERRORSWILL OCCUR BTW THIS IS ALMOST 41 FULL REVLOUTIONS//THE MOTOR I AM USING IS THE SPARKFUN STEPPER MOTOR AND THE DRIVER IS THE EASYDRIVER V3//1600 microsteps is equal to one revolution

    ////////////////////////////////////////////////////////////////////////////////

    ////////////////////////////////////////////////////////////////////

    //VERSION 2 CODE UPADATES//added support for speed control of the stepper motor by controling the delay value//the data is now formated as.......how many characters are in the value (1 - 5)......direction (1-2).....number of characters in the speed value (3 or 4).....speed(100 - 9999 lower is faster)......value (DEPENDENT ON THE FIRST NUMBER)//DO NOT SEND A SPEED VALUE larger or smaller than a three to four digit numberit must be a number between 100 and 9999//LOWERING the value makes it FASTER, INCREASING the value makes it SLOWER..............WARNING your motor may stall at values below 200, at full voltage I haverun it slightly below 150 so run it low at your own risk

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    unsigned int val; //pin for storing the valueint info; //variable that stores the latest serial bit from the serial portint dirPN; //variable that stores the direction that the stepper motor will spininint delayVal; //variable that stores the delay value which controls the speed at

  • 7/30/2019 V2 CODE.txt

    2/8

    which the motor stepsint i; //variable for the counter in the for loopsint dirpin = 3; //variable that holds the pin number of the direction pin for the easy driverint 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 controlpinMode(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 willexecute

    delay(25); //VERY IMPORTANT delay, it can be reduced but if it is to low thearduino will not have receieved all of the serial data so reduce it at your ownrisk

    info = Serial.read(); //reads the first serial bit//THIS IS VERY IMPORTANT READ THE BEGINNING INTOinfo = info - 48; //!!!!!!!!!! The arduino reads the ascii value so to conver

    t 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 backwarddirPN = dirPN - 48;

    info = Serial.read(); //gets the next character which tells the arduino howmany characters are in the next speed/delay value

    info = info - 48;

    if (info == 3) { //this code will execute if the third number is 3 whichmeans that the next speed/delay value is three digits

    info = Serial.read(); //this next portion of code gets the next THREE digits which store the speed or delay value of the motor

    info = info-48;

    delayVal = (info * 100);info = Serial.read();info = info-48;delayVal = delayVal + (info * 10);info = Serial.read();info = info-48;delayVal = delayVal + info;

    }

  • 7/30/2019 V2 CODE.txt

    3/8

  • 7/30/2019 V2 CODE.txt

    4/8

    info = Serial.read();info = info-48;delayVal = delayVal + (info * 100);info = Serial.read();info = info-48;delayVal = delayVal + (info * 10);info = Serial.read();info = info-48;delayVal = delayVal + info;

    }

    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 10sval = (info * 10);info = Serial.read(); //reads the second number then converts it and adds i

    t to the first number it does not need to be multiplied becauseinfo = info - 48 ; //the second number in a two digit number is a single

    digit 1 - 9 once added to the first number the originalval = 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 wit

    h 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 ofthe motor 1 is forward, 2 is backward

    dirPN = dirPN - 48;

    info = Serial.read();

    info = info - 48;if (info == 3) { //this code will execute if the third number is 3 which

    means that the next speed/delay value is three digits

    info = Serial.read(); //this next portion of code gets the next THREE digits which store the speed or delay value of the motor

    info = info-48;delayVal = (info * 100);info = Serial.read();info = info-48;delayVal = delayVal + (info * 10);info = Serial.read();

    info = info-48;delayVal = delayVal + info;

    }

    if (info == 4) { //this code will execute if the third number is 4 which

    means that the next speed/delay value is four digits

    info = Serial.read(); //this next portion of code gets the next FOUR digitswhich store the speed or delay value of the motor

  • 7/30/2019 V2 CODE.txt

    5/8

    info = info-48;delayVal = (info * 1000);info = Serial.read();info = info-48;delayVal = delayVal + (info * 100);info = Serial.read();info = info-48;delayVal = delayVal + (info * 10);info = Serial.read();info = info-48;delayVal = delayVal + info;

    }

    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 char

    acters long

    dirPN = Serial.read(); //gets the next character which is the direction ofthe motor 1 is forward, 2 is backward

    dirPN = dirPN - 48;

    info = Serial.read();info = info - 48;

    if (info == 3) { //this code will execute if the third number is 3 which

    means that the next speed/delay value is three digitsinfo = Serial.read(); //this next portion of code gets the next THREE digit

    s which store the speed or delay value of the motorinfo = info-48;delayVal = (info * 100);info = Serial.read();info = info-48;delayVal = delayVal + (info * 10);info = Serial.read();info = info-48;delayVal = delayVal + info;

    }

    if (info == 4) { //this code will execute if the third number is 4 whichmeans that the next speed/delay value is four digits

    info = Serial.read(); //this next portion of code gets the next FOUR digitswhich store the speed or delay value of the motor

    info = info-48;delayVal = (info * 1000);info = Serial.read();

  • 7/30/2019 V2 CODE.txt

    6/8

    info = info-48;delayVal = delayVal + (info * 100);info = Serial.read();info = info-48;delayVal = delayVal + (info * 10);info = Serial.read();info = info-48;delayVal = delayVal + info;

    }

    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 char

    acters long

    dirPN = Serial.read(); //gets the next character which is the direction ofthe motor 1 is forward, 2 is backward

    dirPN = dirPN - 48;

    info = Serial.read();info = info - 48;

    if (info == 3) { //this code will execute if the third number is 3 which

    means that the next speed/delay value is three digitsinfo = Serial.read(); //this next portion of code gets the next THREE digit

    s which store the speed or delay value of the motorinfo = info-48;delayVal = (info * 100);info = Serial.read();info = info-48;delayVal = delayVal + (info * 10);info = Serial.read();info = info-48;delayVal = delayVal + info;

    }

    if (info == 4) { //this code will execute if the third number is 4 whichmeans that the next speed/delay value is four digits

    info = Serial.read(); //this next portion of code gets the next FOUR digitswhich store the speed or delay value of the motor

    info = info-48;delayVal = (info * 1000);info = Serial.read();

  • 7/30/2019 V2 CODE.txt

    7/8

    info = info-48;delayVal = delayVal + (info * 100);info = Serial.read();info = info-48;delayVal = delayVal + (info * 10);info = Serial.read();info = info-48;delayVal = delayVal + info;

    }

    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 value1 means it goes forward

    digitalWrite(dirpin, HIGH);

    for (i = 0; i

  • 7/30/2019 V2 CODE.txt

    8/8

    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(delayVal); //this is where the speed control delay is, b

    y controlling this delay motor speed control can be obtained

    }

    }} //the serial loop ends and exits

    } //the main loop runs again and awaits serial data to repeat this insane cycleall over again.....