-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBakeoff4.pde
292 lines (248 loc) · 6.84 KB
/
Bakeoff4.pde
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
import java.util.ArrayList;
import java.util.Collections;
import ketai.sensors.*;
KetaiSensor sensor;
float cursorX, cursorY;
float light = 0;
float proxSensorThreshold = 10; //you will need to change this per your device.
float accX;
float accY;
float accZ;
private class Target
{
int target = 0;
int action = 0;
}
int trialCount = 5; //this will be set higher for the bakeoff
int trialIndex = 0;
ArrayList<Target> targets = new ArrayList<Target>();
int startTime = 0; // time starts when the first click is captured
int finishTime = 0; //records the time of the final click
boolean userDone = false;
int countDownTimerWait = 0;
PFont bold, reg;
boolean correctFlag = false;
boolean waitForChoose2 = false;
void setup() {
size(540, 600, P2D); //you can change this to be fullscreen
frameRate(60);
sensor = new KetaiSensor(this);
sensor.start();
orientation(PORTRAIT);
rectMode(CENTER);
bold = createFont("Arial Bold", 60);
reg = createFont("Arial", 40);
textFont(reg); //sets the font to Arial size 20
textAlign(CENTER);
for (int i=0; i<trialCount; i++) //don't change this!
{
Target t = new Target();
t.target = ((int)random(1000))%4;
t.action = ((int)random(1000))%2;
targets.add(t);
println("created target with " + t.target + "," + t.action);
}
Collections.shuffle(targets); // randomize the order of the button;
}
void draw() {
int index = trialIndex;
//uncomment line below to see if sensors are updating
//println("light val: " + light +", cursor accel vals: " + cursorX +"/" + cursorY);
background(80); //background is light grey
noStroke(); //no stroke
countDownTimerWait--;
if (startTime == 0)
startTime = millis();
if (index>=targets.size() && !userDone)
{
userDone=true;
finishTime = millis();
}
if (userDone)
{
text("User completed " + trialCount + " trials", width/2, 50);
text("User took " + nfc((finishTime-startTime)/1000f/trialCount, 1) + " sec per target", width/2, 150);
return;
}
/**
* Ellipses
for (int i=0; i<4; i++)
{
if (targets.get(index).target==i)
fill(0, 255, 0);
else
fill(180, 180, 180);
ellipse(300, i*150+100, 100, 100);
}
*/
if (targets.get(index).action==0)
{
if (correctFlag) {
fill(0,255,0);
} else {
fill(100);
}
rect(width/2,120,width,height/2);
fill(255);
text("UP", width/2, height/2);
}
else {
if (correctFlag) {
fill(0,255,0);
} else {
fill(100);
}
rect(width/2,160+height/2,width,height/2);
fill(255);
text("DOWN", width/2, height/2);
}
fill(255, 0, 0);
ellipse(cursorX, cursorY, 50, 50);
fill(150);//white
text("Trial " + (index+1) + " of " +trialCount, width/2, 50);
//text("Target #" + (targets.get(index).target)+1, width/2, 100);
int num = targets.get(index).target;
switch(num) {
case 0:
if (!correctFlag) {
fill(0,255,0);
textFont(bold);
}
text("OUT", width/2, 100);
fill(255);
textFont(reg);
text("IN", width/2, 580);
text("LEFT", 60, height/2);
text("RIGHT", 480, height/2);
break;
case 1:
if (!correctFlag) {
fill(0,255,0);
textFont(bold);
}
text("IN", width/2, 580);
fill(255);
textFont(reg);
text("OUT", width/2, 100);
text("LEFT", 60, height/2);
text("RIGHT", 480, height/2);
break;
case 2:
if (!correctFlag) {
fill(0,255,0);
textFont(bold);
}
text("LEFT", 60, height/2);
fill(255);
textFont(reg);
text("OUT", width/2, 100);
text("IN", width/2, 580);
text("RIGHT", 480, height/2);
break;
case 3:
if (!correctFlag) {
fill(0,255,0);
textFont(bold);
}
text("RIGHT", 480, height/2);
fill(255);
textFont(reg);
text("OUT", width/2, 100);
text("IN", width/2, 580);
text("LEFT", 60, height/2);
break;
}
}
boolean isCorrect(Target t, float x, float y) {
return (t.target == 0 && y > 3) ||
(t.target == 1 && y < -3) ||
(t.target == 2 && x < -3) ||
(t.target == 3 && x > 3);
}
void onAccelerometerEvent(float x, float y, float z)
{
int index = trialIndex;
accX = x;
accY = y;
accZ = z;
if (userDone || index>=targets.size())
return;
cursorX = 300+x*40; //cented to window and scaled
cursorY = 300-y*40; //cented to window and scaled
println("x: " + (x) + " y: " + (y) + " z: " + (z-9.8));
Target t = targets.get(index);
if (t==null)
return;
print(isCorrect(t,x,y));
if(isCorrect(t,x,y))
correctFlag = true;
// if correct initial choose 4 direction, then check for up/down is correct.
if (countDownTimerWait < 0 && isCorrect(t,x,y)) {
print("Enter first check");
correctFlag = true;
countDownTimerWait=30; //wait roughly 0.5 sec before allowing next trial
waitForChoose2 = true;
} else if (!waitForChoose2 && countDownTimerWait < 0 &&
(((t.target == 0 || t.target ==1) && abs(y) > 3) ||
((t.target == 2 || t.target == 3) && abs(x) > 3))) {
println("wrong round 1 action!");
if (trialIndex>0)
trialIndex--; //move back one trial as penalty!
correctFlag = false;
countDownTimerWait=30; //wait roughly 0.5 sec before allowing next trial
}
//Check if correct motion UP/DOWN
print("ZZZ: " + correctFlag);
if (countDownTimerWait < 0 && correctFlag) {
print("Enter z!!!");
if (((z-9.8)>4 && t.action==0) || ((z-9.8)<-4 && t.action==1)) {
println("Right target, right z direction!");
correctFlag = false;
countDownTimerWait = 60;
trialIndex++; //next trial!
} else if (((z-9.8)<-4 && t.action==0) || ((z-9.8)>4 && t.action==1)) {
correctFlag = false;
println("right target, WRONG z direction!");
}
}
}
/*
if (light<=proxSensorThreshold && abs(z-9.8)>4 && countDownTimerWait<0) //possible hit event
{
if (hitTest()==t.target)//check if it is the right target
{
//println(z-9.8); use this to check z output!
if (((z-9.8)>4 && t.action==0) || ((z-9.8)<-4 && t.action==1))
{
println("Right target, right z direction!");
trialIndex++; //next trial!
} else
{
if (trialIndex>0)
trialIndex--; //move back one trial as penalty!
println("right target, WRONG z direction!");
}
countDownTimerWait=30; //wait roughly 0.5 sec before allowing next trial
}
} else if (light<=proxSensorThreshold && countDownTimerWait<0 && hitTest()!=t.target)
{
println("wrong round 1 action!");
if (trialIndex>0)
trialIndex--; //move back one trial as penalty!
countDownTimerWait=30; //wait roughly 0.5 sec before allowing next trial
}
}
int hitTest()
{
for (int i=0; i<4; i++)
if (dist(300, i*150+100, cursorX, cursorY)<100)
return i;
return -1;
}
*/
/*
void onLightEvent(float v) //this just updates the light value
{
light = v;
}
*/