Hello all,
I'm having some troubles with a Nano and I just cannot identify the issue.
The setup:
- Arduino Uno R4 Minima: Has a Radio module and a joystick connected.
- Arduino Nano: Has a Radio module and 2 servos connected.
The goal:
Move the two servos according to the movement of the joystick.
(expected) Operation:
The Uno takes the X and Y position of the joystick and makes it into a simple data "telegram" (string) the following way: data = position_X,position_Y.
The joystick positions are always between 0 and 90. So an average data string looks something akin to this: "43,12". Then the Uno sends it via radio to the Nano every 10 ms.
The Nano takes the data telegram cuts it in two using strtok() and sets the position of the two servos respectively.
The issue:
I fire up both Arduinos and all seem fine first. The data telegrams arrive as expected (it is set to first print out the telegrams before doing anything with them) and it actually works for some time. I can move the joystick around and the two servos follow (great success) however after a couple of seconds of movement it simply freezez. Even the serial output is stuck. I suspected at first some faulty data in the telegrams (like a sudden Ő instead of numbers) but according to the serial output the telegrams are all fine.
Also note that if i don't touch the joystick at all it goes on indefinitely without freezing. It also
seems like it freezes more frequently when I move the joystick more "wildly". I tried really hard to get a faulty telegram out of the Uno but i just couldn't so i highly suspicious the issue is with the Nano.
Any idea? Is it possible I'm running out of memory?
See the complete code for the Nano:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
//Radio pins CE, CSN
RF24 radio(9, 10);
//Create servo objects
Servo myServoV;
Servo myServoH;
const byte address[6] = "00001";
void setup() {
Serial.begin(9600);
//Setup servo pins
myServoV.attach(7);
myServoH.attach(8);
//setup radio and start listening
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
}
void loop() {
if (radio.available()) {
//Declare variables
int servo_V;
int servo_H;
char joystick[32] = "";
char * data;
//Get and print radio data
radio.read(&joystick, sizeof(joystick));
Serial.print("Data: ");
Serial.println(joystick);
//Get vertical servo pos
data = strtok (joystick,",");
servo_V = atoi(data);
//Get horizontal servo pos
data = strtok (NULL,",");
servo_H = atoi(data);
//Set servo positions
myServoV.write(servo_V);
myServoH.write(servo_H);
}
}