1

Тема: Кодовый замок

#define MASTER 345623
#define TIME_OPEN 3000

#include <iarduino_KB.h>  // https://github.com/tremaru/iarduino_KB.git
#include <TM1637_6_LED.h>
#include <EEPROM.h>
 TM1637_6_LED tm(9, 10); // CLK, DIO 
 iarduino_KB KB(8,7,6,5,4,3,2); /// 8765 432
 
 long code[8],ccc,i,i1,times,master,user;
 int start;
 bool w,w1,cod;
 
void setup(){
  Serial.begin(9600);
  pinMode(13,OUTPUT);// output
  pinMode(11,OUTPUT);// tone
  KB.begin(KB3); 
  tm.brig(1); // ЯРКОСТЬ 0...7   
  tm.print(0, 0, 0b01000000, 0b00111001, 0b01011100, 0b01011110, 0b01111001, 0b01000000);     
  EEPROM.get(0, user);
  }
 
void loop(){
  if(KB.check(KEY_DOWN )){
     if(KB.getNum==15){i=0;ccc=0;for(i1=0;i1<8;i1++){code[i]=0;}Serial.println(ccc); 
       tone(11, 400, 100);tm.print(ccc, 0, -1, -1, -1, -1, -1, -1);} // # стереть число
     if(KB.getNum==14&&cod==1){Serial.println(ccc);delay(200);cod=0;start=1;user=EEPROM.put(0, ccc);
     times=millis();w=1;tone(11, 400, 300);} // start master
     if(KB.getNum==14&&cod==0){Serial.println(ccc);delay(200);w1=1;start=1;tone(11, 400, 300);} // start user
//////////////////////////////////////////////////////////// 
     if(KB.getNum<10&&i<8){times=millis();w=1;
      code[i] = KB.getNum; tone(11, 500+code[i]*50, 200);
   switch(i){
     case 0: ccc = code[0];break;
     case 1: ccc = code[0]*10 + code[1];break;
     case 2: ccc = code[0]*100 + code[1]*10 + code[2];break;
     case 3: ccc = code[0]*1000 + code[1]*100 + code[2]*10 + code[3];break;
     case 4: ccc = code[0]*10000 + code[1]*1000 + code[2]*100 + code[3]*10 + code[4];break;
     case 5: ccc = code[0]*100000 + code[1]*10000 + code[2]*1000 + code[3]*100 + code[4]*10 + code[5];break;
    }i++; if(i>5){i=7;}delay(200);Serial.println(ccc); tm.print(ccc, 0, -1, -1, -1, -1, -1, -1);}
    }
////////////////////////////////////////////////////////////  
 if(millis()-times>5000&&w==1){w=0;
  i=0;ccc=0;w=0;for(i1=0;i1<8;i1++){code[i]=0;
  tm.print(0, 0, 0b01000000, 0b00111001, 0b01011100, 0b01011110, 0b01111001, 0b01000000);}
  Serial.println(ccc);} // -code-

 if(start==1&&ccc==MASTER){start=0;cod=1;i=0;ccc=0;w=0;for(i1=0;i1<8;i1++){code[i]=0;}
  tm.print(0, 0, 0b01000000, 0b01110110, 0b01110110, 0b01110110, 0b01110110, 0b01000000);
 } // -hhhh-
 
 if(start==1&&ccc==user&&w1==1){start=0;w1=0;digitalWrite(13,HIGH);
  tm.print(0, 0, 0b01000000, 0b00111111, 0b01110011, 0b01111001, 0b01010100, 0b01000000); 
  i=0;ccc=0;w=0;for(i1=0;i1<8;i1++){code[i]=0;}
  tone(11, 1000, 1000);delay(TIME_OPEN);digitalWrite(13,LOW);times=millis()-5000;w=1;}// -open-
  
 if(start==1&&ccc!=user&&w1==1){start=0;w1=0;
  times=millis()-4000;w=1;
  tm.print(0, 0, 0b01000000, 0b01000000, 0b01000000, 0b01000000, 0b01000000, 0b01000000);}// ------

      
}//loop