Brandon Ring / Mbed 2 deprecated Project_Stepper

Dependencies:   TextLCD mbed

Committer:
socdough
Date:
Thu Aug 18 15:04:19 2011 +0000
Revision:
0:506d7846938a
Child:
1:529333e22121
rev AB

Who changed what in which revision?

UserRevisionLine numberNew contents of line
socdough 0:506d7846938a 1 /*
socdough 0:506d7846938a 2 Project Stepper
socdough 0:506d7846938a 3 Written by Tony Cacace & Brandon Ring
socdough 0:506d7846938a 4 Date: 08-18-11
socdough 0:506d7846938a 5 Rev: AB
socdough 0:506d7846938a 6
socdough 0:506d7846938a 7 This program will use the mbed micro-controller to step a stepper motor forward and reverse
socdough 0:506d7846938a 8 based on the users input request.
socdough 0:506d7846938a 9 */
socdough 0:506d7846938a 10
socdough 0:506d7846938a 11 #include "mbed.h"
socdough 0:506d7846938a 12 #include "TextLCD.h"
socdough 0:506d7846938a 13
socdough 0:506d7846938a 14 #define DELAYSEQ 3.0 // A wait used between the main while loop
socdough 0:506d7846938a 15
socdough 0:506d7846938a 16 TextLCD lcd(p24, p26, p27, p28, p29, p30);
socdough 0:506d7846938a 17 DigitalOut enable1(p21);
socdough 0:506d7846938a 18 DigitalOut enable2(p22);
socdough 0:506d7846938a 19 DigitalOut STP1(p11);
socdough 0:506d7846938a 20 DigitalOut STP2(p12);
socdough 0:506d7846938a 21 DigitalOut STP3(p13);
socdough 0:506d7846938a 22 DigitalOut STP4(p14);
socdough 0:506d7846938a 23 Serial pc(USBTX, USBRX); // tx, rx
socdough 0:506d7846938a 24
socdough 0:506d7846938a 25 void moveSteps (int); // Fuction prototype: used to define how many steps to take
socdough 0:506d7846938a 26 volatile int count = 1; //keeps track of counted steps 1-4
socdough 0:506d7846938a 27 float delayStep = 0.5; //Speed of motor.....0.005
socdough 0:506d7846938a 28
socdough 0:506d7846938a 29
socdough 0:506d7846938a 30
socdough 0:506d7846938a 31 int main() {
socdough 0:506d7846938a 32 enable1 = 1; //Turn on driver
socdough 0:506d7846938a 33 enable2 = 1; //Turn on driver
socdough 0:506d7846938a 34 // int inputdir; //Variable used to define step direction.
socdough 0:506d7846938a 35 int inputsteps; //Variable used to define step amount.
socdough 0:506d7846938a 36
socdough 0:506d7846938a 37 do { //Print menu on PC to request what to do from the user.
socdough 0:506d7846938a 38 pc.printf("Menu\n\n");
socdough 0:506d7846938a 39 pc.printf("1. Home Stage.\n");
socdough 0:506d7846938a 40 pc.printf("2. Move Stage Forward.\n");
socdough 0:506d7846938a 41 pc.printf("3. Move Stage Backwards.\n");
socdough 0:506d7846938a 42 pc.printf("4. Exit Program.\n\n");
socdough 0:506d7846938a 43 pc.scanf("%d",&count);
socdough 0:506d7846938a 44 pc.printf("Option %i\n\n",count);
socdough 0:506d7846938a 45
socdough 0:506d7846938a 46 switch (count) { //Switch function to move to loop requested by user.
socdough 0:506d7846938a 47 case 1: // (HOME) Call function here to do the required operation
socdough 0:506d7846938a 48
socdough 0:506d7846938a 49 pc.printf("We're sorry, this function is not available at this time!!\n\n");
socdough 0:506d7846938a 50 break;
socdough 0:506d7846938a 51
socdough 0:506d7846938a 52 case 2: // (Move Forward) Call function here to do the required operation
socdough 0:506d7846938a 53
socdough 0:506d7846938a 54 inputsteps=1;
socdough 0:506d7846938a 55 while ( (inputsteps > 0) && (inputsteps < 101) ){ //Experimented to determine this range.
socdough 0:506d7846938a 56 pc.printf("How many steps do you want to move?\n"); //Next two lines print to PC for user input.
socdough 0:506d7846938a 57 pc.printf(" Enter a number between 1 and 100.\n\n");
socdough 0:506d7846938a 58 pc.scanf("%i", &inputsteps); //Reads PC terminal to take in user input.
socdough 0:506d7846938a 59 lcd.cls(); //Clears LCD screen.
socdough 0:506d7846938a 60 lcd.printf("%i", inputsteps); //Print User command to mbed LCD.
socdough 0:506d7846938a 61
socdough 0:506d7846938a 62 int stepcount = 0; // keeps track of how many steps were taken
socdough 0:506d7846938a 63 pc.printf("Move started....\n\n"); //Prints so user knows command was received.
socdough 0:506d7846938a 64
socdough 0:506d7846938a 65 while (stepcount<inputsteps) { // compares how many steps taken to the desired steps passed in
socdough 0:506d7846938a 66 count++;
socdough 0:506d7846938a 67
socdough 0:506d7846938a 68 if (count == 5) // if the motor went around oncce..
socdough 0:506d7846938a 69 count=1; // ...reset the counter
socdough 0:506d7846938a 70
socdough 0:506d7846938a 71 switch (count) {
socdough 0:506d7846938a 72
socdough 0:506d7846938a 73 case 1: // if count equals 1 do this
socdough 0:506d7846938a 74 STP1 = 1;
socdough 0:506d7846938a 75 STP2 = 0;
socdough 0:506d7846938a 76 STP3 = 0;
socdough 0:506d7846938a 77 STP4 = 0;
socdough 0:506d7846938a 78 break; // this skips the code below until stepcount++
socdough 0:506d7846938a 79
socdough 0:506d7846938a 80 case 2:
socdough 0:506d7846938a 81 STP1 = 0;
socdough 0:506d7846938a 82 STP2 = 0;
socdough 0:506d7846938a 83 STP3 = 1;
socdough 0:506d7846938a 84 STP4 = 0;
socdough 0:506d7846938a 85 break;
socdough 0:506d7846938a 86
socdough 0:506d7846938a 87 case 3:
socdough 0:506d7846938a 88 STP1 = 0;
socdough 0:506d7846938a 89 STP2 = 1;
socdough 0:506d7846938a 90 STP3 = 0;
socdough 0:506d7846938a 91 STP4 = 0;
socdough 0:506d7846938a 92 break;
socdough 0:506d7846938a 93
socdough 0:506d7846938a 94 case 4:
socdough 0:506d7846938a 95 STP1 = 0;
socdough 0:506d7846938a 96 STP2 = 0;
socdough 0:506d7846938a 97 STP3 = 0;
socdough 0:506d7846938a 98 STP4 = 1;
socdough 0:506d7846938a 99 break;
socdough 0:506d7846938a 100 }
socdough 0:506d7846938a 101 stepcount++; //Iterate loop to keep track of steps.
socdough 0:506d7846938a 102 lcd.cls(); //Clear mbed LCD screen.
socdough 0:506d7846938a 103 lcd.printf("%i", stepcount);//Print loop count on LCD.
socdough 0:506d7846938a 104 wait(delayStep); //Defines how fast to step the motor.
socdough 0:506d7846938a 105 }
socdough 0:506d7846938a 106 inputsteps=0; //Reset inputsteps so that while loop becomes false.
socdough 0:506d7846938a 107 pc.printf("%i steps completed.\n\n", stepcount); //Prints number of steps performed.
socdough 0:506d7846938a 108 }
socdough 0:506d7846938a 109 break;
socdough 0:506d7846938a 110
socdough 0:506d7846938a 111 case 3: // (Move Backward) Call function here to do the required operation
socdough 0:506d7846938a 112
socdough 0:506d7846938a 113 inputsteps=1;
socdough 0:506d7846938a 114 while ( (inputsteps > 0) && (inputsteps < 101) ){ //Experimented to determine this range.
socdough 0:506d7846938a 115 pc.printf("How many steps do you want to move?\n"); //Next two lines print to PC for user input.
socdough 0:506d7846938a 116 pc.printf(" Enter a number between 1 and 100.\n\n");
socdough 0:506d7846938a 117 pc.scanf("%i", &inputsteps); //Reads PC terminal to take in user input.
socdough 0:506d7846938a 118 lcd.cls(); //Clears LCD screen.
socdough 0:506d7846938a 119 lcd.printf("%i", inputsteps); //Print User command to mbed LCD.
socdough 0:506d7846938a 120
socdough 0:506d7846938a 121 int stepcount = 0; // keeps track of how many steps were taken
socdough 0:506d7846938a 122 inputsteps=-1*inputsteps; // reverses sign so that motor steps backwards.
socdough 0:506d7846938a 123 pc.printf("Move started....\n\n"); //Prints so user knows command was received.
socdough 0:506d7846938a 124 while (stepcount>inputsteps) {
socdough 0:506d7846938a 125
socdough 0:506d7846938a 126 count--;
socdough 0:506d7846938a 127
socdough 0:506d7846938a 128 if (count == 0) // if the motor went around oncce..
socdough 0:506d7846938a 129 count=4; // ...reset the counter
socdough 0:506d7846938a 130
socdough 0:506d7846938a 131 switch (count) {
socdough 0:506d7846938a 132
socdough 0:506d7846938a 133 case 1: // if count equals 1 do this
socdough 0:506d7846938a 134 STP1 = 1;
socdough 0:506d7846938a 135 STP2 = 0;
socdough 0:506d7846938a 136 STP3 = 0;
socdough 0:506d7846938a 137 STP4 = 0;
socdough 0:506d7846938a 138 break;
socdough 0:506d7846938a 139
socdough 0:506d7846938a 140 case 2: // this skips the code below until stepcount--
socdough 0:506d7846938a 141 STP1 = 0;
socdough 0:506d7846938a 142 STP2 = 0;
socdough 0:506d7846938a 143 STP3 = 1;
socdough 0:506d7846938a 144 STP4 = 0;
socdough 0:506d7846938a 145 break;
socdough 0:506d7846938a 146
socdough 0:506d7846938a 147 case 3:
socdough 0:506d7846938a 148 STP1 = 0;
socdough 0:506d7846938a 149 STP2 = 1;
socdough 0:506d7846938a 150 STP3 = 0;
socdough 0:506d7846938a 151 STP4 = 0;
socdough 0:506d7846938a 152 break;
socdough 0:506d7846938a 153
socdough 0:506d7846938a 154 case 4:
socdough 0:506d7846938a 155 STP1 = 0;
socdough 0:506d7846938a 156 STP2 = 0;
socdough 0:506d7846938a 157 STP3 = 0;
socdough 0:506d7846938a 158 STP4 = 1;
socdough 0:506d7846938a 159 break;
socdough 0:506d7846938a 160 }
socdough 0:506d7846938a 161 stepcount--; //Iterate loop to keep track of steps.
socdough 0:506d7846938a 162 lcd.cls(); //Clear mbed LCD screen.
socdough 0:506d7846938a 163 lcd.printf("%i", stepcount);//Print loop count on LCD.
socdough 0:506d7846938a 164 wait(delayStep);
socdough 0:506d7846938a 165 }
socdough 0:506d7846938a 166 inputsteps=0; //Reset inputsteps so that while loop becomes false.
socdough 0:506d7846938a 167 pc.printf("%i steps completed.\n\n", stepcount); //Prints number of steps performed.
socdough 0:506d7846938a 168 }
socdough 0:506d7846938a 169 break;
socdough 0:506d7846938a 170
socdough 0:506d7846938a 171 case 4: // (End Program) Call function here to do the required operation
socdough 0:506d7846938a 172 printf("Goodbye\n\n");
socdough 0:506d7846938a 173 wait(100000); //Wait forever!
socdough 0:506d7846938a 174 break;
socdough 0:506d7846938a 175
socdough 0:506d7846938a 176 default: // (Invaild Input)
socdough 0:506d7846938a 177 pc.printf("Invaild Choice. Try again...\n\n");
socdough 0:506d7846938a 178 wait(1);
socdough 0:506d7846938a 179 break;
socdough 0:506d7846938a 180 }
socdough 0:506d7846938a 181 }
socdough 0:506d7846938a 182
socdough 0:506d7846938a 183 while (1); //Program won't run without this while loop!
socdough 0:506d7846938a 184
socdough 0:506d7846938a 185 }
socdough 0:506d7846938a 186
socdough 0:506d7846938a 187