5
5
#define SAMPLE_FREQUENCY 20
6
6
#define MAXIMUM (DEBOUNCE_TIME * SAMPLE_FREQUENCY)
7
7
8
+ #define NUMBER_MOTORS 4
9
+
10
+ // create a array of ports. Each line corresponds to the motor number starting from 0
11
+ // ie 0 is motor 1
12
+ // the order of the ports is motor, counter, reset
13
+ int ports[NUMBER_MOTORS][3 ] = {{2 , 3 , 4 },{5 , 6 , 7 }};
14
+ // initialize motors
15
+ Servo myServo[NUMBER_MOTORS];
16
+
17
+ void setup () {
18
+ Serial.begin (9600 );
19
+
20
+ // initialize all motor ports
21
+ Serial.println (" Begining Initialization" );
22
+ Serial.print (" Motor Ports: " );
23
+ for (int i = 0 ; i < NUMBER_MOTORS; i++) {
24
+ int x = ports[i][0 ];\
25
+ Serial.print (x);
26
+ Serial.print (" " );
27
+ pinMode (x, OUTPUT);
28
+ }
29
+
30
+ // initialize all counter ports
31
+ Serial.println (" " );
32
+ Serial.print (" Counter Ports: " );
33
+ for (int i = 0 ; i < NUMBER_MOTORS; i++) {
34
+ int x = ports[i][1 ];
35
+ Serial.print (x);
36
+ Serial.print (" " );
37
+ pinMode (x, INPUT_PULLUP);
38
+ }
39
+
40
+ // initialize all reset ports
41
+ Serial.println (" " );
42
+ Serial.print (" Reset Ports: " );
43
+ for (int i = 0 ; i < NUMBER_MOTORS; i++) {
44
+ int x = ports[i][2 ];
45
+ Serial.print (x);
46
+ Serial.print (" " );
47
+ pinMode (x, INPUT_PULLUP);
48
+ }
49
+ Serial.println (" " );
50
+ Serial.println (" <" );
51
+ Serial.println (" Arduino is ready" );
52
+ Serial.println (" >" );
53
+ }
54
+
8
55
// function declarations
9
56
void RecvWithStartEndMarkers ();
10
57
void ProcessData ();
@@ -26,194 +73,145 @@ String motor_direction = "";
26
73
byte motor_rotation_number = 0 ;
27
74
byte motor_sensor_counter1 = 0 ;
28
75
byte motor_sensor_counter2 = 0 ;
29
- //
30
- // variables for motor 2
31
- Servo myservo2;
32
- byte motor_port2 = 9 ;
33
- byte motor_counter_port2 = 8 ;
34
- byte motor_reset_port2 = 7 ;
35
- // motor info
36
- String motor_direction2 = " " ;
37
- byte motor_rotation_number2 = 0 ;
38
- byte motor_sensor_counter12 = 0 ;
39
- byte motor_sensor_counter22 = 0 ;
40
76
//
77
+
41
78
byte input; /* 0 or 1 depending on the input signal */
42
79
int integrator; /* Will range from 0 to the specified MAXIMUM */
43
80
byte output; /* Cleaned-up version of the input signal */
44
81
45
- byte input2; /* 0 or 1 depending on the input signal */
46
- int integrator2; /* Will range from 0 to the specified MAXIMUM */
47
- byte output2; /* Cleaned-up version of the input signal */
48
-
49
82
// ////////////////////////////////////////////////////////////////////////////
50
83
// ////////////////////////////////////////////////////////////////////////////
51
84
52
85
void ProcessData () {
53
-
86
+
54
87
// variables
55
88
String recievedString = " " ;
56
89
String motor_rotation_string = " " ;
57
90
String motor_rotation_string2 = " " ;
58
- // logic
91
+
59
92
recievedString = receivedChars;
60
- motor_direction = getValue (recievedString, ' ,' , 0 );
93
+
94
+ // logic
95
+ motor_direction = getValue (recievedString, ' ,' , 0 );
61
96
motor_rotation_string = getValue (recievedString, ' ,' , 1 );
62
97
motor_rotation_number = motor_rotation_string.toInt ();
63
98
64
- //
65
- motor_direction2 = getValue (recievedString, ' ,' , 2 );
66
- motor_rotation_string2 = getValue (recievedString, ' ,' , 3 );
67
- motor_rotation_number2 = motor_rotation_string2.toInt ();
68
- //
69
-
99
+ //
70
100
if (motor_direction == " Up" ) {
71
101
myservo1.write (100 );
72
102
}
73
103
else if (motor_direction == " Down" ) {
74
- myservo1.write (80 );
104
+ myservo1.write (80 );
75
105
}
76
106
77
- else if (motor_direction == " None" ){
107
+ else if (motor_direction == " None" ) {
78
108
myservo1.write (90 );
79
109
}
80
- else if (motor_direction == " Reset" ){
110
+ else if (motor_direction == " Reset" ) {
81
111
Reset1 ();
82
112
}
83
113
else
84
114
{
85
- // TODO: make this exit the program
86
- Serial.println (" <" );
87
- Serial.print (" Arduino: " );
88
- Serial.println (" Invalid Input!" );
115
+ // Sends Error Message
116
+ Invalid ();
89
117
}
90
-
91
- while (true ) {
92
-
118
+
119
+ // Print First Number
120
+ Serial.println (" ---------" );
121
+ Serial.print (" Motor 1: " );
122
+ Serial.println (motor_rotation_number);
123
+
124
+ while (true ) {
125
+
93
126
motor_sensor_counter2 = motor_sensor_counter1;
94
127
checkswitch (motor_counter_port);
95
128
motor_sensor_counter1 = output;
96
129
delay (10 );
97
-
98
- if (motor_rotation_number == 0 ) {
130
+
131
+ if (motor_rotation_number == 0 ) {
99
132
myservo1.write (90 );
100
133
break ;
101
134
}
102
-
103
- if (digitalRead (motor_reset_port) == 0 ){
135
+
136
+ if (digitalRead (motor_reset_port) == 0 ) {
104
137
myservo1.write (90 );
105
138
break ;
106
139
}
107
-
108
- if (motor_sensor_counter1 == 1 && motor_sensor_counter2 == 0 ) {
140
+
141
+ if (motor_sensor_counter1 == 1 && motor_sensor_counter2 == 0 ) {
109
142
motor_rotation_number--;
110
- }
111
- }
112
143
113
- if (motor_direction2 == " Up" ) {
114
- myservo2.write (100 );
115
- }
116
- else if (motor_direction2 == " Down" ) {
117
- myservo2.write (80 );
118
- }
119
- else if (motor_direction2 == " None" ){
120
- myservo2.write (90 );
121
- }
122
- else if (motor_direction2 == " Reset" ){
123
- Reset2 ();
124
- }
125
- else
126
- {
127
- // TODO: make this exit the program
128
- Serial.println (" <" );
129
- Serial.print (" Arduino: " );
130
- Serial.println (" Invalid Input!" );
131
- }
132
-
133
- while (true ) {
134
-
135
- motor_sensor_counter22 = motor_sensor_counter12;
136
- checkswitch2 (motor_counter_port2);
137
- motor_sensor_counter12 = output2;
138
- delay (10 );
139
-
140
- if (motor_rotation_number2 == 0 ) {
141
- myservo2.write (90 );
142
- break ;
143
- }
144
-
145
- if (digitalRead (motor_reset_port2) == 0 ){
146
- myservo2.write (90 );
147
- break ;
148
- }
149
-
150
- if (motor_sensor_counter12 == 1 && motor_sensor_counter22 == 0 ) {
151
- motor_rotation_number2--;
144
+ // Print Number Every Time It Changes
145
+ Serial.print (" Motor 1: " );
146
+ Serial.println (motor_rotation_number);
152
147
}
153
148
}
154
-
149
+
150
+ // Send Finished Signal
151
+ Finished ();
152
+
155
153
}
156
154
157
155
// ////////////////////////////////////////////////////////////////////////////
158
156
// ////////////////////////////////////////////////////////////////////////////
159
157
160
158
void checkswitch (int switchPort) {
161
- /* Step 1: Update the integrator based on the input signal. Note that the
162
- integrator follows the input, decreasing or increasing towards the limits as
159
+ /* Step 1: Update the integrator based on the input signal. Note that the
160
+ integrator follows the input, decreasing or increasing towards the limits as
163
161
determined by the input state (0 or 1). */
164
- input = digitalRead (switchPort);
162
+ input = digitalRead (switchPort);
165
163
166
- if (input == 0 )
167
- {
168
- if (integrator > 0 )
169
- integrator--;
170
- }
171
- else if (integrator < MAXIMUM)
172
- integrator++;
164
+ if (input == 0 )
165
+ {
166
+ if (integrator > 0 )
167
+ integrator--;
168
+ }
169
+ else if (integrator < MAXIMUM)
170
+ integrator++;
173
171
174
- /* Step 2: Update the output state based on the integrator. Note that the
172
+ /* Step 2: Update the output state based on the integrator. Note that the
175
173
output will only change states if the integrator has reached a limit, either
176
174
0 or MAXIMUM. */
177
175
178
- if (integrator == 0 )
179
- output = 0 ;
180
- else if (integrator >= MAXIMUM)
181
- {
182
- output = 1 ;
183
- integrator = MAXIMUM; /* defensive code if integrator got corrupted */
184
- }
176
+ if (integrator == 0 )
177
+ output = 0 ;
178
+ else if (integrator >= MAXIMUM)
179
+ {
180
+ output = 1 ;
181
+ integrator = MAXIMUM; /* defensive code if integrator got corrupted */
182
+ }
185
183
186
- /* ******************************************************** End of debounce.c */
184
+ /* ******************************************************** End of debounce.c */
187
185
}
188
186
189
187
// ////////////////////////////////////////////////////////////////////////////
190
188
// ////////////////////////////////////////////////////////////////////////////
191
189
192
- void checkswitch2 (int switchPort) {
193
- /* Step 1: Update the integrator based on the input signal. Note that the
194
- integrator follows the input, decreasing or increasing towards the limits as
195
- determined by the input state (0 or 1). */
196
- input2 = digitalRead (switchPort);
197
-
198
- if (input2 == 0 )
199
- {
200
- if (integrator2 > 0 )
201
- integrator2--;
202
- }
203
- else if (integrator2 < MAXIMUM)
204
- integrator2++;
205
-
206
- /* Step 2: Update the output state based on the integrator. Note that the
207
- output will only change states if the integrator has reached a limit, either
208
- 0 or MAXIMUM. */
209
-
210
- if (integrator2 == 0 )
211
- output2 = 0 ;
212
- else if (integrator2 >= MAXIMUM)
213
- {
214
- output2 = 1 ;
215
- integrator2 = MAXIMUM; /* defensive code if integrator got corrupted */
216
- }
217
-
218
- /* ******************************************************** End of debounce.c */
219
- }
190
+ // void checkswitch2(int switchPort) {
191
+ // /* Step 1: Update the integrator based on the input signal. Note that the
192
+ // integrator follows the input, decreasing or increasing towards the limits as
193
+ // determined by the input state (0 or 1). */
194
+ // input2 = digitalRead(switchPort);
195
+ //
196
+ // if (input2 == 0)
197
+ // {
198
+ // if (integrator2 > 0)
199
+ // integrator2--;
200
+ // }
201
+ // else if (integrator2 < MAXIMUM)
202
+ // integrator2++;
203
+ //
204
+ // /* Step 2: Update the output state based on the integrator. Note that the
205
+ // output will only change states if the integrator has reached a limit, either
206
+ // 0 or MAXIMUM. */
207
+ //
208
+ // if (integrator2 == 0)
209
+ // output2 = 0;
210
+ // else if (integrator2 >= MAXIMUM)
211
+ // {
212
+ // output2 = 1;
213
+ // integrator2 = MAXIMUM; /* defensive code if integrator got corrupted */
214
+ // }
215
+ //
216
+ // /********************************************************* End of debounce.c */
217
+ // }
0 commit comments