Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 77847d1

Browse files
Added reset code
1 parent c024921 commit 77847d1

File tree

1 file changed

+82
-41
lines changed

1 file changed

+82
-41
lines changed

arduino_controller/arduino_controller_functions.ino

Lines changed: 82 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,40 @@
99

1010
void ProcessData()
1111
{
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;
1216

1317
PopulateArray();
18+
1419
// initialize all motors and get them moving
1520
for (int i = 0; i < NUMBER_MOTORS; i++)
1621
{
1722
if (motor_commands[i][0] == 0)
1823
{
1924
my_servo[i].write(100);
2025
Serial.println("Reached Up");
26+
is_reset = false;
2127
}
2228
else if (motor_commands[i][0] == 1)
2329
{
2430
my_servo[i].write(80);
2531
Serial.println("Reached Down");
32+
is_reset = false;
2633
}
2734
else if (motor_commands[i][0] == 2)
2835
{
2936
my_servo[i].write(90);
3037
Serial.println("Reached Stopped");
38+
is_reset = false;
3139
}
3240
else if (motor_commands[i][0] == 3)
3341
{
42+
// Start moving pieces up
43+
my_servo[i].write(100);
44+
Serial.println("Reached Reset");
45+
is_reset = true;
3446
// call reset function (still needs to be written)
3547
}
3648
else
@@ -39,59 +51,88 @@ void ProcessData()
3951
}
4052
}
4153

42-
bool go = true;
43-
int total_turns = 0;
44-
45-
while (go == true)
54+
if (is_reset == false)
4655
{
47-
// subtract from motor rotations when a rotation is detected
48-
for (int i = 0; i < NUMBER_MOTORS; i++)
56+
while (go == true)
4957
{
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++)
5460
{
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+
}
5673
}
57-
58-
if (motor_commands[i][1] < 0)
74+
// stop motors that have reached 0
75+
for (int i = 0; i < NUMBER_MOTORS; i++)
5976
{
60-
motor_commands[i][1] = 0;
77+
if (motor_commands[i][1] <= 0)
78+
{
79+
my_servo[i].write(90);
80+
}
6181
}
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++)
6784
{
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);
6988
}
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;
70106
}
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)
80112
{
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("");
88113

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+
}
93135
}
94-
total_turns = 0;
95136
}
96137
// Send Finished Signal
97138
Finished();

0 commit comments

Comments
 (0)