Testing 1 blue pill

Dependencies:   mbed mbed-rtos TextLCD

Committer:
thomasmorris
Date:
Tue Feb 12 10:03:05 2019 +0000
Revision:
17:68b3fdabe4c5
Parent:
STEPPER_MOTOR.cpp@16:9f98ec0ededb
Child:
18:3523660f3930
Working Code;

Who changed what in which revision?

UserRevisionLine numberNew 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 17:68b3fdabe4c5 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 17:68b3fdabe4c5 20
thomasmorris 12:d9c133b360b0 21 int mystep=0;
thomasmorris 17:68b3fdabe4c5 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 17:68b3fdabe4c5 25 //printf("FOR LOOP! step value is=%d %d\n\r",mystep,x);
thomasmorris 12:d9c133b360b0 26 this->pin1 = 0;
thomasmorris 12:d9c133b360b0 27 this->pin2 = 1;
thomasmorris 12:d9c133b360b0 28 this->pin3 = 0;
thomasmorris 12:d9c133b360b0 29 this->pin4 = 1;
thomasmorris 12:d9c133b360b0 30 mystep=1;
thomasmorris 12:d9c133b360b0 31 wait_us(1000);
thomasmorris 14:63998be3d43c 32 //printf("FOR LOOP! step value is=%d %d\n\r",mystep,x);
thomasmorris 12:d9c133b360b0 33 this->pin1 = 0;
thomasmorris 12:d9c133b360b0 34 this->pin2 = 1;
thomasmorris 12:d9c133b360b0 35 this->pin3 = 1;
thomasmorris 12:d9c133b360b0 36 this->pin4 = 0;
thomasmorris 12:d9c133b360b0 37 mystep=2;
thomasmorris 12:d9c133b360b0 38 wait_us(1000);
thomasmorris 14:63998be3d43c 39 //printf("FOR LOOP! step value is=%d %d\n\r",mystep,x);
thomasmorris 12:d9c133b360b0 40 this->pin1 = 1;
thomasmorris 12:d9c133b360b0 41 this->pin2 = 0;
thomasmorris 12:d9c133b360b0 42 this->pin3 = 1;
thomasmorris 12:d9c133b360b0 43 this->pin4 = 0;
thomasmorris 12:d9c133b360b0 44 mystep=3;
thomasmorris 12:d9c133b360b0 45 wait_us(1000);
thomasmorris 17:68b3fdabe4c5 46 //printf("FOR LOOP! step value is=%d %d\n\r",mystep,x);
thomasmorris 12:d9c133b360b0 47 this->pin1 = 1;
thomasmorris 12:d9c133b360b0 48 this->pin2 = 0;
thomasmorris 12:d9c133b360b0 49 this->pin3 = 0;
thomasmorris 12:d9c133b360b0 50 this->pin4 = 1;
thomasmorris 12:d9c133b360b0 51 mystep=4;
thomasmorris 14:63998be3d43c 52 wait_us(1000);
thomasmorris 12:d9c133b360b0 53 /* printf("FOR LOOP! step value is=%d %d\n\r",mystep,x);
thomasmorris 12:d9c133b360b0 54 this->pin1 = 0;
thomasmorris 12:d9c133b360b0 55 this->pin2 = 1;
thomasmorris 12:d9c133b360b0 56 this->pin3 = 0;
thomasmorris 12:d9c133b360b0 57 this->pin4 = 0;
thomasmorris 12:d9c133b360b0 58 mystep=5;
thomasmorris 12:d9c133b360b0 59 wait_ms(0.6);
thomasmorris 12:d9c133b360b0 60 printf("FOR LOOP! step value is=%d %d\n\r",mystep,x);
thomasmorris 12:d9c133b360b0 61 this->pin1 = 1;
thomasmorris 12:d9c133b360b0 62 this->pin2 = 1;
thomasmorris 12:d9c133b360b0 63 this->pin3 = 0;
thomasmorris 12:d9c133b360b0 64 this->pin4 = 0;
thomasmorris 12:d9c133b360b0 65 mystep=6;
thomasmorris 12:d9c133b360b0 66 wait_ms(0.6);
thomasmorris 12:d9c133b360b0 67 printf("FOR LOOP! step value is=%d %d\n\r",mystep,x);
thomasmorris 12:d9c133b360b0 68 this->pin1 = 1;
thomasmorris 12:d9c133b360b0 69 this->pin2 = 0;
thomasmorris 12:d9c133b360b0 70 this->pin3 = 0;
thomasmorris 12:d9c133b360b0 71 this->pin4 = 0;
thomasmorris 12:d9c133b360b0 72 mystep=7;
thomasmorris 12:d9c133b360b0 73 wait_ms(0.6);
thomasmorris 12:d9c133b360b0 74
thomasmorris 12:d9c133b360b0 75 this->pin1 = 1;
thomasmorris 12:d9c133b360b0 76 this->pin2 = 0;
thomasmorris 12:d9c133b360b0 77 this->pin3 = 0;
thomasmorris 12:d9c133b360b0 78 this->pin4 = 1;
thomasmorris 12:d9c133b360b0 79 mystep=0;
thomasmorris 12:d9c133b360b0 80 wait_ms(0.6);
thomasmorris 12:d9c133b360b0 81
thomasmorris 12:d9c133b360b0 82 this->pin1 = 0;
thomasmorris 12:d9c133b360b0 83 this->pin2 = 0;
thomasmorris 12:d9c133b360b0 84 this->pin3 = 0;
thomasmorris 12:d9c133b360b0 85 this->pin4 = 0;
thomasmorris 12:d9c133b360b0 86 mystep=0;
thomasmorris 12:d9c133b360b0 87 wait_ms(0.6);*/
thomasmorris 12:d9c133b360b0 88 }
thomasmorris 17:68b3fdabe4c5 89 this->pin1 = 0;
thomasmorris 17:68b3fdabe4c5 90 this->pin2 = 0;
thomasmorris 17:68b3fdabe4c5 91 this->pin3 = 0;
thomasmorris 17:68b3fdabe4c5 92 this->pin4 = 0;
thomasmorris 12:d9c133b360b0 93 }
thomasmorris 12:d9c133b360b0 94
thomasmorris 12:d9c133b360b0 95 /*
thomasmorris 12:d9c133b360b0 96 switch(mystep){
thomasmorris 12:d9c133b360b0 97 case 0:{
thomasmorris 12:d9c133b360b0 98
thomasmorris 12:d9c133b360b0 99 break;
thomasmorris 12:d9c133b360b0 100 case 1:{
thomasmorris 12:d9c133b360b0 101
thomasmorris 12:d9c133b360b0 102 break;
thomasmorris 12:d9c133b360b0 103 case 2:
thomasmorris 12:d9c133b360b0 104
thomasmorris 12:d9c133b360b0 105 break;
thomasmorris 12:d9c133b360b0 106 case 3:
thomasmorris 12:d9c133b360b0 107
thomasmorris 12:d9c133b360b0 108 break;
thomasmorris 12:d9c133b360b0 109 case 4:
thomasmorris 12:d9c133b360b0 110
thomasmorris 12:d9c133b360b0 111 break;
thomasmorris 12:d9c133b360b0 112 case 5:
thomasmorris 12:d9c133b360b0 113
thomasmorris 12:d9c133b360b0 114 break;
thomasmorris 12:d9c133b360b0 115 case 6:
thomasmorris 12:d9c133b360b0 116
thomasmorris 12:d9c133b360b0 117 break;
thomasmorris 12:d9c133b360b0 118 case 7:
thomasmorris 12:d9c133b360b0 119
thomasmorris 12:d9c133b360b0 120 break;
thomasmorris 12:d9c133b360b0 121 default:
thomasmorris 12:d9c133b360b0 122
thomasmorris 12:d9c133b360b0 123 break;
thomasmorris 12:d9c133b360b0 124 }
thomasmorris 12:d9c133b360b0 125
thomasmorris 12:d9c133b360b0 126 if(_dir){
thomasmorris 12:d9c133b360b0 127 _step++;
thomasmorris 12:d9c133b360b0 128 }else{
thomasmorris 12:d9c133b360b0 129 _step--;
thomasmorris 12:d9c133b360b0 130 }
thomasmorris 12:d9c133b360b0 131 if(_step>7){
thomasmorris 12:d9c133b360b0 132 _step=0;
thomasmorris 12:d9c133b360b0 133 }
thomasmorris 12:d9c133b360b0 134 if(_step<0){
thomasmorris 12:d9c133b360b0 135 _step=7;
thomasmorris 12:d9c133b360b0 136 }
thomasmorris 12:d9c133b360b0 137 */
thomasmorris 12:d9c133b360b0 138 //wait_ms(1);
thomasmorris 12:d9c133b360b0 139
thomasmorris 12:d9c133b360b0 140
thomasmorris 12:d9c133b360b0 141 void STEPPER_MOTOR::Permanent_Rotate_clock_wise()
thomasmorris 12:d9c133b360b0 142 {
thomasmorris 12:d9c133b360b0 143 switch(_step){
thomasmorris 12:d9c133b360b0 144 case 0:
thomasmorris 12:d9c133b360b0 145 pin1 = 0;
thomasmorris 12:d9c133b360b0 146 pin2 = 0;
thomasmorris 12:d9c133b360b0 147 pin3 = 0;
thomasmorris 12:d9c133b360b0 148 pin4 = 1;
thomasmorris 12:d9c133b360b0 149 break;
thomasmorris 12:d9c133b360b0 150 case 1:
thomasmorris 12:d9c133b360b0 151 pin1 = 0;
thomasmorris 12:d9c133b360b0 152 pin2 = 0;
thomasmorris 12:d9c133b360b0 153 pin3 = 1;
thomasmorris 12:d9c133b360b0 154 pin4 = 1;
thomasmorris 12:d9c133b360b0 155 break;
thomasmorris 12:d9c133b360b0 156 case 2:
thomasmorris 12:d9c133b360b0 157 pin1 = 0;
thomasmorris 12:d9c133b360b0 158 pin2 = 0;
thomasmorris 12:d9c133b360b0 159 pin3 = 1;
thomasmorris 12:d9c133b360b0 160 pin4 = 0;
thomasmorris 12:d9c133b360b0 161 break;
thomasmorris 12:d9c133b360b0 162 case 3:
thomasmorris 12:d9c133b360b0 163 pin1 = 0;
thomasmorris 12:d9c133b360b0 164 pin2 = 1;
thomasmorris 12:d9c133b360b0 165 pin3 = 1;
thomasmorris 12:d9c133b360b0 166 pin4 = 0;
thomasmorris 12:d9c133b360b0 167 break;
thomasmorris 12:d9c133b360b0 168 case 4:
thomasmorris 12:d9c133b360b0 169 pin1 = 0;
thomasmorris 12:d9c133b360b0 170 pin2 = 1;
thomasmorris 12:d9c133b360b0 171 pin3 = 0;
thomasmorris 12:d9c133b360b0 172 pin4 = 0;
thomasmorris 12:d9c133b360b0 173 break;
thomasmorris 12:d9c133b360b0 174 case 5:
thomasmorris 12:d9c133b360b0 175 pin1 = 1;
thomasmorris 12:d9c133b360b0 176 pin2 = 1;
thomasmorris 12:d9c133b360b0 177 pin3 = 0;
thomasmorris 12:d9c133b360b0 178 pin4 = 0;
thomasmorris 12:d9c133b360b0 179 break;
thomasmorris 12:d9c133b360b0 180 case 6:
thomasmorris 12:d9c133b360b0 181 pin1 = 1;
thomasmorris 12:d9c133b360b0 182 pin2 = 0;
thomasmorris 12:d9c133b360b0 183 pin3 = 0;
thomasmorris 12:d9c133b360b0 184 pin4 = 0;
thomasmorris 12:d9c133b360b0 185 break;
thomasmorris 12:d9c133b360b0 186 case 7:
thomasmorris 12:d9c133b360b0 187 pin1 = 1;
thomasmorris 12:d9c133b360b0 188 pin2 = 0;
thomasmorris 12:d9c133b360b0 189 pin3 = 0;
thomasmorris 12:d9c133b360b0 190 pin4 = 1;
thomasmorris 12:d9c133b360b0 191 break;
thomasmorris 12:d9c133b360b0 192 default:
thomasmorris 12:d9c133b360b0 193 pin1 = 0;
thomasmorris 12:d9c133b360b0 194 pin2 = 0;
thomasmorris 12:d9c133b360b0 195 pin3 = 0;
thomasmorris 12:d9c133b360b0 196 pin4 = 0;
thomasmorris 12:d9c133b360b0 197 break;
thomasmorris 12:d9c133b360b0 198 }
thomasmorris 12:d9c133b360b0 199 if(_dir){
thomasmorris 12:d9c133b360b0 200 _step++;
thomasmorris 12:d9c133b360b0 201 }else{
thomasmorris 12:d9c133b360b0 202 _step--;
thomasmorris 12:d9c133b360b0 203 }
thomasmorris 12:d9c133b360b0 204 if(_step>7){
thomasmorris 12:d9c133b360b0 205 _step=0;
thomasmorris 12:d9c133b360b0 206 }
thomasmorris 12:d9c133b360b0 207 if(_step<0){
thomasmorris 12:d9c133b360b0 208 _step=7;
thomasmorris 12:d9c133b360b0 209 }
thomasmorris 12:d9c133b360b0 210 }
thomasmorris 12:d9c133b360b0 211
thomasmorris 12:d9c133b360b0 212 void STEPPER_MOTOR::Permanent_Rotate_anti_clock_wise()
thomasmorris 12:d9c133b360b0 213 {
thomasmorris 12:d9c133b360b0 214
thomasmorris 12:d9c133b360b0 215 //Rotate
thomasmorris 12:d9c133b360b0 216 switch(_step){
thomasmorris 12:d9c133b360b0 217 case 0:
thomasmorris 12:d9c133b360b0 218 pin1 = 1;
thomasmorris 12:d9c133b360b0 219 pin2 = 0;
thomasmorris 12:d9c133b360b0 220 pin3 = 0;
thomasmorris 12:d9c133b360b0 221 pin4 = 1;
thomasmorris 12:d9c133b360b0 222 break;
thomasmorris 12:d9c133b360b0 223 case 1:
thomasmorris 12:d9c133b360b0 224 pin1 = 1;
thomasmorris 12:d9c133b360b0 225 pin2 = 0;
thomasmorris 12:d9c133b360b0 226 pin3 = 0;
thomasmorris 12:d9c133b360b0 227 pin4 = 0;
thomasmorris 12:d9c133b360b0 228 break;
thomasmorris 12:d9c133b360b0 229 case 2:
thomasmorris 12:d9c133b360b0 230 pin1 = 1;
thomasmorris 12:d9c133b360b0 231 pin2 = 1;
thomasmorris 12:d9c133b360b0 232 pin3 = 0;
thomasmorris 12:d9c133b360b0 233 pin4 = 0;
thomasmorris 12:d9c133b360b0 234 break;
thomasmorris 12:d9c133b360b0 235 case 3:
thomasmorris 12:d9c133b360b0 236 pin1 = 0;
thomasmorris 12:d9c133b360b0 237 pin2 = 1;
thomasmorris 12:d9c133b360b0 238 pin3 = 0;
thomasmorris 12:d9c133b360b0 239 pin4 = 0;
thomasmorris 12:d9c133b360b0 240 break;
thomasmorris 12:d9c133b360b0 241 case 4:
thomasmorris 12:d9c133b360b0 242 pin1 = 0;
thomasmorris 12:d9c133b360b0 243 pin2 = 1;
thomasmorris 12:d9c133b360b0 244 pin3 = 1;
thomasmorris 12:d9c133b360b0 245 pin4 = 0;
thomasmorris 12:d9c133b360b0 246 break;
thomasmorris 12:d9c133b360b0 247 case 5:
thomasmorris 12:d9c133b360b0 248 pin1 = 0;
thomasmorris 12:d9c133b360b0 249 pin2 = 0;
thomasmorris 12:d9c133b360b0 250 pin3 = 1;
thomasmorris 12:d9c133b360b0 251 pin4 = 0;
thomasmorris 12:d9c133b360b0 252 break;
thomasmorris 12:d9c133b360b0 253 case 6:
thomasmorris 12:d9c133b360b0 254 pin1 = 0;
thomasmorris 12:d9c133b360b0 255 pin2 = 0;
thomasmorris 12:d9c133b360b0 256 pin3 = 1;
thomasmorris 12:d9c133b360b0 257 pin4 = 1;
thomasmorris 12:d9c133b360b0 258 break;
thomasmorris 12:d9c133b360b0 259 case 7:
thomasmorris 12:d9c133b360b0 260 pin1 = 0;
thomasmorris 12:d9c133b360b0 261 pin2 = 0;
thomasmorris 12:d9c133b360b0 262 pin3 = 0;
thomasmorris 12:d9c133b360b0 263 pin4 = 1;
thomasmorris 12:d9c133b360b0 264 break;
thomasmorris 12:d9c133b360b0 265 default:
thomasmorris 12:d9c133b360b0 266 pin1 = 0;
thomasmorris 12:d9c133b360b0 267 pin2 = 0;
thomasmorris 12:d9c133b360b0 268 pin3 = 0;
thomasmorris 12:d9c133b360b0 269 pin4 = 0;
thomasmorris 12:d9c133b360b0 270 break;
thomasmorris 12:d9c133b360b0 271 }
thomasmorris 12:d9c133b360b0 272 if(_dir){
thomasmorris 12:d9c133b360b0 273 _step++;
thomasmorris 12:d9c133b360b0 274 }else{
thomasmorris 12:d9c133b360b0 275 _step--;
thomasmorris 12:d9c133b360b0 276 }
thomasmorris 12:d9c133b360b0 277 if(_step>7){
thomasmorris 12:d9c133b360b0 278 _step=0;
thomasmorris 12:d9c133b360b0 279 }
thomasmorris 12:d9c133b360b0 280 if(_step<0){
thomasmorris 12:d9c133b360b0 281 _step=7;
thomasmorris 12:d9c133b360b0 282 }
thomasmorris 12:d9c133b360b0 283 //wait_ms(1);
thomasmorris 12:d9c133b360b0 284 }
thomasmorris 12:d9c133b360b0 285