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

Commit c0ceeba

Browse files
Refactoring to handle more than 1 motor
1 parent 21818a6 commit c0ceeba

File tree

1 file changed

+38
-56
lines changed

1 file changed

+38
-56
lines changed

arduino_control/arduino_control.ino

Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -121,69 +121,51 @@ void processData() {
121121
Serial.println("<");
122122
Serial.print("RevievedString: ");
123123
Serial.println(recievedString);
124-
Serial.println("Number of Rotations: ");
125-
Serial.print("m1: ");
126-
Serial.println(m1_rotation);
127-
Serial.print("m2: ");
128-
Serial.println(m2_rotation);
129-
130-
131-
// while all motors aren't ready
132-
133-
134124

135-
if (rotation_direction[0] == "Up") {
136-
Serial.println("<");
137-
Serial.print("Arduino: ");
138-
Serial.println("Moving Up!");
139-
upMotor(0);
140-
while (1) {
141-
readValues();
142-
Serial.println(sensor_value2[0]);
143-
if (sensor_value1[0] < 500 && sensor_value2[0] > 500){
144-
rotation_int[0] = rotation_int[0] - 1;
145-
}
146-
147-
if (rotation_int[0] == 0){
148-
break;
149-
}
150-
delay(500);
125+
for (int i=0; i<NUMBER_OF_MOTORS; i++) {
126+
if (rotation_direction[0] == "Up") {
127+
upMotor(i);
128+
}
129+
else if (rotation_direction[0] == "Down") {
130+
downMotor(i);
131+
}
132+
133+
else if (rotation_direction[0] == "None"){
134+
stopMotor(i);
135+
}
136+
else
137+
{
138+
Serial.println("<");
139+
Serial.print("Arduino: ");
140+
Serial.println("Invalid Input!");
151141
}
152-
stopMotor(0);
153142
}
154-
else if (rotation_direction[0] == "Down") {
155-
Serial.println("<");
156-
Serial.print("Arduino: ");
157-
Serial.println("Moving Down!");
158-
downMotor(0);
159-
while (1) {
160-
readValues();
161-
Serial.println(sensor_value2[0]);
162-
if (sensor_value1[0] < 500 && sensor_value2[0] > 500){
163-
rotation_int[0] = rotation_int[0] - 1;
143+
144+
////////////////
145+
rotation_int[1] = 0;
146+
///////////////
147+
while (1) {
148+
int count = 0;
149+
for (int i=0; i<NUMBER_OF_MOTORS; i++) {
150+
count = count + rotation_int[i];
151+
}
152+
Serial.println(count);
153+
if (count == 0) {
154+
break;
155+
}
156+
for (int i=0; i<NUMBER_OF_MOTORS; i++) {
157+
readValues(i);
158+
if (sensor_value1[i] < 500 && sensor_value2[i] > 500){
159+
rotation_int[i] = rotation_int[i] - 1;
164160
}
165-
166-
if (rotation_int[0] == 0){
167-
break;
161+
if (rotation_int[i] == 0) {
162+
stopMotor(i);
168163
}
169-
delay(500);
170164
}
171-
stopMotor(0);
172-
}
173-
174-
else if (rotation_direction[0] == "Stop"){
175-
Serial.println("<");
176-
Serial.print("Arduino: ");
177-
Serial.println("Moving Stopping!");
178-
stopMotor(0);
179-
}
180-
else
181-
{
182-
Serial.println("<");
183-
Serial.print("Arduino: ");
184-
Serial.println("Invalid Input!");
165+
delay(200);
185166
}
186167
}
168+
187169
void showNewData() {
188170
if (newData == true) {
189171
processData();
@@ -194,7 +176,7 @@ void showNewData() {
194176
}
195177
}
196178

197-
void readValues() {
179+
void readValues(int motor_number) {
198180
sensor_value1[0] = sensor_value2[0];
199181
sensor_value2[0] = analogRead(m1_sensor_pin);
200182
}

0 commit comments

Comments
 (0)