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

Commit bf9a4b1

Browse files
Makeing Code More Dynamic/ Clean Up
1 parent b784e49 commit bf9a4b1

File tree

7 files changed

+183
-156
lines changed

7 files changed

+183
-156
lines changed
3.42 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"sketch": "ModulusSprint3.3.ino",
3+
"board": "arduino:avr:megaADK",
4+
"port": "/dev/cu.usbmodem143101",
5+
"programmer": "AVRISP mkII"
6+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Mac",
5+
"includePath": [
6+
"/Users/harlenbains/Library/Arduino15/packages/arduino/tools/**",
7+
"/Users/harlenbains/Library/Arduino15/packages/arduino/hardware/avr/1.6.21/**"
8+
],
9+
"forcedInclude": [
10+
"/Users/harlenbains/Library/Arduino15/packages/arduino/hardware/avr/1.6.21/cores/arduino/Arduino.h"
11+
],
12+
"macFrameworkPath": [
13+
"/System/Library/Frameworks",
14+
"/Library/Frameworks"
15+
],
16+
"intelliSenseMode": "clang-x64"
17+
}
18+
],
19+
"version": 4
20+
}

arduino_control/ModulusSprint3.3/ModulusSprint3.3.ino

Lines changed: 127 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,53 @@
55
#define SAMPLE_FREQUENCY 20
66
#define MAXIMUM (DEBOUNCE_TIME * SAMPLE_FREQUENCY)
77

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+
855
// function declarations
956
void RecvWithStartEndMarkers();
1057
void ProcessData();
@@ -26,194 +73,145 @@ String motor_direction = "";
2673
byte motor_rotation_number = 0;
2774
byte motor_sensor_counter1 = 0;
2875
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;
4076
//
77+
4178
byte input; /* 0 or 1 depending on the input signal */
4279
int integrator; /* Will range from 0 to the specified MAXIMUM */
4380
byte output; /* Cleaned-up version of the input signal */
4481

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-
4982
//////////////////////////////////////////////////////////////////////////////
5083
//////////////////////////////////////////////////////////////////////////////
5184

5285
void ProcessData() {
53-
86+
5487
// variables
5588
String recievedString = "";
5689
String motor_rotation_string = "";
5790
String motor_rotation_string2 = "";
58-
// logic
91+
5992
recievedString = receivedChars;
60-
motor_direction = getValue(recievedString, ',', 0);
93+
94+
// logic
95+
motor_direction = getValue(recievedString, ',', 0);
6196
motor_rotation_string = getValue(recievedString, ',', 1);
6297
motor_rotation_number = motor_rotation_string.toInt();
6398

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+
//
70100
if (motor_direction == "Up") {
71101
myservo1.write(100);
72102
}
73103
else if (motor_direction == "Down") {
74-
myservo1.write(80);
104+
myservo1.write(80);
75105
}
76106

77-
else if (motor_direction == "None"){
107+
else if (motor_direction == "None") {
78108
myservo1.write(90);
79109
}
80-
else if (motor_direction == "Reset"){
110+
else if (motor_direction == "Reset") {
81111
Reset1();
82112
}
83113
else
84114
{
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();
89117
}
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+
93126
motor_sensor_counter2 = motor_sensor_counter1;
94127
checkswitch(motor_counter_port);
95128
motor_sensor_counter1 = output;
96129
delay(10);
97-
98-
if(motor_rotation_number == 0) {
130+
131+
if (motor_rotation_number == 0) {
99132
myservo1.write(90);
100133
break;
101134
}
102-
103-
if(digitalRead(motor_reset_port) == 0){
135+
136+
if (digitalRead(motor_reset_port) == 0) {
104137
myservo1.write(90);
105138
break;
106139
}
107-
108-
if(motor_sensor_counter1 == 1 && motor_sensor_counter2 == 0) {
140+
141+
if (motor_sensor_counter1 == 1 && motor_sensor_counter2 == 0) {
109142
motor_rotation_number--;
110-
}
111-
}
112143

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);
152147
}
153148
}
154-
149+
150+
// Send Finished Signal
151+
Finished();
152+
155153
}
156154

157155
//////////////////////////////////////////////////////////////////////////////
158156
//////////////////////////////////////////////////////////////////////////////
159157

160158
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
163161
determined by the input state (0 or 1). */
164-
input = digitalRead(switchPort);
162+
input = digitalRead(switchPort);
165163

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++;
173171

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
175173
output will only change states if the integrator has reached a limit, either
176174
0 or MAXIMUM. */
177175

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+
}
185183

186-
/********************************************************* End of debounce.c */
184+
/********************************************************* End of debounce.c */
187185
}
188186

189187
//////////////////////////////////////////////////////////////////////////////
190188
//////////////////////////////////////////////////////////////////////////////
191189

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

Comments
 (0)