-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_xv4.c
233 lines (217 loc) · 5.54 KB
/
project_xv4.c
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
/*
This will Be the Main Program for the project
TODO------------------------------------
implement ability to write to file
implement go back sq when double line hit and turn
implement x and y coords in order to go back to start2
implement ability to scan for an object
try running program with motor sync time on turns to see if it helps at all
check forward distance
-------------------------------------------
c15314356
*/
#pragma config(Sensor, S3, lightSensor, sensorEV3_Color)
#pragma config(Sensor, S2, touchSensor, sensorEV3_Touch)
#pragma config(Sensor, S1, ultraSensor, sensorEV3_Ultrasonic)
#pragma config(Motor, motorB, Right, tmotorEV3_Large, PIDControl, driveRight, encoder)
#pragma config(Motor, motorC, Left, tmotorEV3_Large, PIDControl, driveLeft, encoder)
#define TURN2 1770
#define TURN 178
#define SPEED 30
#define ROW 7
#define COL 9
//global variables
int blacksq=0;
int total=0;
int pos1=0,pos2=0;
int grid[ROW][COL];//<--------------------- change all to strings and also change all intial values to 0
//Turn left 90 degrees
void LeftTurn(void);
//Turn right 90 degrees
void RightTurn(void);
//Go forward
void Forward(void);
//Reset count
int ResetCount(int count);
//Move forward a Square
void ForwardSQ(void);
//Move next line on left
int NextLineLeft(int direction);
//Move next line on right
int NextLineRight(int direction);
task main()
{
int count=0;
int direction=1;
int end=0;
int doubleline=0;
int pause=0;
//turn right go forward until hit double line go back half a square then turn left (You are at bottom left sq now
RightTurn();
while(pause==0)
{
motor(motorB)=SPEED;
motor(motorC)=SPEED;
wait1Msec(1);
if(SensorValue(S3)<45&&doubleline!=1)
{
if(time1[T1]<200)
{
pause=1;
}
doubleline=1;
}
if(SensorValue(S3)>45&&doubleline==1)
{
doubleline=0;
clearTimer(T1);
}
}
motor[motorB]=-20;
motor[motorC]=-20;
wait1Msec(700);
LeftTurn();
while(end!=7)
{
//traverse right and count lines
if(direction==1)
{
end++;
while(count<8)
{
if(SensorValue(S3)<45)
{
blacksq++;
//store position to array
grid[pos1][pos2]=1;
}
//increment counters
count++;
total++;
displayBigTextLine(2,"Black:%d",blacksq);
displayBigTextLine(5,"Count:%d ",count);
displayBigTextLine(8,"Total:%d",total);
//move forward one square
Forward();
pos2++;
}
//Switches to next line
if(end!=6)
{
direction=NextLineLeft(direction);
//Resets count
count=ResetCount(count);
pos2=0;
pos1++;
}
}
if(direction==2)
{
end++;
while(count<8)
{
if(SensorValue(S3)<45)
{
blacksq++;
//store position to array
grid[pos1][pos2]=1;
}
//increment counters
count++;
total++;
displayBigTextLine(2,"Black:%d",blacksq);
displayBigTextLine(5,"Count:%d ",count);
displayBigTextLine(8,"Total:%d",total);
//move forward one square
Forward();
pos2++;
}
//Switches to next line
if(end!=7)
{
//Switches to next line
direction=NextLineRight(direction);
//Resets count
count=ResetCount(count);
pos2=0;
pos1++;
}
}//end if()
//go back 7 suares and take away distance from bottom col from start from 7 and go to start
}//end while()
}//end main()
//Turn left 90 degrees
void LeftTurn(void)
{
motor[motorB]=-10;
motor[motorC]=10;
wait1Msec(TURN2);
}//end LeftTurn
//Turn right 90 degrees
void RightTurn(void)
{
motor[motorB]=10;
motor[motorC]=-10;
wait1Msec(TURN2);
}//end RightTurn
//Go Forward
void Forward(void)
{
motor(motorB)=SPEED;
motor(motorC)=SPEED;
wait1Msec(1000);
}//end Forward()
//resets the number of count
int ResetCount(int count)
{
count=0;
return(count);
}//end ResetCount()
//Move Forward a Square
void ForwardSQ(void)//<--------------------------------------------------change distance forward
{
//Move Forward a Square
nMotorEncoder[motorB]=0;
nMotorEncoder[motorC]=0;
setMotorTarget(motorB,220,20);
setMotorTarget(motorC,220,20);
waitUntilMotorStop(motorB);
waitUntilMotorStop(motorC);
}//end ForwardSQ()
//Move next line on left
int NextLineLeft(int direction)
{
if(SensorValue(S1)<45)
{
blacksq++;
//store position to array
grid[pos1][pos2]=1;
}
//increment total
total++;
ForwardSQ();
LeftTurn();
ForwardSQ();
LeftTurn();
direction=2;
return(direction);
}//end NextLine Left()
//Move next line on right
int NextLineRight(int direction)
{
if(SensorValue(S3)<45)
{
blacksq++;
//store position to array
grid[pos1][pos2]=1;
}
total++;
ForwardSQ();
RightTurn();
motor[motorB]=20;
motor[motorC]=20;
wait1Msec(2200);
RightTurn();
direction=1;
return(direction);
}//end NextLineRight ()