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@7:3bed3e005200, 2015-10-15 (annotated)
- Committer:
- prithviganeshk
- Date:
- Thu Oct 15 03:54:33 2015 +0000
- Revision:
- 7:3bed3e005200
- Parent:
- 6:625384a34dd5
- Child:
- 8:7e3f92f0dc47
demo'd with this code
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jiteshg | 2:bf817b299c19 | 1 | |
| jiteshg | 0:fd080fb55bae | 2 | #include "mbed.h" |
| jiteshg | 2:bf817b299c19 | 3 | PwmOut pwm1(p21); //Servo Motor-1 PWM channel |
| jiteshg | 2:bf817b299c19 | 4 | PwmOut pwm2(p22); //Servo Motor-2 PWM channel |
| jiteshg | 2:bf817b299c19 | 5 | |
| jiteshg | 2:bf817b299c19 | 6 | DigitalOut dc1(p5); //DC motor input-1 |
| jiteshg | 2:bf817b299c19 | 7 | DigitalOut dc2(p6); //DC motor input-2 |
| jiteshg | 2:bf817b299c19 | 8 | PwmOut pwm3(p23); //DC motor PWM channel |
| jiteshg | 2:bf817b299c19 | 9 | |
| jiteshg | 3:66a52943c525 | 10 | AnalogIn button(p15); //Analog input from the floor buttons |
| jiteshg | 4:acb62dee5ba9 | 11 | InterruptIn event(p7); //Interrupt for rising and falling edge detection from IR |
| jiteshg | 3:66a52943c525 | 12 | |
| jiteshg | 4:acb62dee5ba9 | 13 | Timer timer; //Timer to read falling and rising edge time |
| prithviganeshk | 7:3bed3e005200 | 14 | |
| jiteshg | 4:acb62dee5ba9 | 15 | Serial pc(USBTX, USBRX);//Serial Communication |
| jiteshg | 0:fd080fb55bae | 16 | |
| jiteshg | 2:bf817b299c19 | 17 | void openGate(); |
| jiteshg | 2:bf817b299c19 | 18 | void closeGate(); |
| jiteshg | 4:acb62dee5ba9 | 19 | void rising(); |
| jiteshg | 4:acb62dee5ba9 | 20 | void falling(); |
| jiteshg | 4:acb62dee5ba9 | 21 | |
| jiteshg | 3:66a52943c525 | 22 | int getState(float adc_val); |
| jiteshg | 3:66a52943c525 | 23 | |
| jiteshg | 2:bf817b299c19 | 24 | int currentState = 1; |
| jiteshg | 4:acb62dee5ba9 | 25 | int begin,end = 0; |
| jiteshg | 4:acb62dee5ba9 | 26 | float frequency = 0; |
| jiteshg | 4:acb62dee5ba9 | 27 | bool flag = false; |
| jiteshg | 5:7b4575bf205e | 28 | //int fvalues[] = {0,100,250,500,700,1000}; |
| prithviganeshk | 7:3bed3e005200 | 29 | int fvalues[] = {0,100,250,500,700,700}; |
| prithviganeshk | 7:3bed3e005200 | 30 | //int fvalues[] = {0,102,270,615,948,1623}; |
| jiteshg | 2:bf817b299c19 | 31 | |
| jiteshg | 0:fd080fb55bae | 32 | int main() { |
| jiteshg | 4:acb62dee5ba9 | 33 | timer.start(); |
| jiteshg | 4:acb62dee5ba9 | 34 | event.rise(&rising); |
| jiteshg | 4:acb62dee5ba9 | 35 | event.fall(&falling); |
| jiteshg | 2:bf817b299c19 | 36 | //Setting dc1 to high and dc2 to low initially |
| jiteshg | 4:acb62dee5ba9 | 37 | dc1 = 0; |
| jiteshg | 4:acb62dee5ba9 | 38 | dc2 = 1; |
| jiteshg | 2:bf817b299c19 | 39 | pwm3.period_ms(20); |
| jiteshg | 2:bf817b299c19 | 40 | pwm3.write(0); |
| jiteshg | 2:bf817b299c19 | 41 | |
| jiteshg | 2:bf817b299c19 | 42 | //Setting the period and duty cycle for Servo motors |
| jiteshg | 0:fd080fb55bae | 43 | pwm1.period_ms(20); |
| jiteshg | 2:bf817b299c19 | 44 | pwm2.period_ms(20); |
| jiteshg | 2:bf817b299c19 | 45 | pwm1.write(0); |
| jiteshg | 2:bf817b299c19 | 46 | pwm2.write(0); |
| jiteshg | 1:8c73948a0864 | 47 | |
| jiteshg | 1:8c73948a0864 | 48 | while(1){ |
| prithviganeshk | 7:3bed3e005200 | 49 | printf("Frequency:-%f\n", frequency); |
| prithviganeshk | 7:3bed3e005200 | 50 | //char c = pc.getc(); |
| prithviganeshk | 7:3bed3e005200 | 51 | //int val = c - 48; |
| prithviganeshk | 7:3bed3e005200 | 52 | float adc_val = button.read(); |
| prithviganeshk | 7:3bed3e005200 | 53 | int val = getState(adc_val); |
| prithviganeshk | 7:3bed3e005200 | 54 | pc.printf("Destionation Floor :%d\n",val); |
| prithviganeshk | 7:3bed3e005200 | 55 | pc.printf("Current Floor : %d\n",currentState); |
| jiteshg | 4:acb62dee5ba9 | 56 | wait(1); |
| jiteshg | 2:bf817b299c19 | 57 | if(val==currentState){ |
| jiteshg | 2:bf817b299c19 | 58 | pwm3.write(0); |
| jiteshg | 2:bf817b299c19 | 59 | } |
| jiteshg | 2:bf817b299c19 | 60 | else if(val > currentState){ |
| jiteshg | 4:acb62dee5ba9 | 61 | closeGate(); //Close gate |
| jiteshg | 2:bf817b299c19 | 62 | //Move Up |
| jiteshg | 4:acb62dee5ba9 | 63 | dc1 = 0; |
| jiteshg | 4:acb62dee5ba9 | 64 | dc2 = 1; |
| prithviganeshk | 7:3bed3e005200 | 65 | pwm3.write(0.6); |
| jiteshg | 4:acb62dee5ba9 | 66 | //wait(2); |
| jiteshg | 4:acb62dee5ba9 | 67 | pc.printf("Floor Frequency value:%d\n",fvalues[val]); |
| jiteshg | 4:acb62dee5ba9 | 68 | while(1){ |
| prithviganeshk | 7:3bed3e005200 | 69 | if(((frequency > (fvalues[val] - 20)) && (frequency < (fvalues[val] + 20)))){ |
| prithviganeshk | 7:3bed3e005200 | 70 | currentState = val; |
| jiteshg | 4:acb62dee5ba9 | 71 | break; |
| jiteshg | 4:acb62dee5ba9 | 72 | } |
| prithviganeshk | 7:3bed3e005200 | 73 | else{ |
| prithviganeshk | 7:3bed3e005200 | 74 | printf("current freq: %f\n",frequency); |
| prithviganeshk | 7:3bed3e005200 | 75 | } |
| jiteshg | 4:acb62dee5ba9 | 76 | } |
| jiteshg | 4:acb62dee5ba9 | 77 | pwm3.write(0); |
| jiteshg | 4:acb62dee5ba9 | 78 | openGate(); |
| prithviganeshk | 7:3bed3e005200 | 79 | |
| jiteshg | 4:acb62dee5ba9 | 80 | }else{ |
| jiteshg | 4:acb62dee5ba9 | 81 | closeGate(); //Close gate |
| jiteshg | 4:acb62dee5ba9 | 82 | //Move Down |
| jiteshg | 2:bf817b299c19 | 83 | dc1 = 1; |
| jiteshg | 2:bf817b299c19 | 84 | dc2 = 0; |
| prithviganeshk | 7:3bed3e005200 | 85 | pwm3.write(0.6); |
| jiteshg | 4:acb62dee5ba9 | 86 | //wait(2); |
| jiteshg | 4:acb62dee5ba9 | 87 | pc.printf("Floor Frequency value:-%d\n",fvalues[val]); |
| jiteshg | 4:acb62dee5ba9 | 88 | //while(!((frequency > (fvalues[val] - 50)) && (frequency < (fvalues[val] + 50)))); |
| jiteshg | 4:acb62dee5ba9 | 89 | while(1){ |
| prithviganeshk | 7:3bed3e005200 | 90 | if(((frequency > (fvalues[val] - 20)) && (frequency < (fvalues[val] + 20)))){ |
| prithviganeshk | 7:3bed3e005200 | 91 | currentState = val; |
| jiteshg | 4:acb62dee5ba9 | 92 | break; |
| jiteshg | 4:acb62dee5ba9 | 93 | } |
| prithviganeshk | 7:3bed3e005200 | 94 | else{ |
| prithviganeshk | 7:3bed3e005200 | 95 | printf("current freq: %f\n",frequency); |
| prithviganeshk | 7:3bed3e005200 | 96 | } |
| jiteshg | 4:acb62dee5ba9 | 97 | } |
| jiteshg | 2:bf817b299c19 | 98 | pwm3.write(0); |
| jiteshg | 4:acb62dee5ba9 | 99 | openGate(); |
| jiteshg | 1:8c73948a0864 | 100 | } |
| prithviganeshk | 7:3bed3e005200 | 101 | //currentState = val; |
| jiteshg | 1:8c73948a0864 | 102 | } |
| jiteshg | 2:bf817b299c19 | 103 | } |
| jiteshg | 2:bf817b299c19 | 104 | |
| jiteshg | 2:bf817b299c19 | 105 | void openGate(){ |
| jiteshg | 2:bf817b299c19 | 106 | pwm1.write(0.0375); // 3.75% duty cycle - Open the gate |
| prithviganeshk | 6:625384a34dd5 | 107 | pwm2.write(0.1125); |
| prithviganeshk | 6:625384a34dd5 | 108 | //start_pwm2(1); // 11.25% duty cycle - Open the gate |
| prithviganeshk | 6:625384a34dd5 | 109 | wait(2); |
| jiteshg | 2:bf817b299c19 | 110 | pwm1.write(0); // Stop |
| prithviganeshk | 6:625384a34dd5 | 111 | pwm2.write(0); |
| jiteshg | 0:fd080fb55bae | 112 | } |
| jiteshg | 2:bf817b299c19 | 113 | |
| jiteshg | 2:bf817b299c19 | 114 | void closeGate(){ |
| jiteshg | 2:bf817b299c19 | 115 | pwm1.write(0.1125); // 11.25% duty cycle - Close the gate |
| prithviganeshk | 6:625384a34dd5 | 116 | pwm2.write(0.0375); |
| prithviganeshk | 6:625384a34dd5 | 117 | //start_pwm2(0); // 3.75% duty cycle - Close the gate |
| prithviganeshk | 6:625384a34dd5 | 118 | wait(2); |
| jiteshg | 2:bf817b299c19 | 119 | pwm1.write(0); // Stop |
| prithviganeshk | 6:625384a34dd5 | 120 | pwm2.write(0); |
| prithviganeshk | 6:625384a34dd5 | 121 | //start_pwm2(0); // 3.75% duty cycle - Close the gate |
| jiteshg | 3:66a52943c525 | 122 | } |
| jiteshg | 3:66a52943c525 | 123 | |
| jiteshg | 3:66a52943c525 | 124 | int getState(float adc_val){ |
| prithviganeshk | 7:3bed3e005200 | 125 | int state = 1; |
| prithviganeshk | 7:3bed3e005200 | 126 | |
| jiteshg | 3:66a52943c525 | 127 | if(adc_val > 0.15 && adc_val < 0.25){ |
| prithviganeshk | 7:3bed3e005200 | 128 | state = 2; |
| prithviganeshk | 7:3bed3e005200 | 129 | printf("%d\n",state); |
| jiteshg | 3:66a52943c525 | 130 | } |
| jiteshg | 3:66a52943c525 | 131 | else if(adc_val > 0.35 && adc_val < 0.45){ |
| prithviganeshk | 7:3bed3e005200 | 132 | state = 1; |
| prithviganeshk | 7:3bed3e005200 | 133 | printf("%d\n",state); |
| jiteshg | 3:66a52943c525 | 134 | } |
| jiteshg | 3:66a52943c525 | 135 | else if(adc_val > 0.55 && adc_val < 0.65){ |
| jiteshg | 3:66a52943c525 | 136 | state = 3; |
| prithviganeshk | 7:3bed3e005200 | 137 | printf("%d\n",state); |
| jiteshg | 3:66a52943c525 | 138 | } |
| jiteshg | 3:66a52943c525 | 139 | else if(adc_val > 0.75 && adc_val < 0.85){ |
| jiteshg | 3:66a52943c525 | 140 | state = 4; |
| prithviganeshk | 7:3bed3e005200 | 141 | printf("%d\n",state); |
| jiteshg | 3:66a52943c525 | 142 | } |
| jiteshg | 3:66a52943c525 | 143 | else if(adc_val > 0.95 && adc_val < 1.05){ |
| jiteshg | 3:66a52943c525 | 144 | state = 5; |
| prithviganeshk | 7:3bed3e005200 | 145 | printf("%d\n",state); |
| jiteshg | 3:66a52943c525 | 146 | } |
| prithviganeshk | 7:3bed3e005200 | 147 | |
| jiteshg | 3:66a52943c525 | 148 | return state; |
| jiteshg | 4:acb62dee5ba9 | 149 | } |
| jiteshg | 4:acb62dee5ba9 | 150 | |
| jiteshg | 4:acb62dee5ba9 | 151 | void rising(){ |
| jiteshg | 4:acb62dee5ba9 | 152 | begin = timer.read_us(); |
| jiteshg | 4:acb62dee5ba9 | 153 | flag = true; |
| jiteshg | 4:acb62dee5ba9 | 154 | } |
| jiteshg | 4:acb62dee5ba9 | 155 | void falling(){ |
| jiteshg | 4:acb62dee5ba9 | 156 | if(flag == true){ |
| jiteshg | 4:acb62dee5ba9 | 157 | end = timer.read_us(); |
| jiteshg | 4:acb62dee5ba9 | 158 | frequency = 500000/(end-begin); |
| jiteshg | 4:acb62dee5ba9 | 159 | begin = 0; |
| jiteshg | 4:acb62dee5ba9 | 160 | end = 0; |
| jiteshg | 4:acb62dee5ba9 | 161 | flag = false; |
| jiteshg | 4:acb62dee5ba9 | 162 | } |
| prithviganeshk | 6:625384a34dd5 | 163 | } |