아두이노

도어록

내동 2017. 3. 9. 05:51


도어록 스케치

트랜지스터: Tr: FGP30N06

자석 도어록 12 V Solenoid

 char keys[rows] [cols] = {


#iinclude<Keypad.h>

#iinclude<EEPROM.h>

char * secretCode = "1234";

int  Position = 0;

const byte rows =4;

const byte cols = 3;

 char keys[rows] [cols] = {

   { '1', '2','3'},

   {'4',  '5', '6'},

  { '7', '8', '9' },

  { '*', '0', ' #' }

};

byte rowPins[ rows] = { 3,8,7,5};

byte colPins[cols] = {6,2,4};

Keypad Keypad = Keypad(makekeymap(keys),rowPins,colPins,rows,cols);

int redPin = 13;

int greenPin = 12;

int solenoidPin = 10;



void setup() {

pinMode( redPin, OUTPUT);

pinMode( greenPin, OUTPUT);

pinMode( solenoidPin, OUTPUT);

loadCode();

flash();

lock();

Serial.begin(9600) ;

while( ! Serial);

Serial.print( " code  is  :  " );

Serial.println(secretCode);

Serial.println( "change Code : cNNN"  ) ;

Serial.println ( " unlock  : u " );

Serial.println (  " lock : l " );

}



void loop()

{

    if ( Serial.available() )

     {

        char  c = Serial.read() ;

         if ( c ==  'u')

            {

               unlock() ;

            }

        if ( c == 'l' )

           {

              lock();

            }

        if ( c == 'c')

          {

            getNewCode();

           }

       char key = Keypad.getkey() ;

            if ( key == ' #' )

            {

               lock() ;

             if ( key == SecretCode [position] )

               {

                       position++ ;

                }

              else if ( key ! = 0 )

                {

                     lock();

                 }

              if ( position == 4 )

                {

                   unlock() ;

                 }

                  delay ( 100) ;

          }



      void lock ()

        {

             position = 0 ;

          digitalWrite(redPin, HIGH );

          digitalWrite(greenPin, LOW );

          digitalWrite(solenoidPin, LOW );

          Serial.println(  " Locked  " );

         }



      void unlock()

      {

          digitalWrite(redPin, LOW );

          digitalWrite(greenPin, HIGH );

          digitalWrite(solenoidPin, HIGH);

          Serial.println(  " UNLocked  " );

           delay(5000);

          lock();

         }



        void  getNewCode()

      {

         for( int  i=o;  i<4 ; i ++)

           {

                char ch = Serial.read() ;

                SecretCode[i]  = ch;

             }

           SaveCode();

          flash() ;   flash();

           Serial.print( " code change to  :  ");

           Serial.println ( SecretCode) ;

        }


        

       void loadCode()

        {

             if ( EEPROM.read(0 )  == 1 )

              {

                 SecretCode[ 0 ] = EEPROM.read ( 1 );

                 SecretCode[ 1 ] = EEPROM.read ( 2 );

                 SecretCode[ 2 ] = EEPROM.read ( 3 );

                 SecretCode[ 3 ] = EEPROM.read ( 4 );

                }

          }


       void saveCode ()

        {

               EEPROM.write ( 1, SecretCode [0]) ;

               EEPROM.write ( 2, SecretCode [1]) ;  

               EEPROM.write ( 3, SecretCode [2]) ;  

               EEPROM.write ( 4, SecretCode [3]) ;

               EEPROM.write ( 0,   1 );

         }

         

        void flash()

           {

              digitalWrite(redPin, HIGH);  

              digitalWrite(greenPin, HIGH); 

             delay(500);

              digitalWrite(redPin, LOW); 

              digitalWrite(greenPin, LOW);  

            }

'아두이노' 카테고리의 다른 글

PID제어기에의한 헬리콥터모터 제어  (0) 2017.06.11
PID제어기에의한 모터 제어  (0) 2017.06.11
메모리 큐브  (0) 2017.02.05
KM-757메뉴얼  (0) 2017.02.01
500W 서보모터   (0) 2017.02.01