RTES / Mbed 2 deprecated mbed_pwmLib

Dependencies:   mbed

main.cpp

Committer:
jiteshg
Date:
2015-10-14
Revision:
5:7b4575bf205e
Parent:
4:acb62dee5ba9
Child:
6:625384a34dd5

File content as of revision 5:7b4575bf205e:

/*
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

DigitalOut dc1(p5);     //DC motor input-1
DigitalOut dc2(p6);     //DC motor input-2
PwmOut pwm3(p23);       //DC motor PWM channel

AnalogIn button(p15);   //Analog input from the floor buttons
InterruptIn event(p7);  //Interrupt for rising and falling edge detection from IR

Timer timer;            //Timer to read falling and rising edge time
Serial pc(USBTX, USBRX);//Serial Communication

void openGate();
void closeGate();
void rising();
void falling();

int getState(float adc_val);

int currentState = 1;
int begin,end = 0;
float frequency = 0;
bool flag = false;
//int fvalues[] = {0,100,250,500,700,1000};
int fvalues[] = {0,102,270,615,948,1623};

int main() {
    timer.start();
    event.rise(&rising);
    event.fall(&falling);
    //Setting dc1 to high and dc2 to low initially
    dc1 = 0;
    dc2 = 1;
    pwm3.period_ms(20);
    pwm3.write(0);
    
    //Setting the period and duty cycle for Servo motors
    pwm1.period_ms(20);
    pwm2.period_ms(20);
    pwm1.write(0);
    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);
        wait(1);
        if(val==currentState){
            pwm3.write(0);
        }
        else if(val > currentState){
            closeGate(); //Close gate
            //Move Up
            dc1 = 0;
            dc2 = 1;
            pwm3.write(0.5);
            //wait(2);
            pc.printf("Floor Frequency value:%d\n",fvalues[val]);
            while(1){
                if(((frequency > (fvalues[val] - 2)) && (frequency < (fvalues[val] + 2)))){
                    break;
                }
                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.5);
            //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)))){ 
                    break;
                }
                else{
                    printf("current freq: %f\n",frequency);
                }
            }
            pwm3.write(0);
            openGate();
        }
        currentState = val;
    }
}

void openGate(){
    pwm1.write(0.0375); // 3.75% duty cycle - Open the gate
    pwm2.write(0.1125); // 11.25% duty cycle - Open the gate
    wait(2);            // 2 sec delay
    pwm1.write(0);      // Stop
    pwm2.write(0);      // Stop
    //wait(2);  
}

void closeGate(){
    pwm1.write(0.1125); // 11.25% duty cycle - Close the gate
    pwm2.write(0.0375); // 3.75% duty cycle - Close the gate
    wait(2);            // 2 sec delay
    pwm1.write(0);      // Stop
    pwm2.write(0);      // Stop  
    //wait(2);
}

int getState(float adc_val){
    int state = 0;
    if(adc_val > 0.15 && adc_val < 0.25){
        state = 1;
    }
    else if(adc_val > 0.35 && adc_val < 0.45){
        state = 2;
    }
    else if(adc_val > 0.55 && adc_val < 0.65){
        state = 3;
    }
    else if(adc_val > 0.75 && adc_val < 0.85){
        state = 4;
    }
    else if(adc_val > 0.95 && adc_val < 1.05){
        state = 5;
    }
    return state;           
}

void rising(){
    begin = timer.read_us();
    flag = true;
}
void falling(){
    if(flag == true){
        end = timer.read_us();
        frequency = 500000/(end-begin);
        begin = 0;
        end = 0;
        flag = false;
    }
}