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
Diff: main.cpp
- Revision:
- 7:3bed3e005200
- Parent:
- 6:625384a34dd5
- Child:
- 8:7e3f92f0dc47
--- a/main.cpp Wed Oct 14 22:30:39 2015 +0000
+++ b/main.cpp Thu Oct 15 03:54:33 2015 +0000
@@ -1,10 +1,4 @@
-/*
-Using pwm to run a servo motor
-Connect the red wire of the servo motor to 3.3V and not 5V
-DC motor pins - p5 and p6 to control the direction of the motor
- p23 to control the duty cycle and period
-*/
#include "mbed.h"
PwmOut pwm1(p21); //Servo Motor-1 PWM channel
PwmOut pwm2(p22); //Servo Motor-2 PWM channel
@@ -17,14 +11,13 @@
InterruptIn event(p7); //Interrupt for rising and falling edge detection from IR
Timer timer; //Timer to read falling and rising edge time
-Timer timer2;
+
Serial pc(USBTX, USBRX);//Serial Communication
void openGate();
void closeGate();
void rising();
void falling();
-void start_pwm2(int x);
int getState(float adc_val);
@@ -33,7 +26,8 @@
float frequency = 0;
bool flag = false;
//int fvalues[] = {0,100,250,500,700,1000};
-int fvalues[] = {0,102,270,615,948,1623};
+int fvalues[] = {0,100,250,500,700,700};
+//int fvalues[] = {0,102,270,615,948,1623};
int main() {
timer.start();
@@ -52,13 +46,13 @@
pwm2.write(0);
while(1){
- //printf("Frequency:-%f\n", frequency);
- char c = pc.getc();
- int val = c - 48;
- //float adc_val = button.read();
- //int val = getState(adc_val);
- pc.printf("Floor-%d\n",val);
- pc.printf("CurrentState-%d\n",currentState);
+ printf("Frequency:-%f\n", frequency);
+ //char c = pc.getc();
+ //int val = c - 48;
+ float adc_val = button.read();
+ int val = getState(adc_val);
+ pc.printf("Destionation Floor :%d\n",val);
+ pc.printf("Current Floor : %d\n",currentState);
wait(1);
if(val==currentState){
pwm3.write(0);
@@ -68,41 +62,43 @@
//Move Up
dc1 = 0;
dc2 = 1;
- pwm3.write(0.1);
+ pwm3.write(0.6);
//wait(2);
pc.printf("Floor Frequency value:%d\n",fvalues[val]);
while(1){
- if(((frequency > (fvalues[val] - 2)) && (frequency < (fvalues[val] + 2)))){
+ if(((frequency > (fvalues[val] - 20)) && (frequency < (fvalues[val] + 20)))){
+ currentState = val;
break;
}
- //else{
- //printf("current freq: %f\n",frequency);
- //}
+ else{
+ printf("current freq: %f\n",frequency);
+ }
}
- //while(!((frequency > (fvalues[val] - 50)) && (frequency < (fvalues[val] + 50))));
pwm3.write(0);
openGate();
+
}else{
closeGate(); //Close gate
//Move Down
dc1 = 1;
dc2 = 0;
- pwm3.write(0.1);
+ pwm3.write(0.6);
//wait(2);
pc.printf("Floor Frequency value:-%d\n",fvalues[val]);
//while(!((frequency > (fvalues[val] - 50)) && (frequency < (fvalues[val] + 50))));
while(1){
- if(((frequency > (fvalues[val] - 2)) && (frequency < (fvalues[val] + 2)))){
+ if(((frequency > (fvalues[val] - 20)) && (frequency < (fvalues[val] + 20)))){
+ currentState = val;
break;
}
- //else{
- // printf("current freq: %f\n",frequency);
- //}
+ else{
+ printf("current freq: %f\n",frequency);
+ }
}
pwm3.write(0);
openGate();
}
- currentState = val;
+ //currentState = val;
}
}
@@ -126,22 +122,29 @@
}
int getState(float adc_val){
- int state = 0;
+ int state = 1;
+
if(adc_val > 0.15 && adc_val < 0.25){
- state = 1;
+ state = 2;
+ printf("%d\n",state);
}
else if(adc_val > 0.35 && adc_val < 0.45){
- state = 2;
+ state = 1;
+ printf("%d\n",state);
}
else if(adc_val > 0.55 && adc_val < 0.65){
state = 3;
+ printf("%d\n",state);
}
else if(adc_val > 0.75 && adc_val < 0.85){
state = 4;
+ printf("%d\n",state);
}
else if(adc_val > 0.95 && adc_val < 1.05){
state = 5;
+ printf("%d\n",state);
}
+
return state;
}
@@ -158,35 +161,3 @@
flag = false;
}
}
-
-void start_pwm2(int x){
-
- int duty1, duty2 = 0;
-
- //printf("Reached startpwm2\ 1 n");
- if(x == 1){
- duty1 = 250; // .25ms on period in us
- duty2 = 19750; // 19.75ms off period
- }
- else if(x == 0){
- //printf("Reached startpwm2 2 \n");
- duty1 = 2250; // 2.25ms on period
- duty2 = 17750; //17.75ms off period
- }
-
- timer2.start();
- int start1 = timer2.read_ms();
- printf("%d\n", start1);
-
- while((timer2.read_ms() - start1) < 2000){
- //printf("entered");
- pwm2 = 1;
- wait_us(duty1);
- pwm2 = 0;
- wait_us(duty2);
- }
-
- pwm2=0;
-
-
-}