MEGA2560 - MEGA2560 data transfer

Hi All,
I am new to Audrino and programming, but would like to start my first little project :slight_smile:

I have been given 2 Audrino MEGA2560 boards and would like to transfer some data between the 2 using the 1-wire protocol. I have tried to edit the example code used for temperature sensors, but have had no joy.

Is there someone who has done this before, or has example code of how to do it?

I'm keen to learn, but just need some beginners help :slight_smile:

Many Thanks

It has been asked before, with middling results.

The main question is - why?

Just something I'd like to try. I like the idea of the 1 wire protocol and would like to give it a go.
Any help would be great...

The one-wire protocol is probably the hardest to get started with. May I suggest async serial?

That uses one wire per direction (signal) plus ground. For a one-wire system you really need two wires anyway (signal + ground).

To talk in both directions (send and receive) you need three wires: transmit, receive, and ground.

I have tried to edit the example code used for temperature sensors, but have had no joy.

Those examples receive only, because the temperature sensor transmits only.

Hi guys, I've had a play over the weekend and have come up with the following code for the master and slave respectively.

Slave code
#include <OneWire.h>

void setup()
{
OneWire.begin(2); // join one wire bus with address #2
OneWire.onReceive(receiveEvent); // register event
OneWire.onRequest(sendInfo); //send infor to master on request
Serial.begin(9600); // start serial for output
}

void loop()
{

delay = 100
}

// This function executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
testByte1 = OneWire.read() //stores the data to the next available memory
testByte2 = OneWire.read() //stores the data to the next available memory
testByte3 = OneWire.read() //stores the data to the next available memory
testByte4 = OneWire.read() //stores the data to the next available memory
testByte5 = OneWire.read() //stores the data to the next available memory
testByte6 = OneWire.read() //stores the data to the next available memory
}

void sendInfo(){
OneWire.write(outgoingByte,2);
}

Master Code
#include <OneWire.h>

// Data wire is plugged into pin 1 on the Arduino
#define ONE_WIRE_BUS 1

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

int numberOfDevices;

void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("1-Wire practice code");

// Start up the library
// Devices.begin();

delay(5000); //serial port can lock up otherwise
numberOfDevices = discoverOneWireDevices();
Serial.println();
}

//void loop(void)
//{
// printDataToSerial();
// delay(10000); //wait 10 sec
//}

void loop()
{
//OneWire.begin();// join the 1 wire bus
OneWire.beginTransmission(2); // transmit to device #2
OneWire.write(testByte1); // sends byte1
OneWire.write(testByte2); // sends byte2
OneWire.write(testByte3); // sends byte3
OneWire.write(testByte4); // sends byte4
OneWire.write(testByte5); // sends byte5
OneWire.write(testByte6); // sends byte6
OneWire.endTransmission(); // stop transmitting

x++;
delay(500); // delay added in

//Recevie data from the slave controller
OneWire.requestFrom(2,2)// requests 2 bytes of data from de vice on address 2
incomingByte[0] = OneWire.read() //stores the data to the nexat available memory
incomingByte[1] = OneWire.read() //stores the data to the nexat available memory
}

int discoverOneWireDevices(void) {
byte i;
byte present = 0;
byte data[12];
byte addr[8];
int count = 0;

Serial.println("Looking for 1-Wire devices..."); // code to search for all 1-wire devices on the bus.
// This can be up to 127
while(oneWire.search(addr)) {
Serial.print("Found '1-Wire' device with address: ");
for( i = 0; i < 8; i++) {
Serial.print("0x");
if (addr < 16) {

  • Serial.print('0');*
  • }*
    _ Serial.print(addr*, HEX);_
    _
    if (i < 7) {_
    _
    Serial.print(", ");_
    _
    }_
    _
    }_
    _
    if ( OneWire::crc8( addr, 7) != addr[7]) { // a simple CRC to confirm the validity of the 1 wire address*_
    * Serial.println("CRC is not valid!");*
    * return 0;*
    * }*
    * Serial.println();*
    * count++;*
    * }*
    * Serial.println("No more devices available");*
    * oneWire.reset_search();
    _
    return count;_
    _
    }*_
    I'm still having some problems, can anyone help?
    Thanks
    Gareth

Please use code tags.

Read this before posting a programming question

I'm still having some problems, can anyone help?

What problems?

How to use this forum