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

Commit 18d4739

Browse files
Added Incomming String Parcing and Updated Variable Names
1 parent 5d8d825 commit 18d4739

File tree

3 files changed

+75
-52
lines changed

3 files changed

+75
-52
lines changed

arduino_controller/arduino_controller.ino

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,22 @@
1010
// function declarations
1111
void RecvWithStartEndMarkers();
1212
void ProcessData();
13-
void ShowNewData();
13+
void CheckSwitch();
14+
void PopulateArray();
1415

1516
// variables for communication
16-
const byte numChars = 32;
17-
char receivedChars[numChars];
18-
bool newData = false;
17+
const byte num_chars = 32;
18+
char received_chars[num_chars];
19+
bool new_data = false;
1920

2021
// initialize motors
21-
Servo myServo[NUMBER_MOTORS];
22+
Servo my_servo[NUMBER_MOTORS];
2223
// create a array of ports with the order: motor, counter, reset
2324
int ports[NUMBER_MOTORS][3] = {{2, 3, 4}, {5, 6, 7}};
2425

25-
// string array of motor directions
26-
String motor_direction[NUMBER_MOTORS] = {""};
26+
// integer array that contains the direction and number of rotations a motor needs to go
27+
int motor_commands[NUMBER_MOTORS][2] = {0};
2728

28-
// array of how many rotations a motor has to go
29-
byte motor_rotation_number[NUMBER_MOTORS] = {0};
3029
// array of new switch values
3130
byte motor_sensor_counter1[NUMBER_MOTORS] = {0};
3231

@@ -53,7 +52,7 @@ void setup()
5352
int x = ports[i][0];
5453
Serial.print(x);
5554
Serial.print(" ");
56-
myServo[i].attach(x);
55+
my_servo[i].attach(x);
5756
}
5857

5958
// initialize all counter ports
@@ -89,12 +88,12 @@ void loop()
8988
RecvWithStartEndMarkers();
9089

9190
// if there is new data process it
92-
if (newData == true)
91+
if (new_data == true)
9392
{
94-
newData = false;
93+
new_data = false;
9594
Serial.println("<");
9695
Serial.print("Arduino: ");
97-
Serial.println(receivedChars);
96+
Serial.println(received_chars);
9897
ProcessData();
9998
}
10099
}

arduino_controller/arduino_controller_functions.ino

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,9 @@
99

1010
void ProcessData() {
1111

12-
/* // variables
13-
String recievedString = "";
14-
String motor_rotation_string = "";
15-
String motor_rotation_string2 = "";
16-
17-
recievedString = receivedChars;
18-
19-
// logic
20-
motor_direction = getValue(recievedString, ',', 0);
21-
motor_rotation_string = getValue(recievedString, ',', 1);
22-
motor_rotation_number = motor_rotation_string.toInt();
23-
24-
//
25-
if (motor_direction == "Up") {
26-
myservo1.write(100);
27-
}
28-
else if (motor_direction == "Down") {
29-
myservo1.write(80);
30-
}
31-
32-
else if (motor_direction == "None") {
33-
myservo1.write(90);
34-
}
35-
else if (motor_direction == "Reset") {
36-
Reset1();
37-
}
38-
else
39-
{
40-
// Sends Error Message
41-
Invalid();
42-
}
43-
12+
PopulateArray();
13+
14+
/*
4415
// Print First Number
4516
Serial.println("---------");
4617
Serial.print("Motor 1: ");
@@ -79,7 +50,60 @@ void ProcessData() {
7950
//////////////////////////////////////////////////////////////////////////////
8051
//////////////////////////////////////////////////////////////////////////////
8152

82-
void checkswitch(int switchPort) {
53+
void PopulateArray() {
54+
55+
//
56+
String recieved_string = "";
57+
recieved_string = received_chars;
58+
59+
for (int i = 0; i < NUMBER_MOTORS; i++)
60+
{
61+
int search1 = (i * 2);
62+
int search2 = ((i * 2 ) + 1);
63+
64+
String value1 = getValue(recieved_string, ',', search1);
65+
String value2 = getValue(recieved_string, ',', search2);
66+
67+
if (value1 == "Up") {
68+
motor_commands[i][1] = 0;
69+
}
70+
else if (value1 == "Down") {
71+
motor_commands[i][1] = 1;
72+
}
73+
74+
else if (value1 == "None") {
75+
motor_commands[i][1] = 2;
76+
}
77+
else if (value1 == "Reset") {
78+
motor_commands[i][1] = 3;
79+
}
80+
else
81+
{
82+
// Sends Error Message
83+
Invalid();
84+
}
85+
86+
motor_commands[i][2] = value2.toInt();
87+
}
88+
89+
// print array
90+
for (int i = 0; i < NUMBER_MOTORS; i++)
91+
{
92+
Serial.print("Motor Number: ");
93+
Serial.print(i);
94+
Serial.print(" - ");
95+
Serial.print(motor_commands[i][1]);
96+
Serial.print(" - ");
97+
Serial.println(motor_commands[i][2]);
98+
}
99+
100+
101+
}
102+
103+
//////////////////////////////////////////////////////////////////////////////
104+
//////////////////////////////////////////////////////////////////////////////
105+
106+
void CheckSwitch(int switchPort) {
83107
// /* Step 1: Update the integrator based on the input signal. Note that the
84108
// integrator follows the input, decreasing or increasing towards the limits
85109
// as determined by the input state (0 or 1). */

arduino_controller/communications.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,27 @@ void RecvWithStartEndMarkers()
1616
char endMarker = '>';
1717
char rc;
1818

19-
while (Serial.available() > 0 && newData == false)
19+
while (Serial.available() > 0 && new_data == false)
2020
{
2121
rc = Serial.read();
2222

2323
if (recvInProgress == true)
2424
{
2525
if (rc != endMarker)
2626
{
27-
receivedChars[ndx] = rc;
27+
received_chars[ndx] = rc;
2828
ndx++;
29-
if (ndx >= numChars)
29+
if (ndx >= num_chars)
3030
{
31-
ndx = numChars - 1;
31+
ndx = num_chars - 1;
3232
}
3333
}
3434
else
3535
{
36-
receivedChars[ndx] = '\0'; // terminate the string
36+
received_chars[ndx] = '\0'; // terminate the string
3737
recvInProgress = false;
3838
ndx = 0;
39-
newData = true;
39+
new_data = true;
4040
}
4141
}
4242

0 commit comments

Comments
 (0)