댓글 쓰기 권한이 없습니다. 로그인 하시겠습니까?
Pi
2016.10.25 11:46
[Arduino] Timer 라이브러리 사용하기
조회 수 16286 댓글 0
http://playground.arduino.cc/Code/Timer NavigationInformationDescriptionSimilar 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. ExamplesThe 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:
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:
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.
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.
You can attach up to 10 events to a timer. InstallationYou 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. Usageint 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. LinksSee also my blog entry on using this library: http://srmonk.blogspot.com/2012/01/arduino-timer-library.html Dreamy의 코드 스크랩내가 모으고 내가 보는
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Designed by sketchbooks.co.kr / sketchbook5 board skin
Sketchbook5, 스케치북5
Sketchbook5, 스케치북5
Sketchbook5, 스케치북5
Sketchbook5, 스케치북5