7
7
8
8
#define NUMBER_MOTORS 2
9
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 }};
10
+ // function declarations
11
+ void RecvWithStartEndMarkers ();
12
+ void ProcessData ();
13
+ void ShowNewData ();
14
+
15
+ // variables for communication
16
+ const byte numChars = 32 ;
17
+ char receivedChars[numChars];
18
+ bool newData = false ;
19
+
14
20
// initialize motors
15
21
Servo myServo[NUMBER_MOTORS];
22
+ // create a array of ports. Each line corresponds to the motor number starting from 0.
23
+ // the order of the ports is motor, counter, reset
24
+ int ports[NUMBER_MOTORS][3 ] = {{2 , 3 , 4 },{5 , 6 , 7 }};
25
+
26
+ String motor_direction[NUMBER_MOTORS] = {" " };;
27
+ byte motor_rotation_number[NUMBER_MOTORS] = {0 };
28
+ byte motor_sensor_counter1[NUMBER_MOTORS] = {0 };;
29
+ byte motor_sensor_counter2[NUMBER_MOTORS] = {0 };;
30
+
31
+ // 0 or 1 depending on the input signal
32
+ byte input[NUMBER_MOTORS] = {0 };
33
+ // will range from 0 to the specified MAXIMUM
34
+ int integrator[NUMBER_MOTORS] = {0 };
35
+ // cleaned-up version of the input signal
36
+ byte output[NUMBER_MOTORS]= {0 };;
16
37
17
38
void setup () {
18
39
Serial.begin (9600 );
@@ -24,7 +45,7 @@ void setup() {
24
45
int x = ports[i][0 ];\
25
46
Serial.print (x);
26
47
Serial.print (" " );
27
- pinMode (x, OUTPUT );
48
+ myServo[i]. attach (x );
28
49
}
29
50
30
51
// initialize all counter ports
@@ -52,166 +73,8 @@ void setup() {
52
73
Serial.println (" >" );
53
74
}
54
75
55
- // function declarations
56
- void RecvWithStartEndMarkers ();
57
- void ProcessData ();
58
- void ShowNewData ();
59
-
60
- // variables for communication
61
- const byte numChars = 32 ;
62
- char receivedChars[numChars];
63
- bool newData = false ;
64
-
65
- // variables for motor 1
66
- // ports
67
- Servo myservo1;
68
- byte motor_port = 3 ;
69
- byte motor_counter_port = 4 ;
70
- byte motor_reset_port = 2 ;
71
- // motor info
72
- String motor_direction = " " ;
73
- byte motor_rotation_number = 0 ;
74
- byte motor_sensor_counter1 = 0 ;
75
- byte motor_sensor_counter2 = 0 ;
76
- //
77
-
78
- byte input; /* 0 or 1 depending on the input signal */
79
- int integrator; /* Will range from 0 to the specified MAXIMUM */
80
- byte output; /* Cleaned-up version of the input signal */
81
-
82
- // ////////////////////////////////////////////////////////////////////////////
83
- // ////////////////////////////////////////////////////////////////////////////
84
-
85
- void ProcessData () {
86
-
87
- // variables
88
- String recievedString = " " ;
89
- String motor_rotation_string = " " ;
90
- String motor_rotation_string2 = " " ;
91
-
92
- recievedString = receivedChars;
93
-
94
- // logic
95
- motor_direction = getValue (recievedString, ' ,' , 0 );
96
- motor_rotation_string = getValue (recievedString, ' ,' , 1 );
97
- motor_rotation_number = motor_rotation_string.toInt ();
98
-
99
- //
100
- if (motor_direction == " Up" ) {
101
- myservo1.write (100 );
102
- }
103
- else if (motor_direction == " Down" ) {
104
- myservo1.write (80 );
105
- }
106
-
107
- else if (motor_direction == " None" ) {
108
- myservo1.write (90 );
109
- }
110
- else if (motor_direction == " Reset" ) {
111
- Reset1 ();
112
- }
113
- else
114
- {
115
- // Sends Error Message
116
- Invalid ();
117
- }
118
-
119
- // Print First Number
120
- Serial.println (" ---------" );
121
- Serial.print (" Motor 1: " );
122
- Serial.println (motor_rotation_number);
123
-
124
- while (true ) {
125
-
126
- motor_sensor_counter2 = motor_sensor_counter1;
127
- checkswitch (motor_counter_port);
128
- motor_sensor_counter1 = output;
129
- delay (10 );
130
-
131
- if (motor_rotation_number == 0 ) {
132
- myservo1.write (90 );
133
- break ;
134
- }
135
-
136
- if (digitalRead (motor_reset_port) == 0 ) {
137
- myservo1.write (90 );
138
- break ;
139
- }
140
-
141
- if (motor_sensor_counter1 == 1 && motor_sensor_counter2 == 0 ) {
142
- motor_rotation_number--;
143
-
144
- // Print Number Every Time It Changes
145
- Serial.print (" Motor 1: " );
146
- Serial.println (motor_rotation_number);
147
- }
148
- }
149
-
150
- // Send Finished Signal
151
- Finished ();
152
-
153
- }
154
-
155
- // ////////////////////////////////////////////////////////////////////////////
156
- // ////////////////////////////////////////////////////////////////////////////
157
-
158
- void checkswitch (int switchPort) {
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
161
- determined by the input state (0 or 1). */
162
- input = digitalRead (switchPort);
163
-
164
- if (input == 0 )
165
- {
166
- if (integrator > 0 )
167
- integrator--;
168
- }
169
- else if (integrator < MAXIMUM)
170
- integrator++;
171
-
172
- /* Step 2: Update the output state based on the integrator. Note that the
173
- output will only change states if the integrator has reached a limit, either
174
- 0 or MAXIMUM. */
175
-
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
- }
183
-
184
- /* ******************************************************** End of debounce.c */
76
+ void loop () {
77
+ RecvWithStartEndMarkers ();
78
+ ShowNewData ();
185
79
}
186
80
187
- // ////////////////////////////////////////////////////////////////////////////
188
- // ////////////////////////////////////////////////////////////////////////////
189
-
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