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

Commit 334603e

Browse files
Cleaning Up Code
- Added constants - Moved most variables into arrays
1 parent 6303164 commit 334603e

File tree

2 files changed

+44
-36
lines changed

2 files changed

+44
-36
lines changed

arduino_control/arduino_control.ino

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,52 @@
1+
// constants
2+
#define NUMBER_OF_MOTORS 2
3+
#define UP_SPEED 295
4+
#define DOWN_SPEED 270
5+
#define STOP_SPEED 0
16

7+
// libraries
28
#include <Wire.h>
39
#include <Adafruit_PWMServoDriver.h>
410
#include <String.h>
5-
611
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
712

8-
#define SERVOSPEED 150
913

1014
const byte numChars = 32;
1115
char receivedChars[numChars];
1216

1317
boolean newData = false;
1418

1519
String recievedString = "";
20+
int sensor_value1[NUMBER_OF_MOTORS];
21+
int sensor_value2[NUMBER_OF_MOTORS];
22+
int rotation_int[NUMBER_OF_MOTORS];
23+
String rotation_direction[NUMBER_OF_MOTORS];
1624

17-
int number_of_motors = 2;
18-
//int rotation_int[number_of_motors];
19-
// motor 1 variables
20-
int m1_sensor_pin = A0;
21-
int m1_sensor_value1 = 0;
22-
int m1_sensor_value2 = 0;
23-
int m1_rotation_int = 0;
24-
String m1_rotation_direction = "";
25-
String m1_rotation = "";
25+
// motor pins
26+
int m1_sensor_pin = A0;
27+
int m2_sensor_pin = A0;
2628

27-
int m2_sensor_pin = A0;
28-
int m2_sensor_value1 = 0;
29-
int m2_sensor_value2 = 0;
30-
int m2_rotation_int = 0;
31-
String m2_rotation_direction = "";
29+
String m1_rotation = "";
3230
String m2_rotation = "";
3331

3432
void setup() {
33+
// set array values
34+
memset(sensor_value1, 0, sizeof(sensor_value1));
35+
memset(sensor_value2, 0, sizeof(sensor_value2));
36+
37+
for (int i=0; i<NUMBER_OF_MOTORS; i++) {
38+
rotation_direction[i] = "";
39+
}
40+
41+
// setup serial monitor
3542
Serial.begin(9600);
3643
Serial.println("<Arduino is ready>");
3744

3845
pwm.begin();
3946
pwm.setPWMFreq(50);
4047
delay(10);
4148
pwm.setPWM(0, 0, 0);
49+
4250
}
4351

4452
void loop() {
@@ -87,13 +95,13 @@ void processData() {
8795

8896
recievedString = receivedChars;
8997

90-
m1_rotation_direction = getValue(recievedString, ',', 0);
98+
rotation_direction[0] = getValue(recievedString, ',', 0);
9199
m1_rotation = getValue(recievedString, ',', 1);
92-
m1_rotation_int = m1_rotation.toInt();
100+
rotation_int[0] = m1_rotation.toInt();
93101

94-
m2_rotation_direction = getValue(recievedString, ',', 2);
102+
rotation_direction[1] = getValue(recievedString, ',', 2);
95103
m2_rotation = getValue(recievedString, ',', 3);
96-
m2_rotation_int = m2_rotation.toInt();
104+
rotation_int[1] = m2_rotation.toInt();
97105

98106

99107
//serial print info
@@ -111,46 +119,46 @@ void processData() {
111119

112120

113121

114-
if (m1_rotation_direction == "Up") {
122+
if (rotation_direction[0] == "Up") {
115123
Serial.println("<");
116124
Serial.print("Arduino: ");
117125
Serial.println("Moving Up!");
118126
upMotor(0);
119127
while (1) {
120128
readValues();
121-
Serial.println(m1_sensor_value2);
122-
if (m1_sensor_value1 < 500 && m1_sensor_value2 > 500){
123-
m1_rotation_int = m1_rotation_int - 1;
129+
Serial.println(sensor_value2[0]);
130+
if (sensor_value1[0] < 500 && sensor_value2[0] > 500){
131+
rotation_int[0] = rotation_int[0] - 1;
124132
}
125133

126-
if (m1_rotation_int == 0){
134+
if (rotation_int[0] == 0){
127135
break;
128136
}
129137
delay(500);
130138
}
131139
stopMotor(0);
132140
}
133-
else if (m1_rotation_direction == "Down") {
141+
else if (rotation_direction[0] == "Down") {
134142
Serial.println("<");
135143
Serial.print("Arduino: ");
136144
Serial.println("Moving Down!");
137145
downMotor(0);
138146
while (1) {
139147
readValues();
140-
Serial.println(m1_sensor_value2);
141-
if (m1_sensor_value1 < 500 && m1_sensor_value2 > 500){
142-
m1_rotation_int = m1_rotation_int - 1;
148+
Serial.println(sensor_value2[0]);
149+
if (sensor_value1[0] < 500 && sensor_value2[0] > 500){
150+
rotation_int[0] = rotation_int[0] - 1;
143151
}
144152

145-
if (m1_rotation_int == 0){
153+
if (rotation_int[0] == 0){
146154
break;
147155
}
148156
delay(500);
149157
}
150158
stopMotor(0);
151159
}
152160

153-
else if (m1_rotation_direction == "Stop"){
161+
else if (rotation_direction[0] == "Stop"){
154162
Serial.println("<");
155163
Serial.print("Arduino: ");
156164
Serial.println("Moving Stopping!");
@@ -174,8 +182,8 @@ void showNewData() {
174182
}
175183

176184
void readValues() {
177-
m1_sensor_value1 = m1_sensor_value2;
178-
m1_sensor_value2 = analogRead(m1_sensor_pin);
185+
sensor_value1[0] = sensor_value2[0];
186+
sensor_value2[0] = analogRead(m1_sensor_pin);
179187
}
180188

181189
String getValue(String data, char separator, int index)

arduino_control/motor_movement.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
void stopMotor(int motor_number) {
2-
pwm.setPWM(motor_number, 0, 0);
2+
pwm.setPWM(motor_number, 0, STOP_SPEED);
33
}
44

55
void upMotor(int motor_number) {
6-
pwm.setPWM(motor_number, 0, 295);
6+
pwm.setPWM(motor_number, 0, UP_SPEED);
77
}
88

99
void downMotor(int motor_number) {
10-
pwm.setPWM(motor_number, 0, 270);
10+
pwm.setPWM(motor_number, 0, DOWN_SPEED);
1111
}

0 commit comments

Comments
 (0)