I cant connect my relay to work with vixen after a master and slave with RS485 connection

Hello there i am trying to make a DMX controller which has to be in rs485 connection because of the long distance that i need to send data … and then the arduino slave is connected to the relay for now i am suing 8 channels.

The relay has to anknowlage the vixen comand so for each part i comand in Vixen one relay must go ative and procede to execute the order.

thanks
Heres my code

//Master

#include <A2a.h> 
#include <SoftwareSerial.h>
#include "Arduino.h"


// DEFINIÇÕES DE PINOS
#define pinLED   2 //aqui coloca-se o primeiro pin do primeiro Relé 
#define n_arduinos 1
#define n_canais_por_arduino 8
#define N_canais_total (n_arduinos*n_canais_por_arduino)
#define end_inicial 0x08 // aqui coloca-se o endereço do primeiro slave , pode ser alterado, apenas tem de coincidir com o que está no programa do slave 1


//variaveis novas que poderam faltar 

#define RS485Transmit    HIGH
#define RS485Receive     LOW

// INSTANCIANDO OBJETOS
A2a arduinoSlave;



#define SSerialRX        10  //Serial Receive pin
#define SSerialTX        11  //Serial Transmit pin

//Pino de controle transmissao/recepcao
#define SSerialTxControl 3
//SoftwareSerial mySerial(11,12); // RX, TX

#define NOT_INVERTED 0
#define INVERTED 1
#define MODE NOT_INVERTED

// DECLARAÇÃO DE VARIÁVEIS
//declaracao dos canais rele
int channels[] = {n_canais_por_arduino,n_canais_por_arduino,n_canais_por_arduino,n_canais_por_arduino,n_canais_por_arduino ,n_canais_por_arduino,n_canais_por_arduino,n_canais_por_arduino};

byte endereco[n_arduinos];
int byteReceived;
int byteSend;

int a = 'a';

byte incomingByte[N_canais_total];
int i=0;
int counter = 0;

void setup() {
Serial.begin(9600);
  Serial3.begin(4800);
  Serial.println("ira comecar a enviar dados");
  Serial.println("A enviar dados");

  pinMode(pinLED, OUTPUT);   
  pinMode(SSerialTxControl, OUTPUT);    

  digitalWrite(SSerialTxControl, RS485Receive);  //   
  {
  for (int i=0;i<n_arduinos;i++){
    
    endereco[i]=(end_inicial)+i;
 
    }
  }
  arduinoSlave.begin();{
  for (byte nS = 0 ; nS < n_arduinos ; nS++) {
    
    for(byte i=0;i<n_canais_por_arduino;i++){
    arduinoSlave.pinWireMode(endereco[nS], pinLED+i, OUTPUT);
    delay(100);
    arduinoSlave.digitalWireWrite(endereco[nS], pinLED+i, LOW);
    }
  }
  }
   if (MODE == NOT_INVERTED){
  for (byte nS = 0 ; nS < n_arduinos ; nS++) {  
    for(byte i=0;i<n_canais_por_arduino;i++){ 
    arduinoSlave.digitalWireWrite(endereco[nS], pinLED+i, HIGH);
    delay(500);
    arduinoSlave.digitalWireWrite(endereco[nS], pinLED+i, LOW);
    }
  }
}
}

void loop(){
  a=a+1;
  {
    Serial.println("Novo dado");
    digitalWrite(pinLED , HIGH);
    Serial.println("Valor lido");
    Serial.println(a);   
    delay(100);
    digitalWrite(SSerialTxControl, RS485Transmit);
    Serial3.write(a);          // Send byte to Remote Arduino
    Serial3.flush();
    Serial.println("Valor enviado");
    Serial.println(a);
    digitalWrite(pinLED , LOW);  // Show activity    

    digitalWrite(SSerialTxControl, RS485Receive);
    delay(100);
    if(Serial3.available()){
      byteReceived = Serial3.read();
      Serial.println("resposta enviada");
      Serial.println(byteReceived);
      while(Serial3.available()){
        Serial3.read();
      }
  {
    for (int i=0; i<8;i++)
    {
      incomingByte[i] = Serial.read();

      // incomingByte[i] = 255 - Serial.read(); 
    }                                     
    int arduino=n_arduinos;
    int N_canais_arduino=n_canais_por_arduino;
    int n_canais_total=N_canais_total;
    
  for(;;){
        
        if(Serial.available() >= n_canais_total){

                      
                    for(int i=0;i<n_canais_total;i++){

                          incomingByte[i]=Serial.read();
                      
                    }
                     if (MODE == NOT_INVERTED) {
                    for(int nS=0;nS<arduino;nS++){
                          for(int i=0;i<N_canais_arduino;i++){
                               int j=nS*N_canais_arduino,p=i+j;
                                if(incomingByte[p]==0){arduinoSlave.digitalWireWrite(endereco[nS], pinLED+i, LOW);
                                }else{arduinoSlave.digitalWireWrite(endereco[nS], pinLED+i, HIGH);
                                
                                
                                 }
                           }
                    
                     }
                    
              }
                
  }
  }
  }
  }
  }
  }

Slave code

#include <A2a.h>
#include <SoftwareSerial.h>
#include "Arduino.h"

#define pinLED 2//primeiro pin do rele

//---------------------- endereço do slave

#define endereco 0x08 // MODIFICAR O ENDEREÇO PARA CADA SLAVE

A2a arduinoMaster;

#define SSerialRX        10 //Serial Receive pin
#define SSerialTX        11  //Serial Transmit pin


//Pino de controle transmissao/recepcao
#define SSerialTxControl 3
#define RS485Transmit    LOW
#define RS485Receive     HIGH

SoftwareSerial RS485Serial(SSerialRX, SSerialTX);

int byteReceived;
int byteSend;

void setup() {
  Serial.begin(9600);
  Serial.println("recebido..."); 
  pinMode(pinLED, OUTPUT);   
  pinMode(SSerialTxControl, OUTPUT);  
  digitalWrite(SSerialTxControl, RS485Receive);  // transmitor
  RS485Serial.begin(9600);   
  arduinoMaster.begin(endereco);
}

void loop() { 
 if(RS485Serial.available()) 
  {
    Serial.println("Resposta chegada");
    byteSend = RS485Serial.read();
    Serial.println(byteSend);
    delay(100);
    digitalWrite(SSerialTxControl, RS485Transmit);   
    RS485Serial.write(byteSend);
    RS485Serial.flush();  
    delay(100);
    digitalWrite(SSerialTxControl, RS485Receive);
    Serial.println("resposta chegada");
    Serial.println(byteSend);
    
  }

}
void receberDados() {
  arduinoMaster.receiveData(); 
}

void enviarDados() {
  arduinoMaster.sendData(); 
}

Without your code, wiring and a detailed description of function, NO!

2 Likes

I was with a rush so i made mistakes, i uploaded my code and updated the description of the problem! soon i will upload the schematic

and some variavels are in my native language so if have any doubt understading then i can translate to you!

Could you post your code using code tags by selecting the code in the IDE, right-click and select “Copy for forum” and paste it here.
hitting the </> code-tags button here on the forum is also starting to work properly,
but omitting them here makes the forum respond to the indentation in your sketch.

As for that, DMX is always in RS485, If you want to make a DMX controller, you should go for the full implementation, that means 250kbps transmission with a 80us break. That should give you at least the specified 100 meters (actually you can get up to about 300 meters if you use a terminator, 100 meters is what the designers felt safe to garantee)

DMX is a one way protocol, but yes you should be able to receive and do whatever you want with the data.
Probably the best to use one of the DMX libraries, personally i use

https://sourceforge.net/p/dmxlibraryforar/wiki/Home/

which works great for sending on an AVR.

i updated the code to forum now its done!!

in the RS485 i will use rj45 cable for both A and B pin for getting that range! its important to tell that also

And also i put some photos i know aint from fritzing but i cant make right now. so i cant give u some photos of the projct

Ok i see from the pictures you are using 2x Arduino Mega,
My first question is :

SoftwareSerial mySerial(11,12); // RX, TX


Why are you using swSerial ? Mega has 4 hardware UARTs.
Secondly (which should have been firstly) You are not trying to communicate using DMX, you are attempting i2C ? But to extend the possible distance you want to use rs485. But i2C has both data & clock pin… You can use a UTP cable for this just fine (as long as you use the twisted pairs of it !!) , but you will need 2 x 2 x Transceivers.
So all my support can be is about DMX (which is a protocol with a pre-set clock speed) for which you will need only 2 transceivers, mind you, not the ones that you are using now, but max485 8-pin dip IC’s are the easiest.
DMX is a specified industry standard, and i know that with the 2 units that you have you can make USB → DMX dongles I made a few myself, to use with freestyler, but i had some reliability issues with them (may have been freestyler, but they would stop working after a while until i reset them) Didn’t really matter for me, i was only using them for testing DMX reception on an Arduino.
Once i created a TFT-DMX mixer (mini size) i used that. Most VJ’s have their own program, and most clubs have their own mixers and programs and other infrastructure. I build devices that receive DMX. LED-strip controllers , dim packs, Slide projectors etc. If you create a device that receives DMX you can use it at any venue and can be controlled by any VJ (so you can party all night)
If you just want to control a bunch of relays remotely, you might be better of creating something wireless, but essentially if you want to use the protocol you have now, you don’t have the correct hardware at the moment.

Can ypu explain me why the 2x2 transceivers are needed and how to connect them?
I made an new upload of the code so you can check it
We cant use wireless for now because this will be in zones without internet connection
and whats the hardware i need?

and the first question u asked me i know it was a mistake, i changed that

And i also have a bunch of P82B715´s here and my RS485 have max485 intregated.
The main point is to control and send data to the relay for more than 150-300 meters

You could try and use them. How much distance do you intend to cover ?

You can do wireless without internet, again how much distance do you intend to cover ?

Well if you plan to use I2C which is a signal that uses a clock pin, you would need to transmit both of these signals via a twisted pair and receive them at the other end, for that you need a transceiver at each end for each signal. This is all going from the point of view that it is a single directional signal (I am not familiar with I2C protocol)

Yeah i know, but can you set those units to receive (rather than send which i am pretty sure is their default) by changing the polarity of the IC pins 2 & 3 from HIGH to LOW ?

I am someone who believes that you actually need to understand what you are doing so let me explain first about ‘twisted pair’ signals.
Twisted pair was invented by Graham Bell (you know) to counter-act the effects of capacitance on data signals. Every (metal) cable has capacitance, which means that every cable works as (small) capacitor. This is because electrons gather on the surface of the metal. In short, the more surface of the metal, the more capacitance. Multi-strand cable has more capacitance than solid cable, the longer and thicker the cable, the more capacitance. (side-branches count, and it all adds up, just like capacitors do)
Capacitance, or putting a capacitor on a data cable influences the signal in such a way that a square data signal starts curving at the receiving end when the transmitting end change polarity. By transmitting the signal over 2 cables, where one cable has the polarity inverted to the other, and twisting these cables, the magnetic effect of the spiral counteracts the capacitance and if you measure the difference between these 2 signals at the other end you can therefore communicate over bigger distance / higher speed than with out twisted pair technology. This is the application used for data transfer, but twisted pairs are also used in analog AC signals like microphones (where a relatively low voltage signal is transmitted) to counteract distortion.

MAX485
If you do not intend to do a full DMX implementation, and you only want to control 8 relays remotely, you might consider using DALI instead. (or a modified version of it) It is used to control lighting in buildings, but works at a much lower BAUD-rate (1200), is self-clocking (Manchester protocol) and requires very little hardware (1 NPN transistor, an Opto-coupler and a few resistors is all you need) and you can run the cables through the same tube as a power-cable (legally)

Please explain again what you want to achieve, what is the type of location, how far does the controller need to be from the relays, will you be using it elsewhere, and what is the type of ‘master-controller’ if any ? (Vixen ?)

I intend to cover at least 150M-300M with that

I didnt know that i can use that is it hard to put that to work the wireless

so basically change to the sender HIGH to low and the receber LOW to High?

Well my achivement is to finish my controller, which connects with the ideology of Master and slave, so one arduino comands and the slaves(the others will execute the order), each slave will be near the relay and them will trigger in the relay activitie, Vixen is a software to create diagrams and config lighting and the relays will get ative when i told them an active for exemple i have 8 relays and i online want the first and the second on to be active and then only the first and the second will respond and be ON
it has to be 150-300 meters i will use then to control light projects

Nope. on the MAX485 chip pins 2 & 3 deal with setting the mode on the chip
Pin 2 is !RE (not RE) so when pin 2 is ‘LOW’ the chip is Receive-Enabled. (when it’s HIGH it is disabled)
pin 3 is DE, so when pin 3 is ‘HIGH’ the chip is Drive-Enabled.
Normally speaking you will want the chip in one mode or the other, and that means that both pins should be in the same state at the same time. When you set them ‘HIGH’ you set the chip in Drive-Enabled, when you set them ‘LOW’ you set it in Receive-Enabled. Then there is still the issue of the different data pins for the different modes. Driving the signal is done on pin 4 (Drive-Input) . Receiving is done on pin 1 (Receive-Output) . Hence the suggestion to use the bare Dip 8 IC, where it is a lot easier to modify the functionality of the transceiver than on the module.

That is a fair way for a wireless system, and even for a wired system 300m would be close to a reliable maximum using twisted pair at 250kbps (which is what DMX uses) but might just work if you use a good cat5 UTP cable and make sure you connect D+ & D+ to half of the same pair. (on an rj45 plugged ethernet cable that would be pins 1 & 2 , 4 & 5, 7 & 8 and 3 & 6. ) and make sure to use 1 of the wires / pins for common GND !
If you can make a hub half way where you receive and drive a pair with 2 extra transceivers you will almost for sure be fine, but that does mean you would need to bring power there.

I know Vixen, but have hardly used it. (my VJ bought some software that had a DMX-dongle included.)

So what i should change in the code and in the connections i made? (i am responding generaly)