-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_arduino.cs
333 lines (306 loc) · 10.4 KB
/
code_arduino.cs
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <Servo.h> //library for servo motor.
#include <math.h>
Servo servo1; //For door left
Servo servo2; //For door middle
Servo servo3; //For door right
Servo servo4; //For door begin_left
Servo servo5; //For door begin_right
//Decalare pin
#define servo_1 31
#define servo_2 32
#define servo_3 33
#define servo_4 34
#define servo_5 35
#define pump_1 36 // left
#define pump_2 37 // middle
#define pump_3 38 // right
#define portIRsensor_1 A5 // left
#define portIRsensor_2 A6 // middle
#define portIRsensor_3 A7 // right
#define portLED_1 28 // left
#define portLED_2 29 // middle
#define portLED_3 30 // right
#define armIRsensor_1 A0 //For arm_left
#define armIRsensor_2 A1 //For arm_middle
#define armIRsensor_3 A2 //For arm_right
#define armIRsensor_4 A3 //For begin_left
#define armIRsensor_5 A4 //For begin_right
#define TTLport 1 //send TTL to intan with info from mouse port 1 , 2 and 3
#define TTLarm_1 2 //send TTL to intan with info from arm 1 , 2 and 3
#define TTLarm_2 3 //send TTL to intan with info from beginsensor 1 and 2 [armIRsensor_4 and armIRsensor_5 ]
int servoopen_1 = 0;
int servoclose_1 = 90;
int servoopen_2 = 180;
int servoclose_2 = 90;
int servoopen_3 = 180;
int servoclose_3 = 90;
int servoopen_4 = 180;
int servoclose_4 = 115;
int servoopen_5 = 12;
int servoclose_5 = 63;
unsigned long PUMP_OPEN_TIME = 100;
unsigned long DOOR_WAIT_TIME = 5000;
int portIRsensorReading_1 = 0;
int portIRsensorReading_2 = 0;
int portIRsensorReading_3 = 0;
int armIRsensorReading_1 = 0;
int armIRsensorReading_2 = 0;
int armIRsensorReading_3 = 0;
int armIRsensorReading_4 = 0;
int armIRsensorReading_5 = 0;
boolean EnableWater_1 = false;
boolean EnableWater_2 = false;
boolean EnableWater_3 = false;
boolean water_open_1 = false;
boolean water_open_2 = false;
boolean water_open_3 = false;
unsigned long Water_1;
unsigned long Water_2;
unsigned long Water_3;
int current_state = 0;
int previous_state = 0;
int corret_trial = 0;
int error_trial = 0;
int trial_num = 0;
int val = 254;
void setup()
{
// put your setup code here, to run once:
servo1.attach(servo_1);
servo2.attach(servo_2);
servo3.attach(servo_3);
servo4.attach(servo_4);
servo5.attach(servo_5);
pinMode(pump_1, OUTPUT);
pinMode(pump_2, OUTPUT);
pinMode(pump_3, OUTPUT);
pinMode(portLED_1, OUTPUT);
pinMode(portLED_2, OUTPUT);
pinMode(portLED_3, OUTPUT);
//pinMode(portIRsensor_1, INPUT);
//pinMode(portIRsensor_2, INPUT);
//pinMode(portIRsensor_3, INPUT);
//pinMode(armIRsensor_1, INPUT);
//pinMode(armIRsensor_2, INPUT);
//pinMode(armIRsensor_3, INPUT);
//pinMode(armIRsensor_4, INPUT);
//pinMode(armIRsensor_5, INPUT);
pinMode(TTLport, OUTPUT);
pinMode(TTLarm_1, OUTPUT);
pinMode(TTLarm_2, OUTPUT);
current_state = 1;
servo1.write(servoopen_1);
servo2.write(servoopen_2);
servo3.write(servoopen_3);
servo4.write(servoclose_4);
servo5.write(servoclose_5);
digitalWrite(portLED_1, HIGH);
digitalWrite(portLED_2, HIGH);
digitalWrite(portLED_3, HIGH);
Serial.begin(9600);
}
//rule : mice need to choose correct arm + correct port // could be only correct port only/ correct arm only
void loop()
{
// put your main code here, to run repeatedly:
// mouse run in a order: state 1: left state 2: middle state 3: right
//Define: Correct=Left
Serial.println(current_state);
portIRsensorReading_1 = analogRead(portIRsensor_1);
portIRsensorReading_2 = analogRead(portIRsensor_2);
portIRsensorReading_3 = analogRead(portIRsensor_3);
armIRsensorReading_1 = analogRead(armIRsensor_1);
armIRsensorReading_2 = analogRead(armIRsensor_2);
armIRsensorReading_3 = analogRead(armIRsensor_3);
armIRsensorReading_4 = analogRead(armIRsensor_4);
armIRsensorReading_5 = analogRead(armIRsensor_5);
//analogWrite(TTLport, round(val / 2));
//analogWrite(TTLport, round(val / 4));
//analogWrite(TTLport, round(val / 8));
//analogWrite(TTLarm_1, round(val / 2));
//analogWrite(TTLarm_1, round(val / 4));
//analogWrite(TTLarm_1, round(val / 8));
//analogWrite(TTLarm_2, round(val / 2));
//analogWrite(TTLarm_2, round(val / 4));
//analogWrite(TTLarm_2, round(val / 8));
if (current_state == 1)
{
EnableWater_1 = true;
}
//Define: Correct=middle
if (current_state == 2)
{
EnableWater_2 = true;
}
//Define: Correct=right
if (current_state == 3)
{
EnableWater_3 = true;
}
//It is time for choosing!
// All door closed
//if (armIRsensorReading_1 < 500 || armIRsensorReading_2 < 500 || armIRsensorReading_3 < 500) //If mice always go back without trying the mouse port delete "//"
if (portIRsensorReading_1 > 500 || portIRsensorReading_2 > 500 || portIRsensorReading_3 > 500)
{
servo1.write(servoclose_1);
servo2.write(servoclose_2);
servo3.write(servoclose_3);
}
// Wrong arm notes: open doors in the begining arm + LED off
// Strict rule: even a mouse first lick correct port but chooses the wrong arm, we will count it as a failure. Loose rule, after mice lick the correct port they will get water.
if ((EnableWater_1 && armIRsensorReading_2 < 500) || (EnableWater_1 && armIRsensorReading_3 < 500))
{
error_trial += 1;
EnableWater_1 = false;
current_state = 0;
previous_state = 3;//keep run state 1
servo4.write(servoopen_4);
servo5.write(servoopen_5);
digitalWrite(portLED_1, LOW);
digitalWrite(portLED_2, LOW);
digitalWrite(portLED_3, LOW);
}
if ((EnableWater_2 && armIRsensorReading_1 < 500) || (EnableWater_2 && armIRsensorReading_3 < 500))
{
error_trial += 1;
EnableWater_1 = false;
current_state = 0;
previous_state = 1;//keep run state 2
servo4.write(servoopen_4);
servo5.write(servoopen_5);
digitalWrite(portLED_1, LOW);
digitalWrite(portLED_2, LOW);
digitalWrite(portLED_3, LOW);
}
if ((EnableWater_3 && armIRsensorReading_1 < 500) || (EnableWater_3 && armIRsensorReading_2 < 500))
{
error_trial += 1;
EnableWater_1 = false;
current_state = 0;
previous_state = 2;//keep run state 3
servo4.write(servoopen_4);
servo5.write(servoopen_5);
digitalWrite(portLED_1, LOW);
digitalWrite(portLED_2, LOW);
digitalWrite(portLED_3, LOW);
}
// Wrong port: open doors in the begining arm + LED off
if (current_state != 0)
{
if ((EnableWater_1 && portIRsensorReading_2 > 500) || (EnableWater_1 && portIRsensorReading_3 > 500))
{
error_trial += 1;
EnableWater_1 = false;
current_state = 0;
previous_state = 3;//keep run state 1
servo4.write(servoopen_4);
servo5.write(servoopen_5);
digitalWrite(portLED_1, LOW);
digitalWrite(portLED_2, LOW);
digitalWrite(portLED_3, LOW);
}
if ((EnableWater_2 && portIRsensorReading_1 > 500) || (EnableWater_2 && portIRsensorReading_3 > 500))
{
error_trial += 1;
EnableWater_1 = false;
current_state = 0;
previous_state = 1;//keep run state 2
servo4.write(servoopen_4);
servo5.write(servoopen_5);
digitalWrite(portLED_1, LOW);
digitalWrite(portLED_2, LOW);
digitalWrite(portLED_3, LOW);
}
if ((EnableWater_3 && portIRsensorReading_2 > 500) || (EnableWater_3 && portIRsensorReading_1 > 500))
{
error_trial += 1;
EnableWater_1 = false;
current_state = 0;
previous_state = 2;//keep run state 3
servo4.write(servoopen_4);
servo5.write(servoopen_5);
digitalWrite(portLED_1, LOW);
digitalWrite(portLED_2, LOW);
digitalWrite(portLED_3, LOW);
}
// Correct trail Correct=Left
if (EnableWater_1 && portIRsensorReading_1 > 500)
{
Water_1 = millis();
EnableWater_1 = false;
water_open_1 = true;
digitalWrite(pump_1, HIGH);
servo4.write(servoopen_4);
servo5.write(servoopen_5);
current_state = 0;
previous_state = 1;//wait until mice learn port 1 is correct
corret_trial += 1;
}
if (water_open_1 && millis() - Water_1 >= PUMP_OPEN_TIME)
{
digitalWrite(pump_1, LOW);
water_open_1 = false;
}
// Correct trail Correct=middle
if (EnableWater_2 && portIRsensorReading_2 > 500)
{
Water_2 = millis();
digitalWrite(pump_2, HIGH);
EnableWater_2 = false;
water_open_2 = true;
servo4.write(servoopen_4);
servo5.write(servoopen_5);
current_state = 0;
previous_state = 2;
}
if (water_open_2 && millis() - Water_2 >= PUMP_OPEN_TIME)
{
digitalWrite(pump_2, LOW);
water_open_2 = false;
}
//Open right port Correct=right
if (EnableWater_3 && portIRsensorReading_3 > 500)
{
Water_3 = millis();
digitalWrite(pump_3, HIGH);
EnableWater_3 = false;
water_open_3 = true;
current_state = 0;
previous_state = 3;
servo4.write(servoopen_4);
servo5.write(servoopen_5);
}
if (water_open_3 && millis() - Water_3 >= PUMP_OPEN_TIME)
{
digitalWrite(pump_3, LOW);
water_open_3 = false;
}
}
if (current_state == 0) //a mouse goes back and reset the task
{
if (armIRsensorReading_4 < 500 || armIRsensorReading_5 < 500)
{
trial_num += 1;
servo4.write(servoclose_4);
servo5.write(servoclose_5);
digitalWrite(portLED_1, HIGH);
digitalWrite(portLED_2, HIGH);
digitalWrite(portLED_3, HIGH);
if (previous_state == 1)
{
current_state = 2;
previous_state = 0;
}
if (previous_state == 2)
{
current_state = 3;
previous_state = 0;
}
if (previous_state == 3)
{
current_state = 1;
previous_state = 0;
}
}
}
}