Arduino互动设计入门

实例 3.8 秘密锁

2018-11-18  本文已影响12人  chenzhenlindx
  1. 面包板


    image.png
  2. 原理图


    image.png
  3. PCB 略
  4. ArduBlock


    ardublock.png
  5. Arduino
#include <EEPROM.h>

/*******************************************************************
 * A way to write an 'int' (2 Bytes) to EEPROM 
 * EEPROM library natively supports only bytes. 
 * Note it takes around 8ms to write an int to EEPROM 
 *******************************************************************/
void eepromWriteInt(int address, int value){
  union u_tag {
    byte b[2];        //assumes 2 bytes in an int
    int INTtime;
  } 
  time;
  time.INTtime=value;

  EEPROM.write(address  , time.b[0]); 
  EEPROM.write(address+1, time.b[1]); 
}

/********************************************************
 * A way to read int (2 Bytes)from EEPROM 
 * EEPROM library natively supports only bytes
 ********************************************************/
int eepromReadInt(int address){

  union u_tag {
    byte b[2];
    int INTtime;
  } 
  time;
  time.b[0] = EEPROM.read(address);
  time.b[1] = EEPROM.read(address+1);
  return time.INTtime;
}

int _ABVAR_1_PIN2 = 0 ;
int _ABVAR_2_PIN3 = 0 ;
int _ABVAR_3_PIN4 = 0 ;
int _ABVAR_4_PIN5 = 0 ;
int _ABVAR_5_Total = 0 ;

void setup()
{
  pinMode( 2 , INPUT);
  pinMode( 3 , INPUT);
  pinMode( 4 , INPUT);
  pinMode( 5 , INPUT);
  Serial.begin(9600);
  eepromWriteInt( 20 , 9 ) ;

  Serial.print("SAVE OK!");
  Serial.println();

  delay( 5000 );

  Serial.print(eepromReadInt( 20 ) );
  Serial.println();

}

void loop()
{
  if (digitalRead(2))
  {
    _ABVAR_1_PIN2 = 1 ;
  }
  else
  {
    _ABVAR_1_PIN2 = 0 ;
  }
  if (digitalRead(3))
  {
    _ABVAR_2_PIN3 = 1 ;
  }
  else
  {
    _ABVAR_2_PIN3 = 0 ;
  }
  if (digitalRead(4))
  {
    _ABVAR_3_PIN4 = 1 ;
  }
  else
  {
    _ABVAR_3_PIN4 = 0 ;
  }
  if (digitalRead(5))
  {
    _ABVAR_4_PIN5 = 1 ;
  }
  else
  {
    _ABVAR_4_PIN5 = 0 ;
  }
  _ABVAR_5_Total = ( ( _ABVAR_1_PIN2 + _ABVAR_2_PIN3 ) + ( _ABVAR_3_PIN4 + _ABVAR_4_PIN5 ) ) ;
  if (( ( _ABVAR_5_Total ) == ( eepromReadInt( 20 )  ) ))
  {
    Serial.print("Welcome!");
    Serial.println();
  }
  else
  {
    Serial.print("Error!");
    Serial.println();
  }
  delay( 5000 );
}
上一篇 下一篇

猜你喜欢

热点阅读