-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoom_Program_Lights_V3.ino
More file actions
132 lines (111 loc) · 3.25 KB
/
Room_Program_Lights_V3.ino
File metadata and controls
132 lines (111 loc) · 3.25 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
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
/*
Logic:
When I Click the button it open the night time activity
then if I Click again it open the serialized lights until it close
*/
const int BUTTON = 5; // Button pin for opening and closing the activity lights
const int NIGHT_ACTIVITY_TRANSISTOR = 6; // Transistor to handle 5 volts to night time activity lights
const int LEDS[] = { A5, A4, A3, A2 }; // ( Red , Blue , Yellow, White )
int clickDelay = 30; // microsecond delay from button
// Button Activity
int clickCounter = 0; // microsecond counter from button
bool isActionIsButton = false; // if the action is in the button
bool isNightLightOpen = false; // If the night time activity light is open
int selectedAction = 1; // if the action is 1 = night time and 2 = serialized lights
/*
The leds must have different colors ( Red , Blue , Yellow, White )
Sleeping Color:
Deep Red = 225 , 25 , 0 , 0
Warm White = 0 , 100 , 50 , 225
Peach = 100 , 0 , 225, 0
Burnt Orange = 225, 150 , 225, 0
*/
int row = 4;
int cols = 4;
int pos = -1;
int SLEEPING_COLOR[4][4] = {
{ 225, 180, 0, 0 },
{ 0, 190, 150, 225 },
{ 180, 0, 225, 0 },
{ 225, 150, 225, 0 }
};
bool isClicked(int pin) {
if (digitalRead(pin) == HIGH) {
return true;
}
return false;
}
void actionSerializedLights() {
// Check if the serialized button is clicked
::pos = ::pos + 1;
// Check if the pos is greater than rows, if true then close the lights and change the pos to -1
if (::pos >= ::row) {
for (int i = 0; i < ::cols; i++) {
analogWrite(::LEDS[i], 1);
}
::pos = -1;
} else {
for (int i = 0; i < ::cols; i++) {
analogWrite(::LEDS[i], ::SLEEPING_COLOR[pos][i]);
}
}
}
void actionNightimeLight() {
// Check if the night time button is clicked
if (::isNightLightOpen) {
// If open then close it
::isNightLightOpen = false;
digitalWrite(::NIGHT_ACTIVITY_TRANSISTOR, LOW);
} else {
// If close then open it
::isNightLightOpen = true;
digitalWrite(::NIGHT_ACTIVITY_TRANSISTOR, HIGH);
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int range = random(0, 1000);
for (int i = 0; i < range; i++) {
randomSeed(analogRead(A0));
}
for (int pin : ::LEDS) {
pinMode(pin, OUTPUT);
}
pinMode(::BUTTON, INPUT);
Serial.begin(9600);
// Open the activity light when run
::isNightLightOpen = true;
digitalWrite(::NIGHT_ACTIVITY_TRANSISTOR, HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
// Activity Logic
bool clicked = false;
if (!::isActionIsButton) {
clicked = ::isClicked(::BUTTON);
if (clicked) {
::isActionIsButton = true;
}
} else {
delayMicroseconds(1);
::clickCounter = ::clickCounter + 1;
if (::clickCounter == ::clickDelay) {
::isActionIsButton = false;
::clickCounter = 0;
}
Serial.println("Button Counter : " + (String)::clickCounter);
}
if (clicked) {
// modify ::selectedAction
::selectedAction = ::selectedAction + 1;
if (::selectedAction == 1){
::actionNightimeLight();
} else if (::selectedAction == 2){
::actionSerializedLights();
} else {
::selectedAction = 1;
::actionNightimeLight();
}
}
}