forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
254 lines (172 loc) · 5.08 KB
/
Main.cpp
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <Arduino_FreeRTOS.h>
#include <Stepper.h>
#include <queue.h>
#include <semphr.h>
const int stepsPerRevolution = 2048;
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);
void TaskBlink( void *pvParameters );
void TaskAnalogRead( void *pvParameters );
QueueHandle_t integerQueue;
SemaphoreHandle_t xSerialSemaphore;
int val1=0;
void setup() {
Serial.begin(9600);
while (!Serial) {
}
integerQueue = xQueueCreate(10, // Queue length
sizeof(int) // Queue item size
);
if ( xSerialSemaphore == NULL ) // Check to confirm that the Serial Semaphore has not already been created.
{
xSerialSemaphore = xSemaphoreCreateMutex(); // Create a mutex semaphore we will use to manage the Serial Port
if ( ( xSerialSemaphore ) != NULL )
xSemaphoreGive( ( xSerialSemaphore ) ); // Make the Serial Port available for use, by "Giving" the Semaphore.
}
if (integerQueue != NULL) {
// Create task that consumes the queue if it was created.
xTaskCreate(TaskSerial, // Task function
"Serial", // A name just for humans
128, // This stack size can be checked & adjusted by reading the Stack Highwater
NULL,
1, // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
NULL);
// Create task that publish data in the queue if it was created.
xTaskCreate(TaskAnalogRead, // Task function
"AnalogRead", // Task name
128, // Stack size
NULL,
1, // Priority
NULL);
xTaskCreate(
TaskEmergency
, "Emergency" // A name just for humans
, 128 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL
, 3 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
, NULL );
xTaskCreate(
TaskDoor
, "Door" // A name just for humans
, 128 // This stack size can be checked & adjusted by reading the Stack Highwater
, NULL
, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
, NULL );
}
}
void loop()
{
}
/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/
void TaskEmergency(void *pvParameters) // This is a task.
{
(void) pvParameters;
pinMode(3, INPUT);
int a=0;
int val2;
for (;;) // A Task shall never return or exit.
{
a=digitalRead(3);
if(a==1)
{
Serial.write(a);
vTaskDelay( 500 / portTICK_PERIOD_MS );
val2 = 0;
xQueueSend(integerQueue, &val2, portMAX_DELAY);
vTaskDelay(1);
}
}
}
void TaskDoor(void *pvParameters) // This is a task.
{
(void) pvParameters;
pinMode(4, INPUT);
int b=0;
int val3;
for (;;) // A Task shall never return or exit.
{
b=digitalRead(3);
if(b==1)
{
vTaskDelay( 500 / portTICK_PERIOD_MS );
val3 = -1;
xQueueSend(integerQueue, &val3, portMAX_DELAY);
vTaskDelay(1);
}
}
}
void TaskAnalogRead(void *pvParameters) // This is a task.
{
(void) pvParameters;
myStepper.setSpeed(15);
int valueFromQueue = 0;
int val;
int flag=0;
for (;;)
{
if (xQueueReceive(integerQueue, &valueFromQueue, portMAX_DELAY) == pdPASS) {
val=valueFromQueue;
}
if (val==1) //first floor
{
if (flag==2)
{
myStepper.step(-val*2000);
delay(1000);
}
else if (flag==0)
{
myStepper.step(val*2000);
delay(1000);
}
flag=1;
}
else if (val==2) // second floor
{
if (flag==0)
{
myStepper.step(val*2000);
delay(1000);
}
else if (flag==1)
{
myStepper.step(flag*2000);
delay(1000);
}
flag=2;
}
else if (val==0) //ground floor
{
if (flag==1)
{
myStepper.step(-flag*2000);
delay(1000);
}
else if (flag==2)
{
myStepper.step(-flag*2000);
delay(1000);
}flag=0;
}
else if(val==-1)
{
Serial.println("Close the door!");
}
else
{
Serial.println("Floor does'nt Exist!");
}
}
}
void TaskSerial(void * pvParameters) {
(void) pvParameters;
for (;;)
{
while (Serial.available() == 0);
val1 = Serial.parseInt();
xQueueSend(integerQueue, &val1, portMAX_DELAY);
// One tick delay (15ms) in between reads for stability
vTaskDelay(1);
}
}