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

Commit 072e043

Browse files
Merge pull request #1 from himalayanelixir/reset-code
Reset code
2 parents c024921 + d9efe8c commit 072e043

File tree

3 files changed

+124
-46
lines changed

3 files changed

+124
-46
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
3+
4+
{{11, 12, 13}, {8, 9, 10}, {5, 6, 7}, {2, 3, 4}, {14, 15, 16}, {17, 18, 19}, {20, 21, 22}, {23, 24, 25}, {29, 30, 31}, {35, 36, 37}, {41, 42, 43}, {47, 48, 49}, {26, 27, 28}, {32, 33, 34}, {38, 39, 40}, {44, 45, 46}, {50, 51, 52}, {53, 54, 55}, {56, 57, 58}, {59, 60, 61}, {63, 64, 65}, {62, 66, 67}};

arduino_controller/arduino_controller.ino

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
#define DEBOUNCE_TIME .4
66
#define SAMPLE_FREQUENCY 20
77
#define MAXIMUM (DEBOUNCE_TIME * SAMPLE_FREQUENCY)
8-
#define NUMBER_MOTORS 1
8+
#define NUMBER_MOTORS 3
9+
#define TIMEOUT 10000
910

1011
// relay pin
12+
//int relayPin = 69;
1113
int relayPin = 53;
1214

1315
// function declarations`
@@ -24,18 +26,17 @@ bool new_data = false;
2426
// initialize motors
2527
Servo my_servo[NUMBER_MOTORS];
2628
// create a array of ports with the order: motor, counter, reset
27-
int ports[NUMBER_MOTORS][3] = {{21, 19, 20}};
28-
// int ports[NUMBER_MOTORS][3] = {{2, 3, 4}, {5, 6, 7}, {8, 9, 10}, {11, 12, 13}, {22, 23, 24}, {25, 26, 27}, {28, 29, 30}, {31, 32, 33}, {34, 35, 36}};
29-
// int ports[NUMBER_MOTORS][3] = {{2, 3, 4}, {5, 6, 7}, {8, 9, 10}, {11, 12, 13}, {22, 23, 24}, {25, 26, 27}};
29+
// int ports[NUMBER_MOTORS][3] = {{62, 66, 67}};
30+
int ports[NUMBER_MOTORS][3] = {{2, 3, 4},{5, 6, 7},{8, 9, 10}};
3031
// integer array that contains the direction and number of rotations a motor
3132
// needs to go
3233
int motor_commands[NUMBER_MOTORS][2] = {0};
3334

3435
// array of new switch values
35-
byte motor_sensor_counter1[NUMBER_MOTORS] = {1};
36+
byte motor_sensor_counter1[NUMBER_MOTORS] = {0};
3637

3738
// array of old switch values
38-
byte motor_sensor_counter2[NUMBER_MOTORS] = {1};
39+
byte motor_sensor_counter2[NUMBER_MOTORS] = {0};
3940

4041
// 0 or 1 depending on the input signal
4142
byte input[NUMBER_MOTORS] = {0};
@@ -51,7 +52,7 @@ void setup()
5152

5253
// initialize relay pin
5354
pinMode(relayPin, OUTPUT);
54-
55+
5556
// initialize all motor ports
5657
Serial.println("Begining Initialization");
5758
Serial.print("Motor Ports: ");

arduino_controller/arduino_controller_functions.ino

Lines changed: 112 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,41 @@
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;
16+
long timeout_counter = 0;
1217

1318
PopulateArray();
19+
1420
// initialize all motors and get them moving
1521
for (int i = 0; i < NUMBER_MOTORS; i++)
1622
{
1723
if (motor_commands[i][0] == 0)
1824
{
1925
my_servo[i].write(100);
2026
Serial.println("Reached Up");
27+
is_reset = false;
2128
}
2229
else if (motor_commands[i][0] == 1)
2330
{
2431
my_servo[i].write(80);
2532
Serial.println("Reached Down");
33+
is_reset = false;
2634
}
2735
else if (motor_commands[i][0] == 2)
2836
{
2937
my_servo[i].write(90);
3038
Serial.println("Reached Stopped");
39+
is_reset = false;
3140
}
3241
else if (motor_commands[i][0] == 3)
3342
{
43+
// Start moving pieces up
44+
my_servo[i].write(100);
45+
Serial.println("Reached Reset");
46+
is_reset = true;
3447
// call reset function (still needs to be written)
3548
}
3649
else
@@ -39,59 +52,119 @@ void ProcessData()
3952
}
4053
}
4154

42-
bool go = true;
43-
int total_turns = 0;
44-
45-
while (go == true)
55+
if (is_reset == false)
4656
{
47-
// subtract from motor rotations when a rotation is detected
48-
for (int i = 0; i < NUMBER_MOTORS; i++)
57+
while (go == true)
4958
{
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++)
5461
{
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+
}
5674
}
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("");
57100

58-
if (motor_commands[i][1] < 0)
101+
// exit loop if there are no more motor rotations remaining
102+
if (total_turns <= 0)
59103
{
60-
motor_commands[i][1] = 0;
104+
go = false;
61105
}
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)
67110
{
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+
}
69117
}
118+
timeout_counter = timeout_counter + 1;
119+
Serial.println(timeout_counter);
70120
}
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;
86128
}
87-
Serial.println("");
88129

89-
// exit loop if there are no more motor rotations remaining
90-
if (total_turns <= 0)
130+
while (go == true)
91131
{
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);
93167
}
94-
total_turns = 0;
95168
}
96169
// Send Finished Signal
97170
Finished();

0 commit comments

Comments
 (0)