9
9
10
10
void ProcessData ()
11
11
{
12
+ // Added this because reset code is different from regular movement code
13
+ bool is_reset = false ;
14
+ bool go = true ;
15
+ int total_turns = 0 ;
16
+ long timeout_counter = 0 ;
12
17
13
18
PopulateArray ();
19
+
14
20
// initialize all motors and get them moving
15
21
for (int i = 0 ; i < NUMBER_MOTORS; i++)
16
22
{
17
23
if (motor_commands[i][0 ] == 0 )
18
24
{
19
25
my_servo[i].write (100 );
20
26
Serial.println (" Reached Up" );
27
+ is_reset = false ;
21
28
}
22
29
else if (motor_commands[i][0 ] == 1 )
23
30
{
24
31
my_servo[i].write (80 );
25
32
Serial.println (" Reached Down" );
33
+ is_reset = false ;
26
34
}
27
35
else if (motor_commands[i][0 ] == 2 )
28
36
{
29
37
my_servo[i].write (90 );
30
38
Serial.println (" Reached Stopped" );
39
+ is_reset = false ;
31
40
}
32
41
else if (motor_commands[i][0 ] == 3 )
33
42
{
43
+ // Start moving pieces up
44
+ my_servo[i].write (100 );
45
+ Serial.println (" Reached Reset" );
46
+ is_reset = true ;
34
47
// call reset function (still needs to be written)
35
48
}
36
49
else
@@ -39,59 +52,119 @@ void ProcessData()
39
52
}
40
53
}
41
54
42
- bool go = true ;
43
- int total_turns = 0 ;
44
-
45
- while (go == true )
55
+ if (is_reset == false )
46
56
{
47
- // subtract from motor rotations when a rotation is detected
48
- for (int i = 0 ; i < NUMBER_MOTORS; i++)
57
+ while (go == true )
49
58
{
50
- motor_sensor_counter2[i] = motor_sensor_counter1[i];
51
- motor_sensor_counter1[i] = CheckSwitch (i, ports[i][1 ]);
52
-
53
- if (motor_sensor_counter1[i] == 1 && motor_sensor_counter2[i] == 0 )
59
+ // subtract from motor rotations when a rotation is detected
60
+ for (int i = 0 ; i < NUMBER_MOTORS; i++)
54
61
{
55
- motor_commands[i][1 ] = motor_commands[i][1 ] - 1 ;
62
+ motor_sensor_counter2[i] = motor_sensor_counter1[i];
63
+ motor_sensor_counter1[i] = CheckSwitch (i, ports[i][1 ]);
64
+
65
+ if (motor_sensor_counter1[i] == 1 && motor_sensor_counter2[i] == 0 )
66
+ {
67
+ motor_commands[i][1 ] = motor_commands[i][1 ] - 1 ;
68
+ }
69
+
70
+ if (motor_commands[i][1 ] < 0 )
71
+ {
72
+ motor_commands[i][1 ] = 0 ;
73
+ }
56
74
}
75
+ // stop motors that have reached 0
76
+ for (int i = 0 ; i < NUMBER_MOTORS; i++)
77
+ {
78
+ if (motor_commands[i][1 ] <= 0 )
79
+ {
80
+ my_servo[i].write (90 );
81
+ }
82
+ }
83
+ // see how many turns are left in the array
84
+ for (int i = 0 ; i < NUMBER_MOTORS; i++)
85
+ {
86
+ total_turns += motor_commands[i][1 ];
87
+ // Serial.print("Total number of turns in array: ");
88
+ // Serial.println(total_turns);
89
+ }
90
+ // print the total number of turns left for each motor
91
+ for (int i = 0 ; i < NUMBER_MOTORS; i++)
92
+ {
93
+ Serial.print (" Motor " );
94
+ Serial.print (i);
95
+ Serial.print (" : " );
96
+ Serial.print (motor_commands[i][1 ]);
97
+ Serial.print (" -- " );
98
+ }
99
+ Serial.println (" " );
57
100
58
- if (motor_commands[i][1 ] < 0 )
101
+ // exit loop if there are no more motor rotations remaining
102
+ if (total_turns <= 0 )
59
103
{
60
- motor_commands[i][ 1 ] = 0 ;
104
+ go = false ;
61
105
}
62
- }
63
- // stop motors that have reached 0
64
- for (int i = 0 ; i < NUMBER_MOTORS; i++)
65
- {
66
- if (motor_commands[i][1 ] <= 0 )
106
+ total_turns = 0 ;
107
+
108
+ // time out loop if stall
109
+ if (timeout_counter >= TIMEOUT)
67
110
{
68
- my_servo[i].write (90 );
111
+ go = false ;
112
+ for (int i = 0 ; i < NUMBER_MOTORS; i++)
113
+ {
114
+ my_servo[i].write (90 );
115
+ Serial.println (" Timeout" );
116
+ }
69
117
}
118
+ timeout_counter = timeout_counter + 1 ;
119
+ Serial.println (timeout_counter);
70
120
}
71
- // see how many turns are left in the array
72
- for (int i = 0 ; i < NUMBER_MOTORS; i++)
73
- {
74
- total_turns += motor_commands[i][1 ];
75
- // Serial.print("Total number of turns in array: ");
76
- // Serial.println(total_turns);
77
- }
78
- // print the total number of turns left for each motor
79
- for (int i = 0 ; i < NUMBER_MOTORS; i++)
80
- {
81
- Serial.print (" Motor " );
82
- Serial.print (i);
83
- Serial.print (" : " );
84
- Serial.print (motor_commands[i][1 ]);
85
- Serial.print (" -- " );
121
+ }
122
+ else
123
+ {
124
+ int reset_counter[NUMBER_MOTORS] = {0 };
125
+
126
+ for (int i = 0 ; i < NUMBER_MOTORS; ++i){
127
+ reset_counter[i] = 1 ;
86
128
}
87
- Serial.println (" " );
88
129
89
- // exit loop if there are no more motor rotations remaining
90
- if (total_turns <= 0 )
130
+ while (go == true )
91
131
{
92
- go = false ;
132
+ // stop motors that have reached 0
133
+ for (int i = 0 ; i < NUMBER_MOTORS; i++)
134
+ {
135
+ if (digitalRead (ports[i][2 ]) == 0 )
136
+ {
137
+ my_servo[i].write (90 );
138
+ reset_counter[i] = 0 ;
139
+ }
140
+ }
141
+ // see how many turns are left in the array
142
+ for (int i = 0 ; i < NUMBER_MOTORS; i++)
143
+ {
144
+ total_turns += reset_counter[i];
145
+ }
146
+
147
+ Serial.print (" Total Turns: " );
148
+ Serial.println (total_turns);
149
+
150
+ if (total_turns <= 0 )
151
+ {
152
+ go = false ;
153
+ }
154
+ total_turns = 0 ;
155
+
156
+ if (timeout_counter >= TIMEOUT)
157
+ {
158
+ go = false ;
159
+ for (int i = 0 ; i < NUMBER_MOTORS; i++)
160
+ {
161
+ my_servo[i].write (90 );
162
+ Serial.println (" Timeout" );
163
+ }
164
+ }
165
+ timeout_counter = timeout_counter + 1 ;
166
+ Serial.println (timeout_counter);
93
167
}
94
- total_turns = 0 ;
95
168
}
96
169
// Send Finished Signal
97
170
Finished ();
0 commit comments