So I'm doing a project right now of controlling a Tank Vehicle that has 4 9V DC Motors that are controlled by a Adafruit MotorShield V2.3 on top of a Arduino Uno WiFi Rev2.
And I control it with an Xbox Wireless Controller, that is connected through Bluetooth to a Raspberry Pi, which then sends the controller-signals as UDP packets through WiFi to the Arduino. (Don't ask why so complicated, its required by how the project is supposed to go further, but the Raspberry Pi kinda has to be involved)
And generelly it's working fine, I have a simple Arduino code that interprets the UDP packets and controls the motors just fine, but every now and then the Arduino program seems to just stop working (based on the stopped/interrupted Serial monitor output) and if the motor is unfortunately still turned on it just drives without stopping and could run into walls, which of course it shouldn't. Now I wanna know if maybe something in my code is prone to crash randomly, if it has something to do with the UDP packets, or my friend proposed it could have something to do with the quartz crystal clock in the Arduino (the Arduino is probably around or more than 4 years old), which then might be a hardware problem, so I just wanna know if theres anything I can do in the software part, before switchting out any parts.
(And I've already re-created the error with Delays behind the Serial Output and without any Serial Monitor Output in the code at all)
Thanks
Code:
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#include <SPI.h>
#include <WiFiNINA.h>
#include <WiFiUdp.h>
char ssid[] = "Tank-WiFi";
char pass[] = "xxxxxxx";
int status = WL_IDLE_STATUS;
unsigned int localPort = 2390;
IPAddress ip(xxx, xxx, x, xxx);
char packet[255];
char ReplyBuffer[] = "acknowledged";
int Speed = 110;
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *RH = AFMS.getMotor(1);
Adafruit_DCMotor *LH = AFMS.getMotor(2);
Adafruit_DCMotor *LV = AFMS.getMotor(3);
Adafruit_DCMotor *RV = AFMS.getMotor(4);
WiFiUDP Udp;
void setup() {
Serial.begin(250000);
while (!Serial) {;}
AFMS.begin();
RH->setSpeed(Speed);
RV->setSpeed(Speed);
LH->setSpeed(Speed);
LV->setSpeed(Speed);
checkWiFi();
WiFi.config(ip); //Wichtig
connectToWiFi();
Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
Serial.println("\nStarting connection to server...");
Udp.begin(localPort);
}
void loop() {
int packetSize = Udp.parsePacket();
if (packetSize) {
Udp.read(packet, UDP_TX_PACKET_MAX_SIZE);
int UDP = atoi(packet);
switch (UDP) {
case 0:
Serial.println("STOP");
RH->run(RELEASE); RV->run(RELEASE);
LH->run(RELEASE); LV->run(RELEASE);
break;
case 1:
Serial.println("FORWARD");
RH->run(FORWARD); RV->run(FORWARD);
LH->run(FORWARD); LV->run(FORWARD);
break;
case 2:
Serial.println("BACKWARD");
RH->run(BACKWARD);RV->run(BACKWARD);
LH->run(BACKWARD);LV->run(BACKWARD);
break;
case 3:
Serial.println("RIGHT");
RH->run(BACKWARD);RV->run(BACKWARD);
LH->run(FORWARD); LV->run(FORWARD);
break;
case 4:
Serial.println("LEFT");
RH->run(FORWARD); RV->run(FORWARD);
LH->run(BACKWARD);LV->run(BACKWARD);
break;
case 5:
Serial.println("SPEED CHANGE TO 80");
Speed = 80;
RH->setSpeed(80); RV->setSpeed(80);
LH->setSpeed(80); LV->setSpeed(80);
break;
case 6:
Serial.println("SPEED CHANGE TO 90");
Speed = 90;
RH->setSpeed(90); RV->setSpeed(90);
LH->setSpeed(90); LV->setSpeed(90);
break;
case 7:
Serial.println("SPEED CHANGE TO 100");
Speed = 100;
RH->setSpeed(100); RV->setSpeed(100);
LH->setSpeed(100); LV->setSpeed(100);
break;
case 8:
Serial.println("SPEED CHANGE TO 110");
Speed = 110;
RH->setSpeed(110); RV->setSpeed(110);
LH->setSpeed(110); LV->setSpeed(110);
break;
}
}
}
//Unimportant
void checkWiFi() {
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
}
void connectToWiFi() {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
while (status != WL_CONNECTED) {
status = WiFi.begin(ssid, pass);
delay(5000);
Serial.println("Trying again...");
}
}
void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
printMacAddress(mac);
}
void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
printMacAddress(bssid);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.println(rssi);
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption, HEX);
Serial.println();
}
void printMacAddress(byte mac[]) {
for (int i = 5; i >= 0; i--) {
if (mac[i] < 16) {
Serial.print("0");
}
Serial.print(mac[i], HEX);
if (i > 0) {
Serial.print(":");
}
}
Serial.println();
}