Jingyuan Dong / Mbed 2 deprecated LoopFinal

main.cpp

Committer:
guruimage
Date:
2010-12-01
Revision:
0:43b0efef866f

File content as of revision 0:43b0efef866f:


#include "mbed.h"
//input from sensor function work well!!!!!
#define numsamples 1

int twoSensor(void);
void HostConfig(void);
void sensorInput(void);

AnalogIn input1(p20);
DigitalIn charger(p19);
DigitalOut ground(p18);


AnalogIn input2(p17);
DigitalIn charger2(p16);
DigitalOut ground2(p15);

Serial pc(USBTX, USBRX); // tx, rx

int ConfigReady =0;
int Count = 0;
int data[5] ={3,3,3,3,3};
char input[100];
int i = 0;
int sensor1EN=0;
int match=0;
int sensor2EN=0;
int num=0;
int off=0;
int errorON=0;
char config[100];
char temp;

int startIn=1;
int output=0;


int main() {
for (i=0;i<100;i++)
{
    input[i]='4';
    config[i]='5';
}
     
 while(1) {        
 
        HostConfig();
        sensorInput();        
        int match=1;
        for(i=0;i<Count;i++){
           
            if(input[i]==config[i]){
                match=match*1;          
            }
            else{
                match=match*0;
            }     
        }
        if (num>0){
            output=1;
        }else{
            output=0;
        }
        if((match==1)&&(output==1)&&(num==Count)){
                pc.printf("MATCH!!");
                num=0;
                
            }
        for(i=0;i<num;i++){
            if(input[i]!=config[i]){
                pc.printf("Your input does NOT MATCH! Please enter your input again!\r\n");
                   
                num=0;
            }
        }
        wait(0.005);
    }
}


void HostConfig(){
    while(pc.readable()){
       
        temp=pc.getc();      
        if (temp == 'S'){
            ConfigReady=1;
            Count=0;           
        }
        if (temp=='E'){
            ConfigReady=0;
             pc.printf("You have input a string:");
            for(i=0;i<Count;i++){
                pc.printf("%c",config[i]);
            }
            pc.printf("! Please enter the %dbit sensor input!\r\n", Count);    
        }
        
        if ((ConfigReady==1)&&((temp=='1')||(temp=='0')||(temp==' ')||(temp=='S'))){     
            if((temp=='1')||(temp=='0')){
                config[Count]=temp;
                Count++;
            }
        }else if(ConfigReady==1){
                pc.printf("HOST ERROR!\r\n");
            }     
    }
}


void sensorInput(void){
    
       for (i=0;i<=3;i++){
            data[i]=data[i+1];
        }
        data[4]=twoSensor();
        off=1;
        for (i=0;i<=4;i++){
            if (data[i]==3){
                off=off*1;
            }else{
                off=off*0;
                if(data[i]==2){
                    errorON=1;
                    sensor1EN=0;
                    sensor2EN=0;
                }
                if(data[i]==0){
                    if(errorON==0){
                        sensor1EN=1;
                    }
                }
                if(data[i]==1){
                    if(errorON==0){
                        sensor2EN=1;
                    }
                }
            }
        }
        
        if((off==1)&&(sensor1EN==1)){
            input[num]='0';
                        pc.printf("You have input a 0!\r\n");
            sensor1EN=0;
            num++;     
        }
        if((off==1)&&(sensor2EN==1)){
            input[num]='1';
                         pc.printf("You have input a 1!\r\n");
            sensor2EN=0;
            num++;
        }
        if ((off==1)&&(errorON==1)){
            pc.printf("TOUCH ERROR!\r\n");
            errorON=0;          
        }
        if(num==99){
            num=0;
        }
}

int twoSensor (void)
{
     float sample1;
     float sample2;
     ground=0;
     charger.mode(PullUp);
     charger.mode(PullNone);
     sample1=input1.read();
     
     ground2=0;
     charger2.mode(PullUp);
     charger2.mode(PullNone);
     sample2=input2.read();
    
     if ((sample1<0.4)&&(sample2>=0.4)){
        return 0;
     }
     else if ((sample2<0.4)&&(sample1>=0.4)){
        return 1;
     }
     else if ((sample2<0.4)&&(sample1<0.4)){
        return 2;
     }
     else if((sample1>=0.4)&&(sample2>=0.4)){
        return 3;
     }
}