-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25_TimerInterrup2.ino
More file actions
65 lines (54 loc) · 1.43 KB
/
25_TimerInterrup2.ino
File metadata and controls
65 lines (54 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// led1은 1초마다 꺼졌다가 켜지기를 반복
// led2는 버튼을 누를 때 생성되는 난수(1~5)를 interval에 곱해 버튼을 누르고 있는 상태에서는 1~5초 사이의 간격으로 랜덤하게 점멸
const int led1 = 8;
const int led2 = 9;
const int btn = 10;
int led1_status = LOW;
int led2_status = LOW;
int random_status = LOW;
int randval = 0;
unsigned long previousMillis = 0;
unsigned long btnpreviousMillis = 0;
const long interval = 1000;
int led2_interval = 0;
void setup(){
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(btn, INPUT_PULLUP);
}
void loop(){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
if(led1_status == LOW)
led1_status = HIGH;
else
led1_status = LOW;
digitalWrite(led1, led1_status);
}
if(!digitalRead(btn))
{
if(random_status == LOW)
{
randval=random(1,6);
random_status = HIGH;
btnpreviousMillis = currentMillis;
led2_interval = interval * randval;
}
if(currentMillis - btnpreviousMillis >= led2_interval)
{
btnpreviousMillis = currentMillis;
if(led2_status == LOW)
led2_status = HIGH;
else
led2_status = LOW;
}
}
else
{
led2_status = LOW;
random_status = LOW;
}
digitalWrite(led2, led2_status);
}