attempt 1

Dependencies:   mbed

main.cpp

Committer:
jaredwil
Date:
2015-03-20
Revision:
0:fda0081e634b
Child:
1:54ae0e3ae1b7

File content as of revision 0:fda0081e634b:

#include "mbed.h"
#include <map>

//elevator I/O
BusOut leds(LED1,LED2,LED3,LED4);
PwmOut DC(p21);
PwmOut servo1(p22);
PwmOut servo2(p23);
AnalogIn  irIN(p16);

Serial pc(USBTX, USBRX);


//itizilize digital I/O for keypad
DigitalOut row1(p24);
DigitalOut row2(p25);
DigitalIn col1(p20);
DigitalIn col2(p19);
DigitalIn col3(p18);

//timer for row switch
Timer t1;
Timer irT;
//init keypad map
map<int, int> keypadMap;

void init(){
        keypadMap[0x11]= 0x1;
        keypadMap[0x12]= 0x2;
        keypadMap[0x14]= 0x4;
        keypadMap[0x18]= 0x0;
        keypadMap[0x21]= 0x8;
        keypadMap[0x22]= 0x0;
        keypadMap[0x24]= 0x0;
        keypadMap[0x28]= 0x0;
        }

int main() {
        
    init();
    //Initialize rows to 0 and keypad stuff
    row1 = 0;
    row2 = 0;
    t1.start();
    int cur_input = 0x00;
    int prev_input = 0x00;
    float irPeriod = 0; 
    
    int nofloor = 0;   //set nofloor state to 0

    //initialize servo and dc motor
    float freq = 50;
    DC.period(1/freq);               // set PWM period to user specified for high tone
    DC=0.4;                         // set duty cycle to 50%
    servo1.period(1/freq);               // set PWM period to user specified for high tone
    servo1=0.0375;                         // set duty cycle to 50%
    servo2.period(1/freq);               // set PWM period to user specified for high tone
    servo2=0.1125;                         // set duty cycle to 50%
    
    while(1) {
 
/////////////////////////////floor detection//////////////////////////////////////


//floor detected
if(irIN > 0.95){
    irT.start();       //start IRfloor timer
    nofloor = 0;   //set nofloor state to 0
    }
else if(irIN < 0.5 && nofloor == 0){ 
    irT.stop();                       //stop timer     
    irPeriod = irT.read_us();    //read time 
    irT.reset();                   //reset
    nofloor = 1;                      //no current floor detcted
    //make floor decision based on irPeriod
    pc.printf("Detected Period: %f val %f\r",irPeriod,irIN.read());

    }


 
/////////////Below contains all code in order to detect current input from keypad////////////////////////////       
        if(prev_input == 0){

            row1 = 0;
            row2 = 0;
            //Keep each pin on for 4ms
            //"turn on" appropriat bit in map when on.
           switch(t1.read_ms()%8){
                            case 0:
                            row1 = 1;
                            cur_input |= 0x10;   
                            break;
                            case 4:
                            row2 = 1;
                            cur_input |= 0x20;
                            break;
                            }
                }
        
            //Check each colum to see if it is high
            //if it is "turn on" that respective bit
             if(col1 == 1)
             cur_input |= 0x01;
             else if(col2 == 1)
             cur_input |= 0x02;
             else if(col3 == 1)
             cur_input |= 0x04;
             else cur_input = 0;    //Detect button release or no input set input to 0
        
            leds = keypadMap[cur_input];
            //Maintain the past input
            prev_input = cur_input;
/////////////////////////////////////////END KEYPAD//////////////////////////////////////////////////////////            
            
            
            } 
       
    
}