0

I need to read acceleration data with ADXL335 using ARDUINO MICRO (or NANO) and send this data with RS485 module to another RS485 module connected to an ARDUINO UNO (300m between RS485 modules). Treated to use the codes below but slave does not receives any data. Data from AXDL335 are in the form "xxx yyy zzz" Do I need to write in a "string format" before send with RS485 (master)?? Many thanks in advance!!

master code

#include <SoftwareSerial.h>
#define SSerialRX   10
#define SSerialTX   11
#define SSerialTxControl  3
#define RS485Transmit  HIGH
#define RS485Receive  LOW
int x;                                                // channel X  ADXL335
int y;                                                // channel Y  ADXL335
int z;                                                // channel Z  ADXL335
SoftwareSerial RS485Serial(SSerialRX, SSerialTX);
void setup() {
  Serial.begin(9600);                                 // configure baud rate monitor

  pinMode(SSerialTxControl, OUTPUT);
  digitalWrite(SSerialTxControl, RS485Receive);
  RS485Serial.begin(4800);                            // starts serial(485) and configure baud rate
}

void loop() {

 x=analogRead(0);                                     //read X in analog(0)
 y=analogRead(1);                                     //read Y in analog(1)
 z=analogRead(2);                                     //read Z in analog(2)
                                                      // print read values in monitor
 Serial.print(x);Serial.print(" ");Serial.print(y);Serial.print(" ");Serial.print(z);Serial.println(" ");  
 RS485Serial.print(x); 
 delay (250);

 RS485Serial.flush();
 delay (750);
 }

SLAVE CODE

#include <SoftwareSerial.h>
#define SSerialRX   10
#define SSerialTX   11
#define SSerialTxControl  3
#define RS485Transmit  HIGH
#define RS485Receive  LOW
char x;
SoftwareSerial RS485Serial(SSerialRX, SSerialTX);

void setup() {
  Serial.begin(9600);        // configure baud rate monitor
  pinMode(13, OUTPUT);
  pinMode(SSerialTxControl, OUTPUT);
  digitalWrite(SSerialTxControl, RS485Receive);
  RS485Serial.begin(4800);  // start serial(485) and configure baud rate
}

void loop() 
{  
 if (RS485Serial.available()>0) {
  x = RS485Serial.read();
  if (x!="n")

  Serial.print(x);Serial.println(" ");
  delay(1000);

 }
}
  • print will print the number as text. use write as counterpart of read or use parseInt to read text printed with print(number) – Juraj Dec 28 '18 at 11:26
  • x is char. you can't compare it with a string with !=. you can compare it with a char x != 'n'. when would x be 'n'? – Juraj Dec 28 '18 at 11:34
  • For 300 meters you need to do it right and avoid things that can cause troubles. I suggest to stay away from the SoftwareSerial. Use a (pro) micro with its hardware serial port. Can you tell what kind of cable it is? Can you give a link to that cable? Some cables are cheap and have almost no copper in it. What kind of rs485 modules do you use? Are you testing this on your table or already with the 300 meters in between? How often do you need the data? Perhaps a slow wireless lora module might work. Add the extra information to your question. Your master has the control pin in receive mode. – Jot Dec 28 '18 at 12:43
  • dear Jot I apologize since I am not native English speaker. At the beginning and if it works on the bench I think of using the RS485 modules of the LC electronics where the chip is already mounted with the DI RE RE pins already connected. If it works at my desk I will push the RS485 away until I check how far the signal emitted every 1 Hz is transmitted and properly received. There I will see which type of cable (4 ways I imagine it should be) can carry the signal and also feed the arduino and the accelerometers. Thanks for your help !! – FERNANDO MÁNTARAS Jan 8 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.