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