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.
main.cpp
- Committer:
- sarakarimi
- Date:
- 2014-12-10
- Revision:
- 0:e6c4994727c2
- Child:
- 1:857e1fecbe35
File content as of revision 0:e6c4994727c2:
#include "mbed.h"
#include <string>
using namespace std;
DigitalOut R1 (D6);
DigitalOut R2 (D7);
DigitalOut R3 (D8);
DigitalOut R4 (D9);
InterruptIn C1 (D10);
DigitalIn C2 (D11);
DigitalIn C3 (D12);
DigitalOut keyled(LED1);
DigitalOut flash(LED4);
AnalogIn ain(PTB0);
Timer t;
float tm ;
char key;
int s=0;
float value_adc =0.0f;
int dig_value = 0;
unsigned char arrkey[12]={ '1' , '2' , '3' ,
'4' , '5' , '6' ,
'7' , '8' , '9' ,
'*' , '0' , '#' };
void unlock (){
printf("DOOR UNLOCK: Welcome!\n\r");
}
void lock (){
printf("DOOR LOCK\n\r");
}
unsigned char char_string[4]= {'*' ,'*' ,'*' ,'*'};
unsigned char password[4]={'1' , '2' , '3' , '4'};
unsigned char passwordA[4]={'4' , '5' , '6' , '7'};
unsigned char keypad(){
unsigned char r,b;
while(1){
for (r=0; r<4; r++){
b=4;
R1 = 1; R2 = 1 ; R3 = 1 ; R4 = 1;
if (r == 0) R1 = 0;
if (r == 1) R2 = 0;
if (r == 2) R3 = 0;
if (r == 3) R4 = 0;
if(C1==0) b=0;
if(C2==0) b=1;
if(C3==0) b=2;
if ((!(b==4))){
key=arrkey[(r*3)+b];
while(C1==0);
while(C2==0);
while(C3==0);
wait(0.2);
char_string[s] = key;
s++;
return key;
}
}
}
}
void handleKeypad (){
//C1.disable_irq ();
t.start();
int firsttime = 0;
keyled = 0;
if (firsttime == 0)
s = -1;
while (1){
//tm = t.read();
key = keypad();
if (s == 4){
//t.reset();
firsttime = 1;
printf("Password: ");
for (int i = 0 ; i < 4 ; i++)
printf("%c", char_string[i]);
printf("\n\r");
s = 0;
int j = 0;
int matchSara = 1;
while (j < 4){
if (char_string [j] == password[j])
j++;
else{
matchSara = 0;
break;
}
}
int k = 0;
int matchAmir = 1;
while (k < 4){
if (char_string [k] == passwordA[k])
k++;
else{
matchAmir = 0;
break;
}
}
if (matchSara == 1){
printf("MATCH Sara\n\r");
unlock();
t.reset();
break;
}
else if (matchAmir == 1){
printf("MATCH Amir\n\r");
unlock();
t.reset();
break;
}
else{
printf("WRONG\n\r");
printf("lock for wrong password\n\r");
t.reset();
break;
//t.reset();
}
}
if (t.read() > 7){
lock();
t.stop();
t.reset();
break;
} // end timer
} // end while
}
void TestIntrpt(){
int counter = 0;
t.start();
while (1){
counter++ ;
keyled = 1;
wait(0.2);
keyled = 0;
wait(0.2);
// tm = t.read();
if (t.read() > 5){
keyled = 1;
t.stop();
t.reset();
C1.enable_irq();
break;
}
}
}
int main() {
keyled = 1;
C1.fall(&handleKeypad);
while(1) { // wait around, interrupts will interrupt this!
value_adc = ain.read();
printf("Analog %5.2f\n\r",value_adc);
if (value_adc < 0.2){
printf("Nobody is here!\n\r");
}
else {
printf("Somebody is here!\n\r");
}
wait(2);
//lcd.cls();
}
//TestIntrpt();
//handleKeypad();
}