2015-08-26 130 views

回答

0
#include <EEPROM.h> // library to access the onboard EEPROM 

const int debounceTime = 15000; // debounce time in microseconds 
int buttonPin = 5; // pushbutton connected to digital pin 5 
int eeAddress = 0; // Address in the eeprom to store the data. 
volatile unsigned long time; // variable to store the time since the program started 
volatile boolean timeRecorded = false; // used to know when to save value 
volatile unsigned long last_Rising; // used to debounce button press 

void setup() 
{ 
    pinMode(buttonPin, INPUT);  // sets the digital pin 5 as input 
    attachInterrupt(buttonPin, debounce, RISING); 
} 

void loop() 
{ 
    // Only want write when a time is saved 
    if(valueRecorded) 
    {   
    EEPROM.put(eeAddress, time); 
    valueRecorded = false; 
    } 
} 

void debounce() 
{ 
    if((micros() - last_Rising) >= debouncing_time) 
    { 
    getTime(); // call actual method to fetch and save time 
    last_Rising = micros(); 
    } 
} 

void getTime() 
{ 
    time = millis(); 
    valueRecorded = true; 
} 

這個答案作以下假設:正在使用

- 一個Arduino的烏諾。
- 將一個瞬時開關(或按鈕)連接到數字引腳5,使得當開關處於「導通」位置時,5v信號將被施加到引腳5,而0v信號將被施加到引腳5時開關處於「關」的位置。
- 您的目標是在機載eeprom中記錄上次發生按鈕更改狀態的時間。

此代碼使用中斷來捕捉從「關」到「開」的轉換。開關的機械特性需要去除輸入(https://en.wikipedia.org/wiki/Switch#Contact_bounce)。

要從eeprom中讀取值,您將同樣使用EEPROM.get(eeAddress, time)這會將保存在eeprom中的值存儲在變量time中。

此代碼也沒有規定處理實際的日曆時間。Arduino遊樂場有一個時間庫(http://playground.arduino.cc/code/time),雖然它顯然過時了。一個「時間」庫鏈接到該頁面上,並具有如何使用它提供日曆時間的文檔,但是,每次重新啓動Arduino時都需要設置和同步時間。