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