1
+ // constants
2
+ #define NUMBER_OF_MOTORS 2
3
+ #define UP_SPEED 295
4
+ #define DOWN_SPEED 270
5
+ #define STOP_SPEED 0
1
6
7
+ // libraries
2
8
#include < Wire.h>
3
9
#include < Adafruit_PWMServoDriver.h>
4
10
#include < String.h>
5
-
6
11
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
7
12
8
- #define SERVOSPEED 150
9
13
10
14
const byte numChars = 32 ;
11
15
char receivedChars[numChars];
12
16
13
17
boolean newData = false ;
14
18
15
19
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];
16
24
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;
26
28
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 = " " ;
32
30
String m2_rotation = " " ;
33
31
34
32
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
35
42
Serial.begin (9600 );
36
43
Serial.println (" <Arduino is ready>" );
37
44
38
45
pwm.begin ();
39
46
pwm.setPWMFreq (50 );
40
47
delay (10 );
41
48
pwm.setPWM (0 , 0 , 0 );
49
+
42
50
}
43
51
44
52
void loop () {
@@ -87,13 +95,13 @@ void processData() {
87
95
88
96
recievedString = receivedChars;
89
97
90
- m1_rotation_direction = getValue (recievedString, ' ,' , 0 );
98
+ rotation_direction[ 0 ] = getValue (recievedString, ' ,' , 0 );
91
99
m1_rotation = getValue (recievedString, ' ,' , 1 );
92
- m1_rotation_int = m1_rotation.toInt ();
100
+ rotation_int[ 0 ] = m1_rotation.toInt ();
93
101
94
- m2_rotation_direction = getValue (recievedString, ' ,' , 2 );
102
+ rotation_direction[ 1 ] = getValue (recievedString, ' ,' , 2 );
95
103
m2_rotation = getValue (recievedString, ' ,' , 3 );
96
- m2_rotation_int = m2_rotation.toInt ();
104
+ rotation_int[ 1 ] = m2_rotation.toInt ();
97
105
98
106
99
107
// serial print info
@@ -111,46 +119,46 @@ void processData() {
111
119
112
120
113
121
114
- if (m1_rotation_direction == " Up" ) {
122
+ if (rotation_direction[ 0 ] == " Up" ) {
115
123
Serial.println (" <" );
116
124
Serial.print (" Arduino: " );
117
125
Serial.println (" Moving Up!" );
118
126
upMotor (0 );
119
127
while (1 ) {
120
128
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 ;
124
132
}
125
133
126
- if (m1_rotation_int == 0 ){
134
+ if (rotation_int[ 0 ] == 0 ){
127
135
break ;
128
136
}
129
137
delay (500 );
130
138
}
131
139
stopMotor (0 );
132
140
}
133
- else if (m1_rotation_direction == " Down" ) {
141
+ else if (rotation_direction[ 0 ] == " Down" ) {
134
142
Serial.println (" <" );
135
143
Serial.print (" Arduino: " );
136
144
Serial.println (" Moving Down!" );
137
145
downMotor (0 );
138
146
while (1 ) {
139
147
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 ;
143
151
}
144
152
145
- if (m1_rotation_int == 0 ){
153
+ if (rotation_int[ 0 ] == 0 ){
146
154
break ;
147
155
}
148
156
delay (500 );
149
157
}
150
158
stopMotor (0 );
151
159
}
152
160
153
- else if (m1_rotation_direction == " Stop" ){
161
+ else if (rotation_direction[ 0 ] == " Stop" ){
154
162
Serial.println (" <" );
155
163
Serial.print (" Arduino: " );
156
164
Serial.println (" Moving Stopping!" );
@@ -174,8 +182,8 @@ void showNewData() {
174
182
}
175
183
176
184
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);
179
187
}
180
188
181
189
String getValue (String data, char separator, int index)
0 commit comments