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
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h 00003 00004 PwmOut pwm1(p21); //Servo Motor-1 PWM channel 00005 PwmOut pwm2(p22); //Servo Motor-2 PWM channel 00006 00007 DigitalOut dc1(p5); //DC motor input-1 00008 DigitalOut dc2(p6); //DC motor input-2 00009 PwmOut pwm3(p23); //DC motor PWM channel 00010 00011 AnalogIn button(p15); //Analog input from the floor buttons 00012 InterruptIn event(p7); //Interrupt for rising and falling edge detection from IR 00013 00014 Timer timer; //Timer to read falling and rising edge time 00015 Timer timer2; 00016 Serial pc(USBTX, USBRX);//Serial Communication 00017 00018 //Functions 00019 void openGate(); 00020 void closeGate(); 00021 void rising(); 00022 void falling(); 00023 void read_button(void const *args); 00024 void addtoarray(int val); 00025 int getState(float adc_val); 00026 00027 //Global variables 00028 int begin,end = 0; 00029 int dir = 1; // 1 is up and 0 is down 00030 int destinationState = 1; // Destination floor 00031 int currentState = 1; //Current floorfloat frequency = 0; 00032 00033 //Global arrays 00034 int fvalues[] = {0,100,250,500,700,1000}; 00035 int press[4]; // to store the total number of keypresses 00036 00037 00038 int main() { 00039 00040 int val; 00041 int i,temp =0; 00042 int flag_up =0; 00043 int flag_down =0; 00044 00045 press[0] = 0; 00046 press[1] = 0; 00047 press[2] = 0; 00048 press[3] = 0; 00049 00050 timer.start(); 00051 event.rise(&rising); 00052 event.fall(&falling); 00053 00054 //Setting dc1 to high and dc2 to low initially 00055 dc1 = 0; 00056 dc2 = 1; 00057 00058 //Setting the period and duty cycle for DC motors 00059 pwm3.period_ms(20); 00060 pwm3.write(0); 00061 00062 //Setting the period and duty cycle for Servo motors 00063 pwm1.period_ms(20); 00064 pwm2.period_ms(20); 00065 pwm1.write(0); 00066 pwm2.write(0); 00067 00068 Thread thread(read_button); 00069 00070 while(1){ 00071 00072 // Take the first element if not empty 00073 if(press[0] > 0) 00074 val = press[0]; 00075 else 00076 continue; 00077 00078 // Ignore if new pressed floor is same as current floor 00079 // Remove the first value and left shift 00080 if(val == currentState || val == 0){ 00081 pwm3.write(0); 00082 press[0] = press[1]; 00083 press[1] = press[2]; 00084 press[2] = press[3]; 00085 press[3] = 0; 00086 continue; 00087 } 00088 00089 00090 00091 dir = 1; 00092 flag_up = 1; 00093 00094 while(dir == 1){ 00095 00096 closeGate(); //Close gate 00097 printf("Moving up\n\n\n"); 00098 printf("Destination Floor: %d\n",val); 00099 printf("Current Floor: %d\n",currentState); 00100 00101 dc1 = 0; 00102 dc2 = 1; 00103 pwm3.write(0.1); 00104 printf("Destination Floor Frequency value:%d\n",fvalues[val]); 00105 00106 while(1){ 00107 if(((frequency > (fvalues[val] - 10)) && (frequency < (fvalues[val] + 10)))){ 00108 currentState = val; 00109 openGate(); 00110 break; 00111 } 00112 //else{ 00113 // printf("current freq: %f\n",frequency); 00114 //} 00115 } 00116 00117 //Check for any key presses greater than current floor 00118 for(i=0; i<4 ;i++){ 00119 00120 if(press[i]> 0 && press[i] > currentState ){ 00121 val = press[i]; 00122 press[i] = 0; 00123 flag_up = 1; 00124 break; 00125 } 00126 } 00127 00128 if(flag_up == 0) 00129 break; 00130 00131 } //end of while dir 00132 00133 00134 00135 dir = 0; 00136 flag_down = 1; 00137 00138 while(dir == 0){ 00139 00140 closeGate(); //Close gate 00141 printf("Moving down\n\n\n"); 00142 printf("Destination Floor: %d\n",val); 00143 printf("Current Floor: %d\n",currentState); 00144 dc1 = 1; 00145 dc2 = 0; 00146 pwm3.write(0.1); 00147 printf("Destination Floor Frequency value:%d\n",fvalues[val]); 00148 00149 while(1){ 00150 if(((frequency > (fvalues[val] - 10)) && (frequency < (fvalues[val] + 10)))){ 00151 currentState = val; 00152 openGate(); 00153 break; 00154 } 00155 //else{ 00156 // printf("current freq: %f\n",frequency); 00157 //} 00158 } 00159 00160 //Check for any key presses lesser than current floor 00161 for(i=4; i>=0 ;i++){ 00162 00163 if(press[i]> 0 && press[i] < currentState ){ 00164 val = press[i]; 00165 press[i] = 0; 00166 flag_down = 1; 00167 break; 00168 } 00169 } 00170 00171 if(flag_down == 0) 00172 break; 00173 } //end of while dir 00174 00175 }//end of outer while 1 00176 00177 } //end of main 00178 00179 00180 00181 00182 //Function to read the key pressed and store in array 00183 void read_button(void const *args) { 00184 00185 int i,prev_val,val = 0; 00186 printf("Entered thread\n"); 00187 00188 while (1) { 00189 00190 if (press[3] == 0){ // Enter only if array is not full 00191 00192 //float adc_val = button.read(); 00193 //int val = getState(adc_val); 00194 char c = pc.getc(); 00195 val = c - 48; 00196 printf("value entered : %d\n",val); 00197 00198 if(val!= prev_val){ // If new floor is pressed 00199 00200 prev_val = val; // Modify the previous value 00201 00202 if(dir == 1){ // If lift is goin up - check if read value is less than current destination 00203 if((val < destinationState)){ // If the value is less than destination floor make the current value as destination 00204 addtoarray(destintionState); 00205 destintionState = val; 00206 } 00207 else{ //if the value is greater than the destination, add it to the list 00208 addtoarray(val); 00209 } 00210 } 00211 00212 else if(dir == 0){ // If the lift is going down - check if read value is greater than current destination 00213 if((val > destinationState)){ // If the value is less than destination floor make the current value as destination 00214 addtoarray(destintionState); 00215 destintionState = val; 00216 } 00217 else{ //if the value is lesser than the destination, add it to the list 00218 addtoarray(val); 00219 } 00220 } 00221 } 00222 } 00223 } 00224 } 00225 00226 00227 00228 //Funtion to remove first element in the array 00229 void removefirst(){ 00230 press[0] = 0; 00231 press[1] = press[2]; 00232 press[2] = press[3]; 00233 press[3] = press[4]; 00234 } 00235 00236 //Function to add to array at the back 00237 void addtoarray(int val){ 00238 00239 int i; 00240 00241 if(press[3] == 0){ //insert only if array is not full 00242 00243 for(i=0;i<4;i++){ 00244 if(press[i] != 0){ 00245 press[i] = val; 00246 break; 00247 } 00248 } 00249 ] 00250 else 00251 printf("The array is full\n"); 00252 } 00253 00254 // Function to open gates 00255 void openGate(){ 00256 pwm1.write(0.0375); // 3.75% duty cycle - Open the gate 00257 pwm2.write(0.1125); 00258 wait(2); 00259 pwm1.write(0); // Stop 00260 pwm2.write(0); 00261 } 00262 00263 // Function to close gates 00264 void closeGate(){ 00265 pwm1.write(0.1125); // 11.25% duty cycle - Close the gate 00266 pwm2.write(0.0375); 00267 wait(2); 00268 pwm1.write(0); // Stop 00269 pwm2.write(0); 00270 } 00271 00272 // Funciton to get floor based on keypress voltage 00273 int getState(float adc_val){ 00274 int state; 00275 00276 if(adc_val > 0.15 && adc_val < 0.25){ 00277 state = 2; 00278 printf("Button Press : %d\n",state); 00279 } 00280 else if(adc_val > 0.35 && adc_val < 0.45){ 00281 state = 1; 00282 printf("Button Press : %d\n",state); 00283 } 00284 else if(adc_val > 0.55 && adc_val < 0.65){ 00285 state = 3; 00286 printf("Button Press : %d\n",state); 00287 } 00288 else if(adc_val > 0.75 && adc_val < 0.85){ 00289 state = 4; 00290 printf("Button Press : %d\n",state); 00291 } 00292 else if(adc_val > 0.95 && adc_val < 1.05){ 00293 state = 5; 00294 printf("Button Press : %d\n",state); 00295 } 00296 00297 return state; 00298 } 00299 00300 00301 // Function to check for rising edge 00302 void rising(){ 00303 begin = timer.read_us(); 00304 flag = true; 00305 } 00306 00307 00308 // Function to check for falling edge 00309 void falling(){ 00310 if(flag == true){ 00311 end = timer.read_us(); 00312 frequency = 500000/(end-begin); 00313 begin = 0; 00314 end = 0; 00315 flag = false; 00316 } 00317 } 00318 00319 00320 00321 //Extra Stuff 00322 00323 00324 /* 00325 //Sorting the array 00326 printf("sorting array\n"); 00327 for (a = 0; a < 4; a++){ 00328 for (b = a + 1; b < 4; b++){ 00329 if (press[a] > 0 && press[b] > 0 && press[a] > press[b]){ 00330 temp = press[a]; 00331 press[a] = press[b]; 00332 press[b] = temp; 00333 } 00334 } 00335 } 00336 */ 00337 00338 00339 /* 00340 //if(press[3] > 0){ 00341 //printf("Queue size exceeded\n"); 00342 //} 00343 */
Generated on Sun Dec 24 2023 03:36:20 by
