- 3개 LED를 독립적으로 켜고 끄기 - 아두이노 예제 BlinkWithoutDelay 으로부터 변형
- 포인터 & 를 사용하여 LED 가 켜져있는 시간 체크 함수 만들기 - 스케치 함수 인자 처리 결과 값 전달 (tistory.com) 이용하여 변수 값 전달함.
// constants won't change. Used here to set a pin number:
const int OUT1 = 2; // the number of the LED pin
const int OUT2 = 6;
const int OUT3 = 9;
// Variables will change:
int OUT1_state = HIGH; // OUT1_state used to set the LED
int OUT2_state = HIGH;
int OUT3_state = HIGH;
int overTimeOUT1 = 1;
int overTimeOUT3 = 1;
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long prevMsecOUT1 = 0, prevMsecOUT2 = 0, prevMsecOUT3 = 0; // will store last time LED was updated
// constants won't change:
long duration = 1000; // duration at which to blink (milliseconds)
void over_duration(int &overTime, unsigned long &prevMsec, unsigned long currMsec, long duration);
int over_duration_return(unsigned long &prevMsec, unsigned long currMsec, long duration);
void setup() {
Serial.begin(9600);
pinMode(OUT1, OUTPUT);
pinMode(OUT2, OUTPUT);
digitalWrite(OUT1, OUT1_state);
digitalWrite(OUT2, OUT2_state);
digitalWrite(OUT2, OUT3_state);
Serial.println("Test Blink 3 LED independently ");
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the duration at which you want to blink the LED.
unsigned long currMsec = millis();
// BlinkWithoutDelay 안에 있는 코드
if (currMsec - prevMsecOUT2 >= duration) {
// save the last time you blinked the LED
prevMsecOUT2 = currMsec;
Serial.print(currMsec);
Serial.println(" overTimeOUT2 ");
// if the LED is off turn it on and vice-versa:
if (OUT2_state == LOW) {
OUT2_state = HIGH;
} else {
OUT2_state = LOW;
}
// set the LED with the OUT2_state of the variable:
digitalWrite(OUT2, OUT2_state);
}
over_duration(overTimeOUT1, prevMsecOUT1, currMsec, 2000);
if (overTimeOUT1 == 1) {
// if the LED is off turn it on and vice-versa:
Serial.print(currMsec);
Serial.println(" overTimeOUT1 ");
if (OUT1_state == LOW) {
OUT1_state = HIGH;
} else {
OUT1_state = LOW;
}
// set the LED with the OUT2_state of the variable:
digitalWrite(OUT1, OUT1_state);
}
if (over_duration_return(prevMsecOUT3, currMsec, 4000) == 1) {
// if the LED is off turn it on and vice-versa:
Serial.print(currMsec);
Serial.println(" overTimeOUT3 ");
if (OUT3_state == LOW) {
OUT3_state = HIGH;
} else {
OUT3_state = LOW;
}
// set the LED with the OUT2_state of the variable:
digitalWrite(OUT3, OUT3_state);
}
}
void over_duration(int &overTime, unsigned long &prevMsec, unsigned long currMsec, long duration) {
if (currMsec - prevMsec >= duration) {
prevMsec = currMsec;
overTime = 1;
} else {
overTime = 0;
}
}
int over_duration_return(unsigned long &prevMsec, unsigned long currMsec, long duration) {
int overTime = 0;
if (currMsec - prevMsec >= duration) {
prevMsec = currMsec;
overTime = 1;
}
return overTime;
}
출력
Test Blink 3 LED independently
1000 overTimeOUT2
2000 overTimeOUT2
2000 overTimeOUT1
3000 overTimeOUT2
4000 overTimeOUT2
4000 overTimeOUT1
4000 overTimeOUT3
5000 overTimeOUT2
6000 overTimeOUT2
6000 overTimeOUT1
7000 overTimeOUT2
8000 overTimeOUT2
8000 overTimeOUT1
8000 overTimeOUT3
9000 overTimeOUT2
반응형
'Arduino' 카테고리의 다른 글
스케치 함수 인자 처리 결과 값 전달 (0) | 2021.11.04 |
---|---|
스텝모터 돌아가게 하기 - 주입펌프 만들기 (0) | 2021.11.01 |
아두이노로 릴레이 동작시키기 (0) | 2021.10.28 |
아두이노 'pinMode(switchPin, INPUT_PULLUP);' (0) | 2021.10.19 |
댓글