#include "SerialCommand.h"
#include <avr/sleep.h>.
#include <avr/wdt.h>
#include <EEPROM.h>
#define VERSION "microFrameWork4Arduino / load EEPROM parameters "
#define LED_PIN 13
#define DEBUG 0
#if (DEBUG)
#define DEBUGP(x) Serial.print(x)
#define DEBUGN(x) Serial.println(x)
#else
#define DEBUGP(x)
#define DEBUGN(x)
#endif
SerialCommand SCmd; // The demo SerialCommand object
//HZ Control
#define HZ_SETTING 10
int mainLoop_count;
unsigned long fast_loopTimer; // Time in miliseconds of main control loop
const int hzCount = (1000 / HZ_SETTING) - 1;
const int timeRemind = 1000 / HZ_SETTING;
void (*resetFunc)(void) = 0; //declare reset function @ address 0 call resetFunc(); if you want to reset
// start reading from the first byte (address 0) of the EEPROM
int address = 0;
byte value;
byte testData[10] = {0, 11, 22, 33, 44, 55, 66, 77, 88, 99};
void setup() {
// initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
pinMode(LED_PIN, OUTPUT);
if (EEPROM.read(100) == 11 and EEPROM.read(101) == 22) {
Serial.println(" (0 _ 0) b 已經有值了,把EEPROM 的值讀進來");
for (int i = 0; i < 10; i++) {
testData[i] = EEPROM.read(i) ;
}
} else {
Serial.println(" (* * )第一次進來 EEPROM 裡面還是空的,我們讀出來看看喔");
Serial.print("testData[0]~testData[9] :");
for (int i = 0; i < 10; i++) {
Serial.print(testData[i], DEC);
Serial.print(",");
}
Serial.println("");
Serial.println("(* ^ * )EEPROM 裡面還是空的,寫入測試資料");
EEPROM.write(100, 11 ) ;
EEPROM.write(101, 22 ) ;
for (int i = 0; i < 10; i++) {
EEPROM.write(i, i);
testData[i] = EEPROM.read(i);
}
}
}
int secCount = 0;
void loop() {
// read a byte from the current address of the EEPROM
if (millis() - fast_loopTimer > hzCount)
{
fast_loopTimer = millis();
mainLoop_count++;
if (mainLoop_count % HZ_SETTING == 0)
{
secCount++;
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
//// 每兩秒印一次 ------------------------------------------------------------
if (secCount % 3 == 0) {
Serial.print("testData[0]~testData[9] :");
for (int i = 0; i < 10; i++) {
Serial.print(testData[i], DEC);
Serial.print(",");
}
Serial.println("");
}
//////////// 我們在開機5 秒後 來一個假的RESET 來模擬系統被重製了,來看看EEPROM 是否被記住
if (secCount == 5) {
Serial.println("假裝一下我們被Reset 了,等等重新開機後,看看是否會改變");
delay(2000);
resetFunc();
}
}
// Time Remind for this Loop ---------------------------------------------------------------
DEBUGP("Time Remind ms :");
DEBUGN(timeRemind - (millis() - fast_loopTimer));
}
}