Hello all,
(Edit) please move this post to "Networking, Protocols, and Devices"
First I want to say hi to the Arduino community. I have been playing around with Basic Stamp II of and on for a few years now. With some of the limitations I have been forced to move over to Ardunio, I have not looked back since.
As a hobby i am building a underwater ROV. I plan to use 2 Arduinos Megas, one on the surface sending commands below for the 6 thrusters the ROV has. In return the MEga below surface will be sending sensor readings all using Nick Gammons RS485 Library.
Now to the question: I have been able to get his example code working using software, however, using Ardunio Megas I would like to use hardware.
Here is my Master Code
#include <RS485_protocol.h>
//#include <SoftwareSerial.h>
const byte ENABLE_PIN = 24;
const byte LED_PIN = 13;
int level = 0;
int pot = A0;
byte loB, hiB;
//SoftwareSerial rs485 (10, 11); // receive pin, transmit pin
// callback routines
void fWrite (const byte what)
{
Serial1.write (what);
}
int fAvailable ()
{
return Serial1.available ();
}
int fRead ()
{
return Serial1.read ();
}
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
//rs485.begin (57600);
pinMode (ENABLE_PIN, OUTPUT); // driver output enable
pinMode (LED_PIN, OUTPUT); // built-in LED
} // end of setup
byte old_level = 0;
void loop()
{
// read potentiometer
level = analogRead (pot);
Serial.println(level);
// no change? forget it
if (level == old_level)
return;
hiB = highByte(level); // splits the highbyte off the int variable "level"
loB = lowByte(level); // splits the loghbyte off the int variable "level"
// assemble message
byte msg [] = {
1, // device 1
2, // turn light on
hiB, // first half of level
loB, // second have of level
};
// send to slave
digitalWrite (ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, msg, sizeof msg);
delayMicroseconds (660);
digitalWrite (ENABLE_PIN, LOW); // disable sending
// receive response
byte buf [10];
byte received = recvMsg (fAvailable, fRead, buf, sizeof buf);
digitalWrite (LED_PIN, received == 0); // turn on LED if error
// only send once per successful change
if (received)
old_level = level;
} // end of loop
Here is my Slave Code.
#include <RS485_protocol.h>
//#include <SoftwareSerial.h>
//SoftwareSerial rs485 (10, 11); // receive pin, transmit pin
int level;
byte loB, hiB;
const byte ENABLE_PIN = 24;
void fWrite (const byte what)
{
Serial1.write (what);
}
int fAvailable ()
{
return Serial1.available ();
}
int fRead ()
{
return Serial1.read ();
}
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
//rs485.begin (57600);
pinMode (ENABLE_PIN, OUTPUT); // driver output enable
digitalWrite (ENABLE_PIN, LOW); // Sets pint to incoming
}
void loop()
{
byte buf [20];
byte received = recvMsg (fAvailable, fRead, buf, sizeof (buf) - 1);
if (received)
{
if (buf [0] != 1)
return; // not my device
if (buf [1] != 2)
return; // unknown command
byte msg [] = {
0, // device 0 (master)
3, // turn light on command received
};
delay (1); // give the master a moment to prepare to receive
digitalWrite (ENABLE_PIN, HIGH); // enable sending
sendMsg (fWrite, msg, sizeof msg);
digitalWrite (ENABLE_PIN, LOW); // disable sending
// Serial.println (buf [2]); // this prints is hib from master
// Serial.println (buf [3]); // this prints is loB from master
int level = word(buf [2], buf[3]); // convert the bytes from master hiB loB back into a word not buf with brackets is the variable
Serial.println(level,DEC);
} // end if something received
} // end of loop
I have confirmed my wiring is correct using the Serial1 pins on the Mega because I have been able to SerialWrite(); from the master and SerialRead(); from the slave not using any library.
Could anyone point me in the right direction?
Andrew