Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed mbed-rtos TextLCD
STEPPER_MOTOR.cpp@12:d9c133b360b0, 2019-02-05 (annotated)
- Committer:
- thomasmorris
- Date:
- Tue Feb 05 16:20:32 2019 +0000
- Revision:
- 12:d9c133b360b0
- Child:
- 14:63998be3d43c
Working LCD
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thomasmorris | 12:d9c133b360b0 | 1 | #include "mbed.h" //Include the mbed libraries |
thomasmorris | 12:d9c133b360b0 | 2 | #include "STEPPER_MOTOR.hpp" //Include the header file, this acts like a series of forward declarations |
thomasmorris | 12:d9c133b360b0 | 3 | |
thomasmorris | 12:d9c133b360b0 | 4 | //Constructor |
thomasmorris | 12:d9c133b360b0 | 5 | STEPPER_MOTOR::STEPPER_MOTOR(PinName N1, PinName N2, PinName N3, PinName N4) : pin1(N1),pin2(N2),pin3(N3),pin4(N4) |
thomasmorris | 12:d9c133b360b0 | 6 | { |
thomasmorris | 12:d9c133b360b0 | 7 | _dir = true; |
thomasmorris | 12:d9c133b360b0 | 8 | _step = 0; |
thomasmorris | 12:d9c133b360b0 | 9 | } |
thomasmorris | 12:d9c133b360b0 | 10 | |
thomasmorris | 12:d9c133b360b0 | 11 | STEPPER_MOTOR::~STEPPER_MOTOR(){} //Destructor |
thomasmorris | 12:d9c133b360b0 | 12 | |
thomasmorris | 12:d9c133b360b0 | 13 | void STEPPER_MOTOR::Rotate_Steps(int Steps) |
thomasmorris | 12:d9c133b360b0 | 14 | { |
thomasmorris | 12:d9c133b360b0 | 15 | #define STIME 1000 |
thomasmorris | 12:d9c133b360b0 | 16 | |
thomasmorris | 12:d9c133b360b0 | 17 | Steps = Steps*50; |
thomasmorris | 12:d9c133b360b0 | 18 | //int correctionfactor; |
thomasmorris | 12:d9c133b360b0 | 19 | //int timeofturn=correctinfactor*speed; |
thomasmorris | 12:d9c133b360b0 | 20 | |
thomasmorris | 12:d9c133b360b0 | 21 | int mystep=0; |
thomasmorris | 12:d9c133b360b0 | 22 | printf("START!!! step value is=%d\n\r",mystep); |
thomasmorris | 12:d9c133b360b0 | 23 | for(int x =0 ; x <= Steps; x++) |
thomasmorris | 12:d9c133b360b0 | 24 | { |
thomasmorris | 12:d9c133b360b0 | 25 | printf("FOR LOOP! step value is=%d %d\n\r",mystep,x); // debugging feedback |
thomasmorris | 12:d9c133b360b0 | 26 | // phase pulse order |
thomasmorris | 12:d9c133b360b0 | 27 | this->pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 28 | this->pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 29 | this->pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 30 | this->pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 31 | mystep=1; |
thomasmorris | 12:d9c133b360b0 | 32 | wait_us(1000); |
thomasmorris | 12:d9c133b360b0 | 33 | |
thomasmorris | 12:d9c133b360b0 | 34 | printf("FOR LOOP! step value is=%d %d\n\r",mystep,x); |
thomasmorris | 12:d9c133b360b0 | 35 | this->pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 36 | this->pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 37 | this->pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 38 | this->pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 39 | mystep=2; |
thomasmorris | 12:d9c133b360b0 | 40 | wait_us(1000); |
thomasmorris | 12:d9c133b360b0 | 41 | |
thomasmorris | 12:d9c133b360b0 | 42 | printf("FOR LOOP! step value is=%d %d\n\r",mystep,x); |
thomasmorris | 12:d9c133b360b0 | 43 | this->pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 44 | this->pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 45 | this->pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 46 | this->pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 47 | mystep=3; |
thomasmorris | 12:d9c133b360b0 | 48 | wait_us(1000); |
thomasmorris | 12:d9c133b360b0 | 49 | |
thomasmorris | 12:d9c133b360b0 | 50 | printf("FOR LOOP! step value is=%d %d\n\r",mystep,x); |
thomasmorris | 12:d9c133b360b0 | 51 | this->pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 52 | this->pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 53 | this->pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 54 | this->pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 55 | mystep=4; |
thomasmorris | 12:d9c133b360b0 | 56 | wait_us(1000);; |
thomasmorris | 12:d9c133b360b0 | 57 | |
thomasmorris | 12:d9c133b360b0 | 58 | /* printf("FOR LOOP! step value is=%d %d\n\r",mystep,x); |
thomasmorris | 12:d9c133b360b0 | 59 | this->pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 60 | this->pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 61 | this->pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 62 | this->pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 63 | mystep=5; |
thomasmorris | 12:d9c133b360b0 | 64 | wait_ms(0.6); |
thomasmorris | 12:d9c133b360b0 | 65 | printf("FOR LOOP! step value is=%d %d\n\r",mystep,x); |
thomasmorris | 12:d9c133b360b0 | 66 | this->pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 67 | this->pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 68 | this->pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 69 | this->pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 70 | mystep=6; |
thomasmorris | 12:d9c133b360b0 | 71 | wait_ms(0.6); |
thomasmorris | 12:d9c133b360b0 | 72 | printf("FOR LOOP! step value is=%d %d\n\r",mystep,x); |
thomasmorris | 12:d9c133b360b0 | 73 | this->pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 74 | this->pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 75 | this->pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 76 | this->pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 77 | mystep=7; |
thomasmorris | 12:d9c133b360b0 | 78 | wait_ms(0.6); |
thomasmorris | 12:d9c133b360b0 | 79 | |
thomasmorris | 12:d9c133b360b0 | 80 | this->pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 81 | this->pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 82 | this->pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 83 | this->pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 84 | mystep=0; |
thomasmorris | 12:d9c133b360b0 | 85 | wait_ms(0.6); |
thomasmorris | 12:d9c133b360b0 | 86 | |
thomasmorris | 12:d9c133b360b0 | 87 | this->pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 88 | this->pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 89 | this->pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 90 | this->pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 91 | mystep=0; |
thomasmorris | 12:d9c133b360b0 | 92 | wait_ms(0.6);*/ |
thomasmorris | 12:d9c133b360b0 | 93 | } |
thomasmorris | 12:d9c133b360b0 | 94 | } |
thomasmorris | 12:d9c133b360b0 | 95 | |
thomasmorris | 12:d9c133b360b0 | 96 | /* |
thomasmorris | 12:d9c133b360b0 | 97 | switch(mystep){ |
thomasmorris | 12:d9c133b360b0 | 98 | case 0:{ |
thomasmorris | 12:d9c133b360b0 | 99 | |
thomasmorris | 12:d9c133b360b0 | 100 | break; |
thomasmorris | 12:d9c133b360b0 | 101 | case 1:{ |
thomasmorris | 12:d9c133b360b0 | 102 | |
thomasmorris | 12:d9c133b360b0 | 103 | break; |
thomasmorris | 12:d9c133b360b0 | 104 | case 2: |
thomasmorris | 12:d9c133b360b0 | 105 | |
thomasmorris | 12:d9c133b360b0 | 106 | break; |
thomasmorris | 12:d9c133b360b0 | 107 | case 3: |
thomasmorris | 12:d9c133b360b0 | 108 | |
thomasmorris | 12:d9c133b360b0 | 109 | break; |
thomasmorris | 12:d9c133b360b0 | 110 | case 4: |
thomasmorris | 12:d9c133b360b0 | 111 | |
thomasmorris | 12:d9c133b360b0 | 112 | break; |
thomasmorris | 12:d9c133b360b0 | 113 | case 5: |
thomasmorris | 12:d9c133b360b0 | 114 | |
thomasmorris | 12:d9c133b360b0 | 115 | break; |
thomasmorris | 12:d9c133b360b0 | 116 | case 6: |
thomasmorris | 12:d9c133b360b0 | 117 | |
thomasmorris | 12:d9c133b360b0 | 118 | break; |
thomasmorris | 12:d9c133b360b0 | 119 | case 7: |
thomasmorris | 12:d9c133b360b0 | 120 | |
thomasmorris | 12:d9c133b360b0 | 121 | break; |
thomasmorris | 12:d9c133b360b0 | 122 | default: |
thomasmorris | 12:d9c133b360b0 | 123 | |
thomasmorris | 12:d9c133b360b0 | 124 | break; |
thomasmorris | 12:d9c133b360b0 | 125 | } |
thomasmorris | 12:d9c133b360b0 | 126 | |
thomasmorris | 12:d9c133b360b0 | 127 | if(_dir){ |
thomasmorris | 12:d9c133b360b0 | 128 | _step++; |
thomasmorris | 12:d9c133b360b0 | 129 | }else{ |
thomasmorris | 12:d9c133b360b0 | 130 | _step--; |
thomasmorris | 12:d9c133b360b0 | 131 | } |
thomasmorris | 12:d9c133b360b0 | 132 | if(_step>7){ |
thomasmorris | 12:d9c133b360b0 | 133 | _step=0; |
thomasmorris | 12:d9c133b360b0 | 134 | } |
thomasmorris | 12:d9c133b360b0 | 135 | if(_step<0){ |
thomasmorris | 12:d9c133b360b0 | 136 | _step=7; |
thomasmorris | 12:d9c133b360b0 | 137 | } |
thomasmorris | 12:d9c133b360b0 | 138 | */ |
thomasmorris | 12:d9c133b360b0 | 139 | //wait_ms(1); |
thomasmorris | 12:d9c133b360b0 | 140 | |
thomasmorris | 12:d9c133b360b0 | 141 | |
thomasmorris | 12:d9c133b360b0 | 142 | void STEPPER_MOTOR::Permanent_Rotate_clock_wise() |
thomasmorris | 12:d9c133b360b0 | 143 | { |
thomasmorris | 12:d9c133b360b0 | 144 | switch(_step){ |
thomasmorris | 12:d9c133b360b0 | 145 | case 0: |
thomasmorris | 12:d9c133b360b0 | 146 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 147 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 148 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 149 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 150 | break; |
thomasmorris | 12:d9c133b360b0 | 151 | case 1: |
thomasmorris | 12:d9c133b360b0 | 152 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 153 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 154 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 155 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 156 | break; |
thomasmorris | 12:d9c133b360b0 | 157 | case 2: |
thomasmorris | 12:d9c133b360b0 | 158 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 159 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 160 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 161 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 162 | break; |
thomasmorris | 12:d9c133b360b0 | 163 | case 3: |
thomasmorris | 12:d9c133b360b0 | 164 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 165 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 166 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 167 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 168 | break; |
thomasmorris | 12:d9c133b360b0 | 169 | case 4: |
thomasmorris | 12:d9c133b360b0 | 170 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 171 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 172 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 173 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 174 | break; |
thomasmorris | 12:d9c133b360b0 | 175 | case 5: |
thomasmorris | 12:d9c133b360b0 | 176 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 177 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 178 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 179 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 180 | break; |
thomasmorris | 12:d9c133b360b0 | 181 | case 6: |
thomasmorris | 12:d9c133b360b0 | 182 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 183 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 184 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 185 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 186 | break; |
thomasmorris | 12:d9c133b360b0 | 187 | case 7: |
thomasmorris | 12:d9c133b360b0 | 188 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 189 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 190 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 191 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 192 | break; |
thomasmorris | 12:d9c133b360b0 | 193 | default: |
thomasmorris | 12:d9c133b360b0 | 194 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 195 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 196 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 197 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 198 | break; |
thomasmorris | 12:d9c133b360b0 | 199 | } |
thomasmorris | 12:d9c133b360b0 | 200 | if(_dir){ |
thomasmorris | 12:d9c133b360b0 | 201 | _step++; |
thomasmorris | 12:d9c133b360b0 | 202 | }else{ |
thomasmorris | 12:d9c133b360b0 | 203 | _step--; |
thomasmorris | 12:d9c133b360b0 | 204 | } |
thomasmorris | 12:d9c133b360b0 | 205 | if(_step>7){ |
thomasmorris | 12:d9c133b360b0 | 206 | _step=0; |
thomasmorris | 12:d9c133b360b0 | 207 | } |
thomasmorris | 12:d9c133b360b0 | 208 | if(_step<0){ |
thomasmorris | 12:d9c133b360b0 | 209 | _step=7; |
thomasmorris | 12:d9c133b360b0 | 210 | } |
thomasmorris | 12:d9c133b360b0 | 211 | } |
thomasmorris | 12:d9c133b360b0 | 212 | |
thomasmorris | 12:d9c133b360b0 | 213 | void STEPPER_MOTOR::Permanent_Rotate_anti_clock_wise() |
thomasmorris | 12:d9c133b360b0 | 214 | { |
thomasmorris | 12:d9c133b360b0 | 215 | |
thomasmorris | 12:d9c133b360b0 | 216 | //Rotate |
thomasmorris | 12:d9c133b360b0 | 217 | switch(_step){ |
thomasmorris | 12:d9c133b360b0 | 218 | case 0: |
thomasmorris | 12:d9c133b360b0 | 219 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 220 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 221 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 222 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 223 | break; |
thomasmorris | 12:d9c133b360b0 | 224 | case 1: |
thomasmorris | 12:d9c133b360b0 | 225 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 226 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 227 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 228 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 229 | break; |
thomasmorris | 12:d9c133b360b0 | 230 | case 2: |
thomasmorris | 12:d9c133b360b0 | 231 | pin1 = 1; |
thomasmorris | 12:d9c133b360b0 | 232 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 233 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 234 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 235 | break; |
thomasmorris | 12:d9c133b360b0 | 236 | case 3: |
thomasmorris | 12:d9c133b360b0 | 237 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 238 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 239 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 240 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 241 | break; |
thomasmorris | 12:d9c133b360b0 | 242 | case 4: |
thomasmorris | 12:d9c133b360b0 | 243 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 244 | pin2 = 1; |
thomasmorris | 12:d9c133b360b0 | 245 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 246 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 247 | break; |
thomasmorris | 12:d9c133b360b0 | 248 | case 5: |
thomasmorris | 12:d9c133b360b0 | 249 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 250 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 251 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 252 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 253 | break; |
thomasmorris | 12:d9c133b360b0 | 254 | case 6: |
thomasmorris | 12:d9c133b360b0 | 255 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 256 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 257 | pin3 = 1; |
thomasmorris | 12:d9c133b360b0 | 258 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 259 | break; |
thomasmorris | 12:d9c133b360b0 | 260 | case 7: |
thomasmorris | 12:d9c133b360b0 | 261 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 262 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 263 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 264 | pin4 = 1; |
thomasmorris | 12:d9c133b360b0 | 265 | break; |
thomasmorris | 12:d9c133b360b0 | 266 | default: |
thomasmorris | 12:d9c133b360b0 | 267 | pin1 = 0; |
thomasmorris | 12:d9c133b360b0 | 268 | pin2 = 0; |
thomasmorris | 12:d9c133b360b0 | 269 | pin3 = 0; |
thomasmorris | 12:d9c133b360b0 | 270 | pin4 = 0; |
thomasmorris | 12:d9c133b360b0 | 271 | break; |
thomasmorris | 12:d9c133b360b0 | 272 | } |
thomasmorris | 12:d9c133b360b0 | 273 | if(_dir){ |
thomasmorris | 12:d9c133b360b0 | 274 | _step++; |
thomasmorris | 12:d9c133b360b0 | 275 | }else{ |
thomasmorris | 12:d9c133b360b0 | 276 | _step--; |
thomasmorris | 12:d9c133b360b0 | 277 | } |
thomasmorris | 12:d9c133b360b0 | 278 | if(_step>7){ |
thomasmorris | 12:d9c133b360b0 | 279 | _step=0; |
thomasmorris | 12:d9c133b360b0 | 280 | } |
thomasmorris | 12:d9c133b360b0 | 281 | if(_step<0){ |
thomasmorris | 12:d9c133b360b0 | 282 | _step=7; |
thomasmorris | 12:d9c133b360b0 | 283 | } |
thomasmorris | 12:d9c133b360b0 | 284 | //wait_ms(1); |
thomasmorris | 12:d9c133b360b0 | 285 | } |
thomasmorris | 12:d9c133b360b0 | 286 |