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
00001 #include "mbed.h" //Include the mbed libraries 00002 #include "STEPPER_MOTOR.hpp" //Include the header file, this acts like a series of forward declarations 00003 int loop_wait_time = 0; 00004 //Constructor 00005 STEPPER_MOTOR::STEPPER_MOTOR(PinName N1, PinName N2, PinName N3, PinName N4) : pin1(N1),pin2(N2),pin3(N3),pin4(N4) 00006 { 00007 _dir = true; 00008 _step = 0; 00009 } 00010 00011 STEPPER_MOTOR::~STEPPER_MOTOR(){} //Destructor 00012 00013 void STEPPER_MOTOR::Pause_Code() 00014 { 00015 _Pause_Code = 1; 00016 } 00017 void STEPPER_MOTOR::Unpause_Code() 00018 { 00019 _Pause_Code = 0; 00020 } 00021 float STEPPER_MOTOR::Get_Turns() 00022 { 00023 return (_Steps_Done /50); 00024 } 00025 void STEPPER_MOTOR::Rotate_Steps(int Steps,int Function,bool Direction) 00026 { 00027 if(Function == 2)//Coiling 00028 { 00029 loop_wait_time = 2; 00030 } 00031 else if(Function == 3) 00032 { 00033 loop_wait_time = 2;//Twisting 00034 } 00035 Steps = Steps*50;//int correctionfactor;//int timeofturn=correctinfactor*speed; 00036 _Steps_Done = 0; 00037 00038 for(int x =0 ; x <= Steps; x++) 00039 { 00040 if(_Pause_Code == 0) 00041 { 00042 if(Direction == 0) 00043 { 00044 this->pin1 = 0; 00045 this->pin2 = 1; 00046 this->pin3 = 0; 00047 this->pin4 = 1; 00048 Thread::wait(loop_wait_time); 00049 this->pin1 = 0; 00050 this->pin2 = 1; 00051 this->pin3 = 1; 00052 this->pin4 = 0; 00053 Thread::wait(loop_wait_time); 00054 this->pin1 = 1; 00055 this->pin2 = 0; 00056 this->pin3 = 1; 00057 this->pin4 = 0; 00058 Thread::wait(loop_wait_time); 00059 this->pin1 = 1; 00060 this->pin2 = 0; 00061 this->pin3 = 0; 00062 this->pin4 = 1; 00063 } 00064 else if (Direction == 1) 00065 { 00066 this->pin1 = 1; 00067 this->pin2 = 0; 00068 this->pin3 = 0; 00069 this->pin4 = 1; 00070 Thread::wait(loop_wait_time); 00071 this->pin1 = 1; 00072 this->pin2 = 0; 00073 this->pin3 = 1; 00074 this->pin4 = 0; 00075 Thread::wait(loop_wait_time); 00076 this->pin1 = 0; 00077 this->pin2 = 1; 00078 this->pin3 = 1; 00079 this->pin4 = 0; 00080 Thread::wait(loop_wait_time); 00081 this->pin1 = 0; 00082 this->pin2 = 1; 00083 this->pin3 = 0; 00084 this->pin4 = 1; 00085 } 00086 Thread::wait(loop_wait_time); 00087 _Steps_Done = _Steps_Done +1; 00088 } 00089 else if (_Pause_Code == 1) 00090 { 00091 printf("Stepper motor code paused\n"); 00092 while(_Pause_Code == 1) 00093 { 00094 Thread::wait(1); 00095 } 00096 } 00097 }//End of for loop 00098 this->pin1 = 0; 00099 this->pin2 = 0; 00100 this->pin3 = 0; 00101 this->pin4 = 0; 00102 } 00103 void STEPPER_MOTOR::Permanent_Rotate_clock_wise() 00104 { 00105 switch(_step){ 00106 case 0: 00107 pin1 = 0; 00108 pin2 = 0; 00109 pin3 = 0; 00110 pin4 = 1; 00111 break; 00112 case 1: 00113 pin1 = 0; 00114 pin2 = 0; 00115 pin3 = 1; 00116 pin4 = 1; 00117 break; 00118 case 2: 00119 pin1 = 0; 00120 pin2 = 0; 00121 pin3 = 1; 00122 pin4 = 0; 00123 break; 00124 case 3: 00125 pin1 = 0; 00126 pin2 = 1; 00127 pin3 = 1; 00128 pin4 = 0; 00129 break; 00130 case 4: 00131 pin1 = 0; 00132 pin2 = 1; 00133 pin3 = 0; 00134 pin4 = 0; 00135 break; 00136 case 5: 00137 pin1 = 1; 00138 pin2 = 1; 00139 pin3 = 0; 00140 pin4 = 0; 00141 break; 00142 case 6: 00143 pin1 = 1; 00144 pin2 = 0; 00145 pin3 = 0; 00146 pin4 = 0; 00147 break; 00148 case 7: 00149 pin1 = 1; 00150 pin2 = 0; 00151 pin3 = 0; 00152 pin4 = 1; 00153 break; 00154 default: 00155 pin1 = 0; 00156 pin2 = 0; 00157 pin3 = 0; 00158 pin4 = 0; 00159 break; 00160 } 00161 if(_dir){ 00162 _step++; 00163 }else{ 00164 _step--; 00165 } 00166 if(_step>7){ 00167 _step=0; 00168 } 00169 if(_step<0){ 00170 _step=7; 00171 } 00172 } 00173 00174 void STEPPER_MOTOR::Permanent_Rotate_anti_clock_wise() 00175 { 00176 00177 //Rotate 00178 switch(_step){ 00179 case 0: 00180 pin1 = 1; 00181 pin2 = 0; 00182 pin3 = 0; 00183 pin4 = 1; 00184 break; 00185 case 1: 00186 pin1 = 1; 00187 pin2 = 0; 00188 pin3 = 0; 00189 pin4 = 0; 00190 break; 00191 case 2: 00192 pin1 = 1; 00193 pin2 = 1; 00194 pin3 = 0; 00195 pin4 = 0; 00196 break; 00197 case 3: 00198 pin1 = 0; 00199 pin2 = 1; 00200 pin3 = 0; 00201 pin4 = 0; 00202 break; 00203 case 4: 00204 pin1 = 0; 00205 pin2 = 1; 00206 pin3 = 1; 00207 pin4 = 0; 00208 break; 00209 case 5: 00210 pin1 = 0; 00211 pin2 = 0; 00212 pin3 = 1; 00213 pin4 = 0; 00214 break; 00215 case 6: 00216 pin1 = 0; 00217 pin2 = 0; 00218 pin3 = 1; 00219 pin4 = 1; 00220 break; 00221 case 7: 00222 pin1 = 0; 00223 pin2 = 0; 00224 pin3 = 0; 00225 pin4 = 1; 00226 break; 00227 default: 00228 pin1 = 0; 00229 pin2 = 0; 00230 pin3 = 0; 00231 pin4 = 0; 00232 break; 00233 } 00234 if(_dir){ 00235 _step++; 00236 }else{ 00237 _step--; 00238 } 00239 if(_step>7){ 00240 _step=0; 00241 } 00242 if(_step<0){ 00243 _step=7; 00244 } 00245 //wait_ms(1); 00246 } 00247
Generated on Tue Jul 12 2022 21:12:35 by
1.7.2