int pin = 13;
void setup()
{
pinMode(13, OUTPUT);
digitalWrite(pin, HIGH);
delay(10 * 60 * 60 * 1000);
digitalWrite(pin, LOW);
}
void loop()
{
}
“ Timer”庫版本如下所示:
Timer t;
int pin = 13;
void setup()
{
pinMode(pin, OUTPUT);
t.pulse(pin, 10 * 60 * 1000, HIGH); // 10 minutes
}
void loop()
{
t.update();
}
#include "Timer.h"
Timer t;
int pin = 13;
void setup()
{
Serial.begin(9600);
pinMode(pin, OUTPUT);
t.oscillate(pin, 100, LOW);
t.every(1000, takeReading);
}
void loop()
{
t.update();
}
void takeReading()
{
Serial.println(analogRead(0));
}
每個事件都有一個與之關聯的整數ID,因此您可以停止一個事件,
Timer t;
int ledEvent;
void setup()
{
Serial.begin(9600);
int tickEvent = t.every(2000, doSomething);
Serial.print("2 second tick started id=");
Serial.println(tickEvent);
pinMode(13, OUTPUT);
ledEvent = t.oscillate(13, 50, HIGH);
Serial.print("LED event started id=");
Serial.println(ledEvent);
int afterEvent = t.after(10000, doAfter);
Serial.print("After event started id=");
Serial.println(afterEvent);
}
void loop()
{
t.update();
}
void doSomething()
{
Serial.print("2 second tick: millis()=");
Serial.println(millis());
}
void doAfter()
{
Serial.println("stop the led event");
t.stop(ledEvent);
t.oscillate(13, 500, HIGH, 5);
}
As with all libraries, unzip the file into the 'libraries' folder in your Arduino directory,
int every(long period, callback)
Run the 'callback' every 'period' milliseconds.
Returns the ID of the timer event.
每隔“週期”毫秒運行一次“回調”。
返回計時器事件的ID。
int every(long period, callback, int repeatCount)
Run the 'callback' every 'period' milliseconds for a total of 'repeatCount' times.
Returns the ID of the timer event.
(長周期,回調,int repeatCount)
每隔“週期”毫秒運行一次“回調”,共計“ repeatCount”次。
返回計時器事件的ID。
int after(long duration, callback)
Run the 'callback' once after 'period' milliseconds.
Returns the ID of the timer event.
在“週期”毫秒後運行一次“回調”。
返回計時器事件的ID。
Stop the timer event running.
Returns the ID of the timer event.
停止運行計時器事件。
返回計時器事件的ID。
int update()
Must be called from 'loop'. This will service all the events associated with the timer.