-
Notifications
You must be signed in to change notification settings - Fork 0
/
Photoresistor.ino
150 lines (134 loc) · 2.5 KB
/
Photoresistor.ino
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const int RED_LED = 11;
const int GREEN_LED = 10;
const int BLUE_LED = 9;
const int BUTTON = 2;
const int SENSOR = A0;
class ButtonLED
{
private:
bool lastButton_;
bool currentButton_;
bool ledOn_;
int currState_;
bool debounce(bool lastState)
{
bool currentState = digitalRead(BUTTON);
if (lastState != currentState);
{
delay(10);
}
return digitalRead(BUTTON);
}
void changeState()
{
currentButton_ = debounce(lastButton_);
if (!lastButton_ && currentButton_)
{
if (currState_ < 1)
{
currState_ += 1;
}
else
{
currState_ = 0;
}
}
lastButton_ = currentButton_;
}
void pwmPink()
{
int brightness = 0;
analogWrite(GREEN_LED, 0);
analogWrite(BLUE_LED, 10);
while (brightness < 256)
{
if (millis() % 50 == 0)
{
analogWrite(RED_LED, brightness);
brightness++;
}
}
while (brightness >= 0)
{
if (millis() % 50 == 0)
{
analogWrite(RED_LED, brightness);
brightness--;
}
}
}
void pwmLemon()
{
int brightness = 0;
analogWrite(RED_LED, 0);
analogWrite(BLUE_LED, 20);
while (brightness < 256)
{
if (millis() % 50 == 0)
{
analogWrite(GREEN_LED, brightness);
brightness++;
}
}
while (brightness >= 0)
{
if (millis() % 50 == 0)
{
analogWrite(GREEN_LED, brightness);
brightness--;
}
}
}
public:
ButtonLED()
{
lastButton_ = 0;
currentButton_ = 0;
currState_ = 0;
}
void light()
{
changeState();
switch(currState_)
{
case 0:
{
pwmPink();
break;
}
case 1:
{
pwmLemon();
break;
}
}
}
void turnOff()
{
digitalWrite(RED_LED, 0);
digitalWrite(GREEN_LED, 0);
digitalWrite(BLUE_LED, 0);
}
};
void setup()
{
pinMode (RED_LED, OUTPUT);
pinMode (GREEN_LED, OUTPUT);
pinMode (BLUE_LED, OUTPUT);
pinMode (BUTTON, INPUT);
pinMode (SENSOR, INPUT);
}
ButtonLED buttonLed;
void loop()
{
int sensor = analogRead(SENSOR);
int brightness = map(sensor, 1023, 0, 255, 0);
if (brightness <= 55)
{
buttonLed.light();
}
else
{
buttonLed.turnOff();
}
}