Arduino互动设计入门

实例 3.7 不能说的秘密

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

  2. 原理图 略

  3. PCB 略

  4. ArduBlock


    image.png
  5. Arduino

#include <EEPROM.h>

/********************************************************
 * 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;
}

/*******************************************************************
 * 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]); 
}


void setup()
{
  Serial.begin(9600);
  Serial.print("Read from Arduino");
  Serial.println();

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

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

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

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

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

  delay( 1000 );

  eepromWriteInt( 10 , 1 ) ;

  eepromWriteInt( 11 , 2 ) ;

  eepromWriteInt( 12 , 3 ) ;

  eepromWriteInt( 13 , 4 ) ;

  eepromWriteInt( 14 , 5 ) ;

}

void loop()
{
}
上一篇 下一篇

猜你喜欢

热点阅读