Lab3

Dependencies:   mbed-rtos mbed

Fork of ESE519_Lab3_EC_v2 by ese519

main.cpp

Committer:
jfields
Date:
2015-10-03
Revision:
0:015087d61ca1
Child:
1:e79ac0826624

File content as of revision 0:015087d61ca1:

#include "mbed.h"

Serial pc(USBTX,USBRX);
// functions
void find_keys();
Timer t;

// init board
PwmOut dc_motor(p26);
PwmOut servo1(p25);
PwmOut servo2(p24);
DigitalOut in1(p14);    // elevator direction
DigitalOut in2(p13);    // elevator direction
AnalogIn IRSensor(p16);      // read IR sensor
//AnalogOut test(p18);

// keyboard read
DigitalIn col1(p20);
DigitalIn col2(p19);
DigitalIn col3(p18);
DigitalOut row1(p30);
DigitalOut row2(p27);

DigitalOut ind1(LED1);
DigitalOut ind2(LED2);
DigitalOut ind3(LED3);
DigitalOut ind4(LED4);


// init vars
float ADC_Voltage;
float period = 0.02;    // sec
float dty_dc = 0.75;    // PCT [0-100]
float dty_servo_open = 0.1125;  // PCT [3.75-11.25]
float dty_servo_close = 0.0375; // PCT [3.75-11.25]
int cur_floor = 1;
int desired_floor = 1;
int tperiod;

int main() {
    
    // set period (constant)
    dc_motor.period(period);
   // servo1.period(period);
   // servo2.period(period);
    
    // start with closed doors
   // servo1.write(dty_servo_close);
    //servo2.write(dty_servo_close);

    // run elevator program
    while(1) {
        
        
        // check for key press
        find_keys();
        
        // check if need to move
        if (cur_floor != desired_floor) {
            
            // determine direction
            if (cur_floor > desired_floor) {    // move down
                in1 = 0;
                in2 = 1;
            }
            else {  // move up
                in1 = 1;
                in2 = 0;
            }
            
            // start car
            dc_motor.write(dty_dc);
            
            // check IR sensors
            
            while (cur_floor != desired_floor) {
                // check IR emitter here!
                
                while(!ADC_Voltage)
                    ADC_Voltage = IRSensor.read();
            
                t.start();
            
                while(ADC_Voltage)
                    ADC_Voltage = IRSensor.read();
           
                t.stop();
        
                tperiod = 2 * t.read_us();
        
                t.reset();
        
            if(tperiod > 9900 && tperiod < 10100)
                cur_floor = 1;
            else if(tperiod > 3900 && tperiod < 4100)
                cur_floor = 2;
            else if(tperiod > 1900 && tperiod < 2100)
                cur_floor = 3;
            else if(tperiod > 1300 && tperiod < 1500)
                cur_floor = 4;
            else if(tperiod > 900 && tperiod < 1100)
                cur_floor = 5;
                
            //pc.printf("%d", cur_floor);
            //wait_ms(1000);
            }
        }
            
            // stop car 
            dc_motor.write(0);
                      
      //  }
        
        // open door
        
      //  servo1.write(dty_servo_open);
       // servo2.write(dty_servo_open);
        
         
    } 
    
     
}

void find_keys() {
    
    // check row 1
    row1 = 0;
    row2 = 1;
    if (!col1) {
        desired_floor = 1;    
    }
    if (!col2) {
        desired_floor = 2;    
    }
    if (!col3) {
        desired_floor = 3;    
    }
    
    // check row 2
    row1 = 1;
    row2 = 0;
    if (!col1) {
        desired_floor = 4;
    }
    
}