code:
#include <Servo.h>
const byte numChars = 80;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
int rightShoulderX = 0;
int rightShoulderY = 0;
int rightElbowX = 0;
int rightElbowY = 0;
int joystickX = 0;
int joystickY = 0;
int left;
int right;
int pos;
Servo turn;
Servo l;
Servo r;
boolean newData = false;
//============
void setup() {
Serial.begin(9600);
turn.attach(4);
turn.write(90);
l.attach(5);
r.attach(6);
delay(1000);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(24, INPUT);
pinMode(26, INPUT);
pinMode(28, INPUT);
pinMode(30, INPUT);
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
if (joystickY > 0) {
digitalWrite(30, LOW);
digitalWrite(28, HIGH);
digitalWrite(26, LOW);
digitalWrite(24, HIGH);
} else if (joystickY < 0) {
digitalWrite(30, HIGH);
digitalWrite(28, LOW);
digitalWrite(26, HIGH);
digitalWrite(24, LOW);
}
Serial.println(pos);
newData = false;
}
left = map(joystickX, 70, 110, -255, 255);
left = abs(left);
joystickY = abs(joystickY);
right = (255 - joystickY) - (255 - left);
if (joystickX < 90) {
analogWrite(2, joystickY);
analogWrite(3, joystickY + right);
} else if (joystickX > 90) {
analogWrite(2, joystickY + right);
analogWrite(3, joystickY);
} else
;
{
analogWrite(2, joystickY);
analogWrite(3, joystickY);
}
if (pos == HIGH) {
l.write(95);
r.write(85);
} else
;
{
l.write(5);
r.write(175);
}
turn.write(joystickX);
delay(5);
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
} else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char* strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ","); // this continues where the previous call left off
rightShoulderX = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ",");
rightShoulderY = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
rightElbowX = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
rightElbowY = atoi(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ",");
joystickX = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
joystickY = atoi(strtokIndx);
strtokIndx = strtok(NULL, ",");
pos = atoi(strtokIndx);
}
im sending ints from one arduino to another via hc 05 bluetooth module. I have everything working. The servos are all moving how they r suppose to. But whenever i open the serial moniter everything just stops and nothing is being read by the hc 05. the other arduino that is sending the data works just fine and i can open the serial moniter. but not for this arduino mega.
Sorry if the code is kinda hard to read.