1 2 //EEPROM数据保存---------------------- 3 #include <EEPROM.h> 4 #define EEPROM_write(address, p) {int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof(p); i++) EEPROM.write(address+i, pp[i]);} 5 #define EEPROM_read(address, p) {int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof(p); i++) pp[i]=EEPROM.read(address+i);} 6 // EEPROM_write(0, value); 7 // EEPROM_read(0,getValue); 8 unsigned long incomingByte = 0;// 定义无符号长整数型变量incomingByte初始值为0 9 //-------------------------------------- 10 11 12 //AD --------------------------------- 13 int keyboardPin = A1; //模拟口0 14 int keyboardValue = 0; // 取得键AD模拟分量 15 //-------------------------------------- 16 17 void setup() { 18 //通信波特率 19 Serial.begin(9600); 20 } 21 22 23 void loop() { 24 25 keyboardValue = analogRead(keyboardPin); // 读出端口AD模拟分量 26 Serial.println(keyboardValue); // 串口打印 27 28 //加 29 if ((keyboardValue >50) && (keyboardValue <200)) {incomingByte = incomingByte+1;} 30 //减 31 if ((keyboardValue >400) && (keyboardValue < 800)){incomingByte = incomingByte-1;} 32 33 EEPROM_write(0,incomingByte) //把incomingByte变量从0地址位开始写入EEPROM 34 35 EEPROM_read(0,incomingByte) //把incomingByte变量从0地址位开始读出EEPROM 36 Serial.println(incomingByte); 37 Serial.println("//--------------------------------"); 38 39 delay(1000); 40 41 } 42