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