1
1
// constants
2
2
#define NUMBER_OF_MOTORS 2
3
3
#define UP_SPEED 295
4
- #define DOWN_SPEED 270
4
+ #define DOWN_SPEED 265
5
5
#define STOP_SPEED 0
6
6
7
7
// libraries
@@ -30,10 +30,7 @@ int sensor_value1[NUMBER_OF_MOTORS];
30
30
int sensor_value2[NUMBER_OF_MOTORS];
31
31
int rotation_int[NUMBER_OF_MOTORS];
32
32
String rotation_direction[NUMBER_OF_MOTORS];
33
-
34
- // motor pins
35
- int m1_sensor_pin = A0;
36
- int m2_sensor_pin = A0;
33
+ int sensor_pin[] = {A0, A2};
37
34
38
35
String m1_rotation = " " ;
39
36
String m2_rotation = " " ;
@@ -49,6 +46,9 @@ void setup() {
49
46
rotation_direction[i] = " " ;
50
47
}
51
48
49
+ pinMode (14 , INPUT);
50
+ pinMode (15 , INPUT);
51
+
52
52
// setup serial monitor
53
53
Serial.begin (9600 );
54
54
@@ -123,41 +123,62 @@ void processData() {
123
123
Serial.println (recievedString);
124
124
125
125
for (int i=0 ; i<NUMBER_OF_MOTORS; i++) {
126
- if (rotation_direction[0 ] == " Up" ) {
126
+ if (rotation_direction[i ] == " Up" ) {
127
127
upMotor (i);
128
128
}
129
- else if (rotation_direction[0 ] == " Down" ) {
129
+ else if (rotation_direction[i ] == " Down" ) {
130
130
downMotor (i);
131
131
}
132
132
133
- else if (rotation_direction[0 ] == " None" ){
133
+ else if (rotation_direction[i ] == " None" ){
134
134
stopMotor (i);
135
135
}
136
136
else
137
137
{
138
+ // TODO: make this exit the program
138
139
Serial.println (" <" );
139
140
Serial.print (" Arduino: " );
140
141
Serial.println (" Invalid Input!" );
141
142
}
143
+
142
144
}
143
-
144
145
// //////////////
145
- rotation_int[1 ] = 0 ;
146
+ // rotation_int[1] = 0;
146
147
// /////////////
148
+
147
149
while (1 ) {
148
150
int count = 0 ;
151
+
149
152
for (int i=0 ; i<NUMBER_OF_MOTORS; i++) {
150
- count = count + rotation_int[i];
153
+ count = count + rotation_int[i];
154
+ // Serial.print("Motor: ");
155
+ // Serial.println(i);
156
+ // Serial.print("Rotations: ");
157
+ // Serial.println(rotation_int[i]);
151
158
}
152
- Serial. println (count);
153
- if (count = = 0 ) {
159
+
160
+ if (count < = 0 ) {
154
161
break ;
155
162
}
163
+
156
164
for (int i=0 ; i<NUMBER_OF_MOTORS; i++) {
165
+ if (rotation_int[i] == 0 ) {
166
+ stopMotor (i);
167
+ }
157
168
readValues (i);
169
+
158
170
if (sensor_value1[i] < 500 && sensor_value2[i] > 500 ){
159
171
rotation_int[i] = rotation_int[i] - 1 ;
172
+ // Serial.print("Sensor ");
173
+ // Serial.print(i);
174
+ // Serial.print(" Value: ");
175
+ // Serial.println(sensor_value2[i]);
176
+ if (rotation_int[i] < 0 ) {
177
+ rotation_int[i] = 0 ;
178
+ }
160
179
}
180
+ }
181
+ for (int i=0 ; i<NUMBER_OF_MOTORS; i++) {
161
182
if (rotation_int[i] == 0 ) {
162
183
stopMotor (i);
163
184
}
@@ -177,8 +198,8 @@ void showNewData() {
177
198
}
178
199
179
200
void readValues (int motor_number) {
180
- sensor_value1[0 ] = sensor_value2[0 ];
181
- sensor_value2[0 ] = analogRead (m1_sensor_pin );
201
+ sensor_value1[motor_number ] = sensor_value2[motor_number ];
202
+ sensor_value2[motor_number ] = analogRead (sensor_pin[motor_number] );
182
203
}
183
204
184
205
String getValue (String data, char separator, int index)
0 commit comments