Hello guys,
My project is fairly simple. I have one arduino uno (collects data) and one arduino nano (sends data)
I am trying to make a serial rs485 connection between arduinos, however because of my lack of knowledge in programming i have difficulty understanding the code provided by nick gammon at
So the purpose is that the arduino nano has an ultrasonic sensor on it, collects its data and then trasmit those data on the arduino uno then the uno stores this value on a opc item , then the opc server(PC) collects that item via usb connection.
In order for the opc server to work i need to use softwareserial, my problem is with the example code provided by Nick Gammon, i have trouble understanding the code although the documentation is very well written.
So i altered some of the code in order to fit to my project needs and i need some guidance if possible, is this the right way to do it?
My code that is supposed to run on the nano
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance
#include <SoftwareSerial.h>
const byte ENABLE_PIN = 4;
const byte LED_PIN = 13;
SoftwareSerial rs485 (2, 3); // receive pin, transmit pin
// callback routines
#include <RS485_non_blocking.h>
size_t fWrite (const byte what)
{
return rs485.write (what);
}
RS485 myChannel (NULL, NULL, fWrite, 0);
void setup ()
{
Serial.begin (115200);
myChannel.begin ();
} // end of setup
//const byte msg [] = distance;
void loop ()
{
int distance= sonar.ping_cm();
byte msg [] = {distance};
myChannel.sendMsg (msg, sizeof (msg));
delay (1000);
} // end of loop
This code compiles with a warning
warning: narrowing conversion of 'distance' from 'int' to 'byte {aka unsigned char}' inside { } [-Wnarrowing]
byte msg [] = {distance};
Is this the right way to do it?
Help would be grately appreciated.
Thanks.