아두이노 스케치 Sketch 메뉴에서 Include Library > Manage Libraries 클릭
검색 상자에 "TimerOne"을 입력하고 검색을 클릭합니다.
TimerOne 라이브러리를 선택하고 설치 버튼을 클릭합니다.
#include <TimerOne.h>
int led = 3;
void timerIsr()
{
// 1초마다 실행되는 코드
analogWrite(led, 255);
delay(20);
analogWrite(led, 10);
delay(20);
}
void setup()
{
Timer1.initialize(1000000); //1000000이 1초마다
Timer1.attachInterrupt(timerIsr);
Timer1.start();
pinMode(led, OUTPUT);
}
void loop()
{
// 아무 작업도 하지 않음
}
void timerIsr() {} 함수를 일정 시간마다(1초) 실행
#include<TimerOne.h>
const int LED = 10;
void setup() {
// put your setup code here, to run once:
Timer1.initialize();
Timer1.pwm(LED,0);
Timer1.setPeriod(1000000/1000); //1000Hz 1초에 1000번 실행
// setPeriod(Num) : Num마다 10번 핀 led on, 1000000은 1초
Timer1.setPwmDuty(LED, 900); //0~1023 //led가 켜지는 시간의 비중을 0부터1023으로 선언
//Timer1.setPeriod(Num) 에서 Num값을 적게주면 LED가 항상 켜져있는 것처럼 보이며
//결과적으로 Timer1.setPwmDuty(LED, Num2)
//에서 Num2의 조절로 LED의 밝기 조절이 가능하다
}
void loop() {
// put your main code here, to run repeatedly:
}
#include <TimerOne.h>
const int LED =10;
void setup() {
// put your setup code here, to run once:
Timer1.initialize();
Timer1.pwm(LED, 0);
Timer1.setPeriod(1000); //피리어드를 짧게 준 후 setPwmDuty를 loop로 내렸다
}
void loop() {
// put your main code here, to run repeatedly:
for (int t_high=0; t_high<=1024;t_high++){
Timer1.setPwmDuty(LED, t_high*1);
delay(5);
}
}
setPwmDuty함수를 loop함수로 내려서 점점 밝기가 커지는 led를 구현
#include <TimerOne.h>
const int LED =10;
void setup() {
// put your setup code here, to run once:
Timer1.initialize();
Timer1.pwm(LED, 0);
Timer1.setPeriod(1000); //피리어드를 짧게 준 후 setPwmDuty를 loop로 내렸다
}
void loop() {
// put your main code here, to run repeatedly:
for (int t_high=0; t_high<=1024; t_high++){
Timer1.setPwmDuty(LED, t_high*1);
delay(3);
}
for (int t_high=1024; t_high>=0; t_high--){
Timer1.setPwmDuty(LED, t_high);
delay(3);
}
}
for문을 추가하여 밝기가 점점 최대까지 커지고 점점 줄어드는 led 구현
글 잘 봤습니다, 감사합니다.