Go Down

Topic: Uno to Mega via UARTS over two RS MAX485 boards (Read 31 times) previous topic - next topic

MegaNodens

Greetings all. Before I start, I swear that I've tried looking high and low for the answer to this, but what I've found has either been too specific to another problem to be applied to my own or my understanding is too rudimentary to understand it.

So, had I known that I would need these RS485 boards I would have never attempted this project, but without those boards I though it was just far enough out of my comfort zone... and I was right, until I added a 100 ft Cat5 cable between the boards it worked like a charm. I've since rush ordered two of the RS485 units and spent the day installing them on either side, but no test code has worked and I'm afraid I'm either not understanding the pin assignments or if I've wired it some way incorrectly.

To start the whole point of this is one-way communication from master to slave. The following is whats on each board, and what pins are in use, as well as any other information I can think to add in.

Master Board: An arduino UNO R3 with PS2 shield utilizing D11 as TX & D10 as RX at a 9.6k br

Slave Board: An arduino Mega with three motor shields, each with their own RX/DX pins (D3 & D2, D5 & D4, D6 & D7 respectively)

Before I added the wire, the working configuration for UARTS was using RX0/DX0 on the Uno to RX1/DX1 on the Mega (Pins 18 and 19). This was more or less the vanilla setting for the Easy Transfer library I have been utilizing

My understanding of the RS485 boards is largely from this thread, and I have each end wired accordingly (aside from the resistor. I only have 1k resistors at the moment)
http://forum.arduino.cc/index.php/topic,17118.0.html

The RS485 boards are the ones listed here: http://amzn.com/B00FHG1QC8

Below is the code known to work prior to the installation of the 100ft cable. I haven't gotten to the point of altering it, and indeed I'm unsure of the scale of what alterations are needed.

Master Code:
Code: [Select]

#include <SoftwareSerial.h>
#include <Cytron_PS2Shield.h>
#include <EasyTransfer.h>

//create object
EasyTransfer ET;

struct SEND_DATA_STRUCTURE{
  //put your variable definitions here for the data you want to send
  //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
  int blinks;
  int pause;
  int Left_Up_Value;
  int Left_Down_Value;
  int Right_Up_Value;
  int Right_Down_Value;
};

//give a name to the group of data
SEND_DATA_STRUCTURE mydata;

Cytron_PS2Shield ps2(10, 11); // SoftwareSerial: Rx and Tx pin

void setup()
{
  ps2.begin(9600); // This baudrate must same with the jumper setting at PS2 shield
  Serial.begin(9600); // Set monitor baudrate to 9600
  ET.begin(details(mydata), &Serial);
 
  randomSeed(analogRead(0));
  pinMode(13, OUTPUT);

//  pinMode(pinPwm, OUTPUT);
//  pinMode(pinDir, OUTPUT);
}

void loop()
{
  mydata.Left_Up_Value = ps2.readButton(PS2_JOYSTICK_LEFT_UP);         //Gets Left Joystick Forward Motion Value
  mydata.Left_Down_Value = ps2.readButton(PS2_JOYSTICK_LEFT_DOWN);     //Gets Left Joystick Reverse Motion Value
  mydata.Right_Up_Value = ps2.readButton(PS2_JOYSTICK_RIGHT_UP);      //Gets Right Joystick Forward Motion Value
  mydata.Right_Down_Value = ps2.readButton(PS2_JOYSTICK_RIGHT_DOWN);   //Gets Right Joystick Reverse Motion Value

  for(int i=0; i<5; i++){
  ET.sendData();
  }
  delay(100);
}

Slave Code:
Code: [Select]

#include <Cytron_PS2Shield.h>
#include <SoftwareSerial.h>
#include <EasyTransfer.h>

//create object
EasyTransfer ET;

struct RECEIVE_DATA_STRUCTURE{
  //put your variable definitions here for the data you want to receive
  //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
  int blinks;
  int pause;
  int Left_Up_Value;
  int Left_Down_Value;
  int Right_Up_Value;
  int Right_Down_Value; 
};

//give a name to the group of data
RECEIVE_DATA_STRUCTURE mydata;

//Left Thruster  (PWM = D3 | DIR = D2)  //Defines and assigns pins to each truster
const int leftThrusterPwm = 3;
const int leftThrusterDir = 2;
//Right Thruster (PWM = D5 | DIR = D4) //Defines and assigns pins to each truster
const int rightThrusterPwm = 5;
const int rightThrusterDir = 4;
//Vert. Thruster (PWM = D9 | DIR = D8) //Defines and assigns pins to each truster
const int vertThrusterPwm = 9;
const int vertThrusterDir = 8;

// Speed of the motor.
  int iStop = 0;

// Acceleration of the motor.
   int iAcc = 1;

void setup()
{
  Serial.begin(9600); // Set monitor baudrate to 9600
 
  ET.begin(details(mydata), &Serial);
  pinMode(13, OUTPUT);
 
  pinMode(leftThrusterPwm, OUTPUT);
  pinMode(leftThrusterDir, OUTPUT);
 
  pinMode(rightThrusterPwm, OUTPUT);
  pinMode(rightThrusterDir, OUTPUT);
}

void loop()
{
 for(int i=0; i<5; i++){

  if(ET.receiveData()){
   
  if (mydata.Left_Up_Value > 10) {
    analogWrite(leftThrusterPwm, mydata.Left_Up_Value);
    digitalWrite(leftThrusterDir, LOW);
    Serial.println(mydata.Left_Up_Value);

  }
  else if (mydata.Left_Down_Value > 10) {
    analogWrite(leftThrusterPwm, mydata.Left_Down_Value);
    digitalWrite(leftThrusterDir, HIGH);
  }
  else{
    analogWrite(leftThrusterPwm, iStop);
    digitalWrite(leftThrusterDir, HIGH);
  }

  //responds
  if (mydata.Right_Up_Value > 10) {
    analogWrite(rightThrusterPwm, mydata.Right_Up_Value);
    digitalWrite(rightThrusterDir, LOW);
    Serial.println(mydata.Right_Up_Value);
  }
  else if (mydata.Right_Down_Value > 10) {
    analogWrite(rightThrusterPwm, mydata.Right_Down_Value);
    digitalWrite(rightThrusterDir, HIGH);
  }
  else{
    analogWrite(rightThrusterPwm, iStop);
    digitalWrite(rightThrusterDir, HIGH);
  }
 }
 delay(25);
 }
}


As far as being specific to what I am stumped by, I figure the issue could be any combination of the following:

1. I do not know where to connect the wire from combined DE/RE
2. I am unsure whether I am correct that Ground and/or 5v do not need to be shared between both RS485 boards.
3. Are these boards essentially plug-and-play, in the sense that as long as I account for that third pin on each side I do not need to add/alter my code? Do I need to change up my RX/DX pin assignments on the slave board?

Well, that's all I've got. For those curious about the nature of this project, it's another ROV that I've been working on since January, its made of a mixture of 3D printed parts and PVC piping. This is pretty much the last hurdle. Thanks in advance to anyone who takes the time to reply. This is my first post but I hope that I will be able to return the favor to others in the future!

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy