Joystick serial communication

Hi im new to this but i have a question ,i have a stepper motor thats controlled by a joystick ,which is working good, my question is ,is it possible to control the stepper motor through the serial of another uno board .What i need is a master / slave system where i have the motor attached to the slave uno board but the joystick connected to the master uno board .Can i send joystick data from the master board to control the motor on the slave board
Many Thanks

Welcome to the Arduino forum. Your proposal is certainly doable. Your first hurdle is to determine how moving the joystick ACTUALLY controls the stepper motor. Is there something between the joystick and the stepper? Can that something also accept a digital signal from an Arduino? Is there 5 volt power available to power your Arduino slave?


Hi thanks for the quick response ,i have attached a photo of what i have .I have the motor attached to a driver which is attached to the Uno board,the joystick connects to the uno board A0,A1 and the 0v and 5v.Yes i have power available to both boards.
Thankyou

Yes, you could send the joystick values to the slave Uno. The program you are using now has all the logic necessary to process those values, so almost all the code is already written and tested. Same with the new master Uno, All the joystick code is already written and tested.

Now you just have to get some version of software serial to work on both boards.

Is there a reason for that? In general it makes life complicated if you use two boards.

Hi yeah the reason is that the motor will be incased in a subsea pod so therefore i cannot operate the joystick underwater,so the idea was to have the motor driver and uno board subsea with a tether connected to the surface and have the joystick topside to control the motor underwater using serial comms.

Would anyone have any ideas on how i could do this? Basically i need the joystick to control the motor but it cannot be plugged into the uno board that the motor is in,i thought maybe having another uno board and having a master /slave system where the joystick is plugged into the master board and sent though serial comms to the slave board.Im very new to this so am still trying to learn.Thanks

Google for "arduino software serial" and try to make the examples work. Then add the code to your existing programs.

The serial input basics tutorial will show how to make packets of data to send, how to receive the packets and parse the data from them.

Thanks for the response ,i'll give it a read through.
Thankyou

How long is the tether? If it is very long you may need to use something like RS485 which uses line drivers and is good for pretty long distance.

Here is a simple example to send 1 joystick's values over a serial link. It is easily expanded to more joysticks or even other types of data. I modify example code from the tutorial.

sender:

// send data via software serial

#include <SoftwareSerial.h>

SoftwareSerial ss(2, 3);

const byte joy1X_pin = A0;
const byte joy1Y_pin = A1;

void setup()
{
   Serial.begin(9600);
   Serial.println("starting joystick sender");
   ss.begin(9600);
}

void loop()
{

   static unsigned long timer = 0;
   unsigned long interval = 500;
   if (millis() - timer >= interval)
   {
      timer = millis();

      int joy1X_value = analogRead(joy1X_pin);
      int joy1Y_value = analogRead(joy1Y_pin);
      Serial.println(joy1Y_value);
      char buffer[12];
      sprintf(buffer, "%d, %d", joy1X_value, joy1Y_value );

      ss.println(buffer); // send values
   }
}

receiver:

// recieve data via software serial
#include <SoftwareSerial.h>

SoftwareSerial ss(2, 3);

const byte numChars = 12;
char receivedChars[numChars];

int joy1X_value = 0;
int joy1Y_value = 0;

char *strings[2];
bool newData = false;

void setup()
{
   Serial.begin(9600);
   Serial.println("starting joystick receiver");
   ss.begin(9600);

}

void loop()
{
   recvWithEndMarker();
   parseData();
   displayData();
}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   while (ss.available() > 0 && newData == false)
   {
      rc = ss.read();
      //Serial.print(rc);
      if (rc == '\r')
      {
         return;
      }
      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}


void parseData()
{
   if (newData == true)
   {
      char *ptr = NULL;
      byte index = 0;
      ptr = strtok(receivedChars, ",");
      while (ptr != NULL)
      {
         strings[index] = ptr;
         index++;
         ptr = strtok(NULL, ",");
      }
      /*
         //Serial.println(index);
         // the separate pieces
         for (int n = 0; n < index; n++)
         {
         Serial.print("piece # ");
         Serial.print(n);
         Serial.print("  ");
         Serial.println(strings[n]);
         }
         newData = false;
      */
   }
}

void displayData()
{
   if (newData)
   {
      // convert strings to int data type (atoi()).

      joy1X_value = atoi(strings[0]);
      joy1Y_value = atoi(strings[1]);
     
      Serial.print("joystick 1 X axis value = ");
      Serial.println(joy1X_value);
      Serial.print("joystick 1 Y axis value = ");
      Serial.println(joy1Y_value);
      Serial.println();

      newData = false;
   }
}

output are X and Y values as numbers that can be used to control the motor.

joystick 1 X axis value = 995
joystick 1 Y axis value = 425

joystick 1 X axis value = 611
joystick 1 Y axis value = 0

joystick 1 X axis value = 548
joystick 1 Y axis value = 1023

joystick 1 X axis value = 537
joystick 1 Y axis value = 323

Hi thanks for the code i will give it a try in the morning,the tether itself is long around 1000m,but it goes through a fibre optic mux so in fact its only around 8 mtrs from motor to fibre optic ,i can use 232 or 485 through the fibres so wont matter on length but it has to be one of those.I will give this code a try.Many Thanks for the help

You could connect the 2 Unos using their hardware serial ports and get a much faster data rate if the hardware serial ports are not being used for anything else.

Also, using start and end markers (as explained in the tutorial) may make the communication more robust.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.