I am trying to code a Arduino board that receives a string and stores for 10 inputs from another Arduino. When I connect the Arduino everything works great for about 30 seconds, after that the output changes and the code no longer talks to my motors and the code does not work if anyone can help me that would be great: here is my code: #include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11); // RX, TX
#include <Servo.h>
Servo servo1; // create servo object to control a servo
Servo servo2; // servo 2 gripper rotation button 2 and 4
Servo servo3; // servo 3 gripper angle button 2 and 4
int servoPos1 = 130; // variable to store the current position of the servo
int servoPos2 = 160; // variable to store the current position of the servo
int servoPos3 = 10; // variable to store the current position of the servo
int increment = 1; // increment of servo position
int delayTime = 20; // delay time in milliseconds
// Define relay pins
const int relay1 = 30;
const int relay2 = 31;
// Define motor control pins
int motorAPinIN1 = 22;
int motorAPinIN2 = 23;
int motorAPinEN = 2;
int motorBPinIN3 = 24;
int motorBPinIN4 = 25;
int motorBPinEN = 3;
int motorAPinIN1LEFT = 22;
int motorAPinIN2LEFT = 23;
int motorAPinENLEFT = 2;
int motorBPinIN3LEFT = 24;
int motorBPinIN4LEFT = 25;
int motorBPinENLEFT = 3;
int motorCPinIN1 = 26;
int motorCPinIN2 = 27;
int motorCPinEN = 4;
int motorDPinIN3 = 28;
int motorDPinIN4 = 29;
int motorDPinEN = 5;
const byte numInputsExpected = 10; // Set the expected number of inputs as a constant
const byte maxNumInputs = 10; // Set the maximum number of inputs as a constant
int* parseInputs(String inputString, byte & numInputs) { // Use byte instead of int for the number of inputs
// Remove the first and last characters of the input string, which are "<" and ">"
inputString = inputString.substring(1, inputString.length() - 1);
// Remove any spaces from the input string
inputString.replace(" ", "");
// Count the number of commas to determine the number of inputs
numInputs = 1;
byte inputCounter = 1; // Initialize the input counter to 1
for (int i = 0; i < inputString.length(); i++) {
if (inputString.charAt(i) == ',') {
numInputs++;
inputCounter++; // Increment the input counter
}
if (inputCounter > maxNumInputs) { // Check if the input counter exceeds the limit
numInputs = 0;
inputCounter = 1; // Reset the input counter
break; // Exit the loop
}
}
// Check if the number of inputs is as expected
if (numInputs != numInputsExpected) {
numInputs = 0;
return NULL;
}
// Allocate an array to store the input values
int* inputs = new int[numInputs];
// Parse the input string and store the values in the array
int currentIndex = 0;
for (int i = 0; i < numInputs; i++) {
int nextIndex = inputString.indexOf(',', currentIndex);
if (nextIndex == -1) {
// If there are less inputs than expected, return an error
if (i != numInputs-1) {
numInputs = -1;
delete[] inputs;
return NULL;
}
nextIndex = inputString.length();
}
String inputValueString = inputString.substring(currentIndex, nextIndex);
// If the input value cannot be parsed as an integer, return an error
int inputValue;
if (!inputValueString.toInt() && inputValueString != "0" && inputValueString != "-0") {
numInputs = -1;
delete[] inputs;
return NULL;
}
inputValue = inputValueString.toInt();
inputs[i] = inputValue;
currentIndex = nextIndex + 1;
}
// Free memory allocated for the input string
String* inputStringPtr = new String(inputString);
delete inputStringPtr;
return inputs;
}
void setup() {
Serial.begin(57600); // Initialize serial communication for debugging
// Initialize relay pins
pinMode(relay1, OUTPUT); // Relay 1 control pin
pinMode(relay2, OUTPUT); // Relay 2 control pin
// Initialize motor control pins
pinMode(motorAPinIN1, OUTPUT);
pinMode(motorAPinIN2, OUTPUT);
pinMode(motorAPinEN, OUTPUT);
pinMode(motorBPinIN3, OUTPUT);
pinMode(motorBPinIN4, OUTPUT);
pinMode(motorBPinEN, OUTPUT);
pinMode(motorAPinIN1LEFT, OUTPUT);
pinMode(motorAPinIN2LEFT, OUTPUT);
pinMode(motorAPinENLEFT, OUTPUT);
pinMode(motorBPinIN3LEFT, OUTPUT);
pinMode(motorBPinIN4LEFT, OUTPUT);
pinMode(motorBPinENLEFT, OUTPUT);
pinMode(motorCPinIN1, OUTPUT);
pinMode(motorCPinIN2, OUTPUT);
pinMode(motorCPinEN, OUTPUT);
pinMode(motorDPinIN3, OUTPUT);
pinMode(motorDPinIN4, OUTPUT);
pinMode(motorDPinEN, OUTPUT);
// Servos
servo1.attach(8); // attaches the servo1 on pin 9 to the servo object
servo1.write(servoPos1); // initialize servo to start at 90 degrees
servo2.attach(6); // attach servo 2 to pin 34 rotate gripper
servo2.write(servoPos2); // initialize servo to start at 90 degrees
servo3.attach(7); // attach servo 2 to pin 34 rotate gripper
servo3.write(servoPos3); // initialize servo to start at 90 degrees
}
void loop() {
if (Serial.available()) {
String inputString = Serial.readStringUntil('\n'); // read the string
Serial.println(inputString); // print the string for debugging
// Parse input values
byte numInputs;
int* inputs = parseInputs(inputString, numInputs);
if (numInputs == 0) {
Serial.println("Error: Expected " + String(numInputsExpected) + " inputs, received " + String(inputs ? abs(numInputs) : 0));
} else {
// Extract the input values and do something with them...
const int joystick1X = inputs[0];
const int joystick1Y = inputs[1];
const int joystick2X = inputs[2];
const int joystick2Y = inputs[3];
bool button1State = (inputs[4] == 0);
bool button2State = (inputs[5] == 0);
bool button3State = (inputs[6] == 0);
bool button4State = (inputs[7] == 0 );
bool button5State = (inputs[8] == 0);
bool button6State = (inputs[9] == 0);
/*
// Print the values for debugging
Serial.print("Joystick 1 X: ");
Serial.println(joystick1X);
Serial.print("Joystick 1 Y: ");
Serial.println(joystick1Y);
Serial.print("Joystick 2 X: ");
Serial.println(joystick2X);
Serial.print("Joystick 2 Y: ");
Serial.println(joystick2Y);
Serial.print("Button 1 state: ");
Serial.println(button1State);
Serial.print("Button 2 state: ");
Serial.println(button2State);
Serial.print("Button 3 state: ");
Serial.println(button3State);
Serial.print("Button 4 state: ");
Serial.println(button4State);
Serial.print("Button 5 state: ");
Serial.println(button5State);
Serial.print("Button 6 state: ");
Serial.println(button6State);
*/
// Control the relays based on button states arm motor
if (button1State) {
digitalWrite(relay1, HIGH); // Turn on relay 1
digitalWrite(relay2, LOW); // Turn off relay 2
}
else if (button3State) {
digitalWrite(relay1, LOW); // Turn off relay 1
digitalWrite(relay2, HIGH); // Turn on relay 2
}
else {
digitalWrite(relay1, LOW); // Turn off relay 1
digitalWrite(relay2, LOW); // Turn off relay 2
}
// Set motor C and D speeds based on joystick1Y VERT MOTORS GOING UP AND DOWN
int motorSpeed = joystick1Y;
int VoltageMax = 12;
int motorMaxSpeed = 100; // set the maximum motor speed to 100
int motorPWM = map(constrain(motorSpeed, -motorMaxSpeed, motorMaxSpeed), -motorMaxSpeed, motorMaxSpeed,-VoltageMax15, VoltageMax15);
if (motorSpeed > 0) { // moving joystick up
digitalWrite(motorCPinIN1, HIGH); // set motor C direction forward
digitalWrite(motorCPinIN2, LOW);
analogWrite(motorCPinEN, motorPWM); // set motor C speed forward
digitalWrite(motorDPinIN3, HIGH); // set motor D direction reverse
digitalWrite(motorDPinIN4, LOW);
analogWrite(motorDPinEN, motorPWM); // set motor D speed reverse
}
else if(motorSpeed < 0){ // moving joystick down
digitalWrite(motorCPinIN1, LOW); // set motor C direction reverse
digitalWrite(motorCPinIN2, HIGH);
analogWrite(motorCPinEN, -motorPWM); // set motor C speed reverse
digitalWrite(motorDPinIN3, LOW); // set motor D direction forward
digitalWrite(motorDPinIN4, HIGH);
analogWrite(motorDPinEN, -motorPWM); // set motor D speed forward
}
else{ // joystick at rest
digitalWrite(motorCPinIN1, LOW); // stop motor C
digitalWrite(motorCPinIN2, LOW);
analogWrite(motorCPinEN, 0);
digitalWrite(motorDPinIN3, LOW); // stop motor D
digitalWrite(motorDPinIN4, LOW);
analogWrite(motorDPinEN, 0);
}
// HORZ MOTORS
int motorSpeed2 = joystick2Y; // Get joystick2Y value for Foward and reverse movement
int motorSpeed3 = joystick2X; // Get joystick2X value for Turning left and right
// Check if both joystick values are zero
if (motorSpeed2 == 0 && motorSpeed3 == 0) {
digitalWrite(motorAPinIN1, LOW); // stop motor A
digitalWrite(motorAPinIN2, LOW);
analogWrite(motorAPinEN, 0);
digitalWrite(motorBPinIN3, LOW); // stop motor B
digitalWrite(motorBPinIN4, LOW);
analogWrite(motorBPinEN, 0);
digitalWrite(motorAPinIN1LEFT, LOW); // stop motor A
digitalWrite(motorAPinIN2LEFT, LOW);
analogWrite(motorAPinENLEFT, 0);
digitalWrite(motorBPinIN3LEFT, LOW); // stop motor B
digitalWrite(motorBPinIN4LEFT, LOW);
analogWrite(motorBPinENLEFT, 0);
}
else if (motorSpeed2 != 0) { // If vertical joystick movement detected
// Set motor A and B speeds based on joystick2Y value
// HORZ MOTORS GOING FORWARD AND BACK
int VoltageMax2 = 6;
int motorMaxSpeed2 = 100; // set the maximum motor speed to 100
int motorPWM2 = map(constrain(motorSpeed2, -motorMaxSpeed2, motorMaxSpeed2), -motorMaxSpeed2, motorMaxSpeed2, -VoltageMax2*15, VoltageMax2*15);
if (motorSpeed2 > 0) { // moving joystick up
digitalWrite(motorAPinIN1, HIGH); // set motor A direction forward
digitalWrite(motorAPinIN2, LOW);
analogWrite(motorAPinEN, motorPWM2); // set motor A speed forward
digitalWrite(motorBPinIN3, LOW); // set motor B direction reverse
digitalWrite(motorBPinIN4, HIGH);
analogWrite(motorBPinEN, motorPWM2); // set motor B speed reverse
} else { // moving joystick down
digitalWrite(motorAPinIN1, LOW); // set motor A direction reverse
digitalWrite(motorAPinIN2, HIGH);
analogWrite(motorAPinEN, -motorPWM2); // set motor A speed reverse
digitalWrite(motorBPinIN3, HIGH); // set motor B direction forward
digitalWrite(motorBPinIN4, LOW);
analogWrite(motorBPinEN, -motorPWM2); // set motor B speed forward
}
} else if (motorSpeed3 != 0) { // If horizontal joystick movement detected
// Set motor A and B speeds based on joystick2X value
// HORZ MOTORS turn left or right
int VoltageMax3 = 6; // set the maximum voltage to 8V
int motorMaxSpeed3 = 100; // set the maximum motor speed to 100
int motorPWM3 = map(constrain(motorSpeed3, -motorMaxSpeed3, motorMaxSpeed3), -motorMaxSpeed3, motorMaxSpeed3,-VoltageMax3*15, VoltageMax3*15);
if (motorSpeed3 > 0) { // moving joystick left
digitalWrite(motorAPinIN1LEFT, HIGH); // set motor A direction forward
digitalWrite(motorAPinIN2LEFT, LOW);
analogWrite(motorAPinENLEFT, motorPWM3); // set motor A speed forward
digitalWrite(motorBPinIN3LEFT, HIGH); // set motor B direction reverse
digitalWrite(motorBPinIN4LEFT, LOW);
analogWrite(motorBPinENLEFT, motorPWM3); // set motor B speed reverse
} else { // moving joystick right
digitalWrite(motorAPinIN1LEFT, LOW); // set motor A direction reverse
digitalWrite(motorAPinIN2LEFT, HIGH);
analogWrite(motorAPinENLEFT, -motorPWM3); // set motor A speed reverse
digitalWrite(motorBPinIN3LEFT, LOW); // stop motor B
digitalWrite(motorBPinIN4LEFT, HIGH);
analogWrite(motorBPinENLEFT,-motorPWM3 );
}
}
// servo 1 open close gripper with left joystick moving in x direction
int Servo1Num = joystick1X ;
if (Servo1Num == 0) { // if joystick input is 0, servo remains in last position
// Do nothing
} else if (Servo1Num < 0) { // if joystick is pushed left (negative X-axis value)
if (servoPos1 < 160) { // rotate servo towards 180 degrees
servoPos1 += 5; // increment servo position by 1 degree
servo1.write(servoPos1); // write new servo position to servo
}
} else if (Servo1Num > 0) { // if joystick is pushed right (positive X-axis value)
if (servoPos1 > 100) { // rotate servo towards 0 degrees
servoPos1 -= 5; // decrement servo position by 1 degree
servo1.write(servoPos1); // write new servo position to servo
}
}
// servo 3 angle
// Check for button 5 and 6 state
if (button5State) {
if (servoPos3 < 100) { // rotate servo towards 180 degrees
servoPos3 += 1;
servo3.write(servoPos3);
}
} else if (button6State) {
if (servoPos3 > 20) { // rotate servo towards 0 degrees
servoPos3 -= 1;
servo3.write(servoPos3);
}
}
// servo 2 rotatio
// Check for button 2 and 4 state
if (button2State) {
if (servoPos2 < 160) { // rotate servo towards 180 degrees
servoPos2 += 1;
servo2.write(servoPos2);
}
} else if (button4State) {
if (servoPos2 > 95) { // rotate servo towards 0 degrees
servoPos2 -= 1;
servo2.write(servoPos2);
}
}
}
}
}
and here is the serial monitor output: <0,0,0,0,1,1,1,1,1,1>
<0,0,0,0,1,1,1,1,1,1>
<0,0,0,0,1,1,1,1,1,1>
<0,0,0,0,1,1,1,1,1,1>
<0,0,0,0,1,1,1,1,1,1>
<0,0,0,0,1,1,1,1,1,1>
<0,0,0,0,1,1,1,1,1,1>
<0,0,0,0,1,1,1,1,1,1>
<0,0,0,0,1,1,1,1,1,1>
<0,0,0,0,1,1,1,1,1,1>
<0,0,0,0,1,1,1,1,1,1>
<0,0,0,0,1,1,1,1,1,1>
<0,0,0,0,1,1,1,1,1,1>
<0,0,0,0,1,1,1,1,1,1>