This repository has been archived by the owner on May 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
17_0077_0107.ino
214 lines (166 loc) · 4.86 KB
/
17_0077_0107.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <Arduino_FreeRTOS.h>
#include <queue.h>
// Define LED
#define LED_RED 4
#define LED_YELLOW 5
#define LED_GREEN 6
// Define Button
#define BUTTON_RED 7
#define BUTTON_YELLOW 8
#define BUTTON_GREEN 9
// Defin Delay
#define Delay(x) vTaskDelay(pdMS_TO_TICKS(x))
QueueHandle_t RED_QUEUE;
QueueHandle_t YELLOW_QUEUE;
QueueHandle_t GREEN_QUEUE;
void setup()
{
RED_QUEUE = xQueueCreate(10, sizeof(bool));
YELLOW_QUEUE = xQueueCreate(10, sizeof(bool));
GREEN_QUEUE = xQueueCreate(10, sizeof(bool));
xTaskCreate(redTaskReceiver, "RED TASK RECIEVER", 128, (void *)LED_RED, 1, NULL);
xTaskCreate(redTaskSender, "RED TASK SENDER", 128, (void *)BUTTON_RED, 1, NULL);
xTaskCreate(yellowTaskReceiver, "YELLOW TASK RECIEVER", 128, (void *)LED_YELLOW, 1, NULL);
xTaskCreate(yellowTaskSender, "YELLOW TASK SENDER", 128, (void *)BUTTON_YELLOW, 1, NULL);
xTaskCreate(greenTaskReceiver, "GREEN TASK RECEIVER", 128, (void *)LED_GREEN, 1, NULL);
xTaskCreate(greenTaskSender, "GREEN TASK SENDER", 128, (void *)BUTTON_GREEN, 1, NULL);
}
void redTaskSender(void *pvParameters)
{
unsigned short button = (unsigned short)pvParameters;
int is_button_pressed = false;
pinMode(button, INPUT_PULLUP);
while (true)
{
if (isButtonPressed(button) && !is_button_pressed)
{
is_button_pressed = true;
xQueueSend(RED_QUEUE, &button, 0);
}
if (!isButtonPressed(button))
is_button_pressed = false;
Delay(50);
}
}
void redTaskReceiver(void *pvParameters)
{
unsigned short led = (unsigned short)pvParameters;
pinMode(led, OUTPUT);
unsigned short red_count = 0;
while (true)
{
// REMOVE VALUE
if (red_count > 0 && led == LED_RED)
red_count--;
bool is_received = false;
// RECEIVE
if (led == LED_RED)
is_received = xQueueReceive(RED_QUEUE, NULL, 0);
// IF RED LED RECEIVE
if (is_received && led == LED_RED)
red_count += 3;
// RED LED CONDITION
if (red_count > 0 && led == LED_RED)
digitalWrite(LED_RED, HIGH);
else if (!red_count && led == LED_RED)
digitalWrite(LED_RED, LOW);
Delay(1000);
}
}
void yellowTaskSender(void *pvParameters)
{
unsigned short button = (unsigned short)pvParameters;
int is_button_pressed = false;
pinMode(button, INPUT_PULLUP);
while (true)
{
if (isButtonPressed(button) && !is_button_pressed)
{
is_button_pressed = true;
xQueueSend(YELLOW_QUEUE, &button, 0);
}
if (!isButtonPressed(button))
is_button_pressed = false;
Delay(50);
}
}
void yellowTaskReceiver(void *pvParameters)
{
unsigned short led = (unsigned short)pvParameters;
pinMode(led, OUTPUT);
bool is_yellow_blink = true;
bool yellow_blink = true;
while (true)
{
bool is_received = false;
if (led == LED_YELLOW)
is_received = xQueueReceive(YELLOW_QUEUE, NULL, 0);
// RECEIVE
if (is_received && led == LED_YELLOW)
is_yellow_blink = !is_yellow_blink;
// YELLOW LED CONDITION
if (is_yellow_blink && led == LED_YELLOW)
{
digitalWrite(LED_YELLOW, !yellow_blink);
yellow_blink = !yellow_blink;
}
else if (!is_yellow_blink && led == LED_YELLOW)
{
digitalWrite(LED_YELLOW, LOW);
}
Delay(500);
}
}
void greenTaskSender(void *pvParameters)
{
unsigned short button = (unsigned short)pvParameters;
int is_button_pressed = false;
pinMode(button, INPUT_PULLUP);
while (true)
{
if (isButtonPressed(button) && !is_button_pressed)
{
is_button_pressed = true;
xQueueSend(GREEN_QUEUE, &button, 0);
}
if (!isButtonPressed(button))
{
is_button_pressed = false;
}
Delay(50);
}
}
void greenTaskReceiver(void *pvParameters)
{
unsigned short led = (unsigned short)pvParameters;
pinMode(led, OUTPUT);
unsigned short green_count = 0;
while (true)
{
bool is_received = false;
if (led == LED_GREEN)
is_received = xQueueReceive(GREEN_QUEUE, NULL, 0);
// RECEIVE
if (is_received && led == LED_GREEN && green_count == 0)
green_count = 6;
if (green_count > 0 && green_count % 2 != 0 && led == LED_GREEN)
{
digitalWrite(led, LOW);
green_count -= 1;
}
else if (green_count > 0 && green_count % 2 == 0 && led == LED_GREEN)
{
digitalWrite(led, HIGH);
green_count -= 1;
}
Delay(500);
}
}
bool isButtonPressed(int pin)
{
Delay(10);
if (digitalRead(pin) == LOW)
return true;
return false;
}
void loop() {}