조회 수 13944 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print

http://playground.arduino.cc/Code/Timer


Navigation


Information

Description

Similar to SimpleTimer, which I discovered after writing this library, this library also allows control of pins in a similar way to Metro, so I guess its a bit like a fusion of the two.

The library does not interfere with the built-in timers, it just uses 'millis()' in a crude type of scheduler to decide when something needs doing.

Examples

The Arduino 'delay' function is both a blessing and a curse. Its great for showing beginners how to make an LED flash. But as soon as you get more complex and start slowing down your 'loop' function you will run into problems.

A classic example is turning a relay on for 10 minutes. The 'delay'-way looks like this:

  1. int pin = 13;
  2.  
  3.  
  4. void setup()
  5. {
  6.   pinMode(13, OUTPUT);
  7.   digitalWrite(pin, HIGH);
  8.   delay(10 * 60 * 1000);
  9.   digitalWrite(pin, LOW);
  10. }
  11.  
  12.  
  13. void loop()
  14. {
  15. }

The disadvantage of the delay approach is that nothing else can go on while the 'delay' is happening. You cannot update a display, or check for key presses for example.

My 'Timer' library version looks like this:

  1. #include "Timer.h"
  2.  
  3.  
  4. Timer t;
  5. int pin = 13;
  6.  
  7.  
  8. void setup()
  9. {
  10.   pinMode(pin, OUTPUT);
  11.   t.pulse(pin, 10 * 60 * 1000, HIGH); // 10 minutes  
  12. }
  13.  
  14.  
  15. void loop()
  16. {
  17.   t.update();
  18. }

The 'pulse' method takes arguments of a pin to change, the period to change it for and its initial state.

The call to t.update() will take a matter of microseconds to run, unless the appropriate period of time has passed.

Lets look at another example that uses two timer events. One to flash an LED and another that reads A0 and displays the result in the Serial Monitor.

  1. #include "Timer.h"
  2.  
  3.  
  4. Timer t;
  5. int pin = 13;
  6.  
  7.  
  8. void setup()
  9. {
  10.   Serial.begin(9600);
  11.   pinMode(pin, OUTPUT);
  12.   t.oscillate(pin, 100, LOW);
  13.   t.every(1000, takeReading);
  14. }
  15.  
  16.  
  17. void loop()
  18. {
  19.   t.update();
  20. }
  21.  
  22.  
  23. void takeReading()
  24. {
  25.   Serial.println(analogRead(0));
  26. }

The first thing to notice is that we are using a callback function called 'takeReading'. We connect it to the Timer using the 'every' command, which in this case, will call the function every second.

We have also attached another event to the timer using the method 'oscillate'. This will cause the LED to toggle state every 100 milliseconds.

Each of the events has an integer ID associated with it, so that you can stop an event, as we do in this example below, which will write to the serial monitor every 2 seconds, flash the LED and after 5 seconds, stop the LED flashing fast, and flash it 5 times slowly.

  1. #include "Timer.h"
  2.  
  3.  
  4. Timer t;
  5.  
  6.  
  7. int ledEvent;
  8.  
  9.  
  10. void setup()
  11. {
  12.   Serial.begin(9600);
  13.   int tickEvent = t.every(2000, doSomething);
  14.   Serial.print("2 second tick started id=");
  15.   Serial.println(tickEvent);
  16.  
  17.   pinMode(13, OUTPUT);
  18.   ledEvent = t.oscillate(13, 50, HIGH);
  19.   Serial.print("LED event started id=");
  20.   Serial.println(ledEvent);
  21.  
  22.   int afterEvent = t.after(10000, doAfter);
  23.   Serial.print("After event started id=");
  24.   Serial.println(afterEvent);
  25.  
  26. }
  27.  
  28.  
  29. void loop()
  30. {
  31.   t.update();
  32. }
  33.  
  34.  
  35. void doSomething()
  36. {
  37.   Serial.print("2 second tick: millis()=");
  38.   Serial.println(millis());
  39. }
  40.  
  41.  
  42. void doAfter()
  43. {
  44.   Serial.println("stop the led event");
  45.   t.stop(ledEvent);
  46.   t.oscillate(13, 500, HIGH, 5);
  47. }

You can attach up to 10 events to a timer.

Installation

You can download the library from here:

https://github.com/JChristensen/Timer

As with all libraries, unzip the file into the 'libraries' folder in your Arduino directory, which will be in something like 'My Documents\Arduino' on Windows, 'Documents/Arduino' on Mac etc. If this is the first library you have installed, you will need to create a directory there called 'libraries'.

The library is compatible with both Arduino 1.0 and earlier versions.

Usage

int every(long period, callback)

 Run the 'callback' every 'period' milliseconds.
 Returns the ID of the timer event.

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 after(long duration, callback)

 Run the 'callback' once after 'period' milliseconds.
 Returns the ID of the timer event.

int oscillate(int pin, long period, int startingValue)

 Toggle the state of the digital output 'pin' every 'period' milliseconds. The pin's starting value is specified in 'startingValue', which should be HIGH or LOW.
 Returns the ID of the timer event.

int oscillate(int pin, long period, int startingValue, int repeatCount)

 Toggle the state of the digital output 'pin' every 'period' milliseconds 'repeatCount' times. The pin's starting value is specified in 'startingValue', which should be HIGH or LOW.
 Returns the ID of the timer event.

int pulse(int pin, long period, int startingValue)

 Toggle the state of the digital output 'pin' just once after 'period' milliseconds. The pin's starting value is specified in 'startingValue', which should be HIGH or LOW.
 Returns the ID of the timer event.

int stop(int id)

 Stop the timer event running.
 Returns the ID of the timer event.

int update()

 Must be called from 'loop'. This will service all the events associated with the timer.

Links

See also my blog entry on using this library: http://srmonk.blogspot.com/2012/01/arduino-timer-library.html


Dreamy의 코드 스크랩

내가 모으고 내가 보는

List of Articles
번호 분류 제목 날짜 조회 수 추천 수
431 JAVA Timer 사용하기 - Timer, TimerTask 2017.12.23 4839 0
430 JAVA java JSON Parsing 예제들 2017.12.21 17677 0
429 일반 딥러닝 공부 방법 secret 2017.10.23 0 0
428 Pi 라즈베리파이 커널 컴파일 2017.10.18 5911 0
427 Pi 라즈베리파이를 NAS로 사용하기 2017.10.18 5588 0
426 Pi 안면인식, 동작인식을위한 OpenCV 설치 및 샘플 2017.10.18 6467 0
425 Pi 라즈베리파이를 크롬캐스트 처럼 사용하는 방법 - RaspiCast 2017.10.17 8911 0
424 Pi 라즈베리파이 적외선(IR) 리모컨 송신/수신 - LIRC Library 2017.10.17 7663 0
423 Pi 브라우저로 gpio 제어, WebIOPi 2017.10.17 43559 0
422 일반 딥러닝/머신러닝/인공지능 secret 2017.08.16 0 0
421 일반 아마존 '알렉사 Alexa' 명령어 모음 2017.08.04 23983 0
420 Pi 전압 분배(분배 저항)로 병렬 저항 계산하기 2017.07.25 9599 0
419 Pi [아두이노] 포트를 직접 억세스하기 (Direct Port Manipulation in Arduino) 1 2017.07.24 16989 0
418 Pi 아두이노 Arduino String Class 2017.07.16 10184 0
417 Pi Teensy 3.5 spec & pin 2017.07.12 4777 0
목록
Board Pagination ‹ Prev 1 2 3 4 5 6 7 8 9 10 ... 34 Next ›
/ 34

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5