Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma config OSC=INTIO2, WDT=OFF, DEBUG=ON
- #include <p18F1330.h>
- #include <delays.h>
- #include <adc.h>
- #define GREEN_LED PORTBbits.RB5 //Define green LED as pin 11
- #define RED_LED PORTBbits.RB4 //Define red LED as pin 10
- #define PIEZO PORTAbits.AN0 //Define PIEZO as pin 1
- #define SWITCH PORTBbits.RB1 //Define SWITCH as pin 9
- #define MOTOR PORTBbits.RB0 //Define MOTOR as pin 8
- #define MAX_KNOCKS 8 //Maximum number of knocks allowed
- unsigned char knock; //Knock in progress
- unsigned char CorrectKnock; //Variable for if knock matches secret knock
- unsigned char i;
- unsigned char x;
- unsigned char T; //Time variable
- unsigned char KD; //Knock Time variable
- unsigned char piezo; //Value returned from A/D converter
- unsigned char start; //Knock started?
- unsigned char stop; //Knock stopped?
- unsigned char ProgramStart; //Programming knock started?
- unsigned char ProgramStop; //Programming knock stopped?
- unsigned int KnockNumber; //Number of knocks
- unsigned int ProgramNumber; //Number of programmed knocks
- int KnockPattern[MAX_KNOCKS]; //Knock time array
- int ProgrammedPattern[MAX_KNOCKS]; //Program knock time array
- void unlock_door(void) //Unlock The Door
- {
- MOTOR=1; //Turn on motor to unlock door
- Delay10KTCYx(200); //Leave on long enought to unlock
- MOTOR=0; //Turn off motor
- }
- void Check_Patterns(void)
- {
- if(KnockPattern==ProgrammedPattern)
- {
- GREEN_LED=1;
- unlock_door();
- Delay10KTCYx(200);
- GREEN_LED=0;
- }
- else
- {
- for(i=0;i<4;i++)
- {
- RED_LED=1;
- Delay10KTCYx(200);
- RED_LED=0;
- Delay10KTCYx(200);
- }
- }
- }
- int GetPiezoValue(void) //Find Piezo Value
- {
- piezo=0; //Initialize variable
- ADCON0bits.GO=1;
- while(ADCON0bits.GO==1); //Wait until conversion is finished
- piezo=ADRESH<<8; //Return piezo value
- piezo=piezo+ADRESL;
- }
- void program_knock(void) //Program Secret Knock
- {
- T=0;
- while(knock!=1); //Wait until first knock is detected
- ProgramStop=0; //Start programming knock
- ProgramStart=1;
- ProgramNumber=0;
- while(ProgramStart==1) //While programming...
- {
- while(knock!=1) //Do this until another knock is heard
- {
- T++; //Time variable counts up
- KD++; //Increase knock delay time
- if(KD>=1000000) //If the delay is longer than 3 seconds...
- {
- ProgramStart=0; //...Knock is over
- ProgramStop=1;
- }
- }
- ProgrammedPattern[ProgramNumber]=T; //Send time value to program pattern array
- ProgramNumber++; //Increase number of program knocks
- KD=0; //Reset knock delay time
- }
- }
- void record_timing(void) //Listen For Knock (Record Timing Between Them)
- {
- T=0;
- while(knock!=1); //Wait until first knock is detected
- ProgramStop=0; //Start programming knock
- ProgramStart=1;
- KnockNumber=0;
- while(ProgramStart==1) //While programming...
- {
- while(knock!=1) //Do this until another knock is heard
- {
- T++; //Time variable counts up
- KD++; //Increase knock delay time
- if(KD>=1000000) //If the delay is longer than 3 seconds...
- {
- ProgramStart=0; //...Knock is over
- ProgramStop=1;
- Check_Patterns(); //Check to see if patterns match
- }
- }
- KnockPattern[KnockNumber]=T; //Send Time value to knock pattern array
- KnockNumber++; //Increase number of program knocks
- KD=0; //Reset knock delay time
- }
- }
- void main(void)
- {
- RED_LED=0;
- GREEN_LED=0;
- TRISA=0b00000100;
- TRISB=0b00000000;
- ADCON0bits.CHS1=0; //Set to channel 0 (AN0)
- ADCON0bits.CHS0=0;
- ADCON0bits.ADON=1; //Enable A/D converter
- ADCON1bits.VCFG0=0; //Voltage reference is VDD
- ADCON1bits.PCFG3=0; //Pin configured as RA6
- ADCON1bits.PCFG2=0; //Pin configured as RA4
- ADCON1bits.PCFG1=0; //Pin configured as RA1
- ADCON1bits.PCFG0=1; //Pin configured as AN0
- ADCON2bits.ADFM=1; //Right-justified
- ADCON2bits.ACQT2=1; //Acquisition time set to 20 TAD
- ADCON2bits.ACQT1=1;
- ADCON2bits.ACQT0=1;
- ADCON2bits.ADCS2=0; //Clock select set to FOSC/32
- ADCON2bits.ADCS1=1;
- ADCON2bits.ADCS0=0;
- knock=0; //Initialize variables
- start=0;
- stop=0;
- KnockNumber=0;
- while(1)
- {
- if(SWITCH==1)
- {
- while(SWITCH==1) //Check if we're programming
- {
- RED_LED=1; //Turn on red led to show that it is programming
- GetPiezoValue(); //Call function to check piezo
- if (1023>=piezo>=10) //When first knock is detected...
- {
- KnockNumber++; //increase number of sensed knocks
- start=1; //Knock has started
- knock=1; //Single knock in progress
- GREEN_LED=1; //Flash LED to signify knock detection
- Delay10KTCYx(100);
- GREEN_LED=0;
- knock=0; //Single knock ended
- program_knock(); //...call programming function...
- }
- }
- RED_LED=0;
- }
- else //If we're not programming...
- {
- GetPiezoValue(); //Call function to check piezo
- if (1023>=piezo>=10) //When first knock is detected...
- {
- KnockNumber++; //increase number of sensed knocks
- start=1; //Knock has started
- knock=1; //Single knock in progress
- GREEN_LED=1; //Flash LED to signify knock detection
- Delay10KTCYx(100);
- GREEN_LED=0;
- knock=0; //Single knock ended
- record_timing(); //Call timing function
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement