MASTER WRITER / SLAVE RECEIVER UNDER 'LEARNING': SOLVED

Does not work.

Used copy/paste to create the sketches. The sketches are pasted below.

See: "--- THE SKETCH HANGS HERE ---" Below in the Master Writer Code.

Very new to Arduino; What am I missing?

Master Writer Code - Program for Arduino 1

// Wire Master Writer
// by Nicholas Zambetti http://www.zambetti.com

// Demonstrates use of the Wire library
// Writes data to an I2C/TWI slave device
// Refer to the "Wire Slave Receiver" example for use with this

// Created 29 March 2006

// This example code is in the public domain.

#include <Wire.h>

void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
}

byte x = 0;

void loop()
{
Wire.beginTransmission(4); // transmit to device #4
Wire.write("x is "); // sends five bytes
Wire.write(x); // sends one byte

--- THE SKETCH HANGS HERE ---

Wire.endTransmission(); // stop transmitting

x++;
delay(500);
}

Slave Receiver Code - Program for Arduino 2

// Wire Slave Receiver
// by Nicholas Zambetti http://www.zambetti.com

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this

// Created 29 March 2006

// This example code is in the public domain.

#include <Wire.h>

void setup()
{
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}

void loop()
{
delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
while(1 < Wire.available()) // loop through all but the last
{
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}

Hi benlights

What type of Arduino is the master and the slave? How are they connected?

How have you determined that the program is hanging exactly at the point you have marked?

Regards

Ray

Both are Uno's. Pin4 To Pin4 and Pin5 To Pin5.

You can see the code under "Learning" > Examples > Wire Library

Both are UNO. Pin 4 To Pin 4 and Pin 5 To Pin 5.

You can see the code under "Learning" > Examples > Wire Library

Thanks for the information. You have not said how you know where the program stops working. What output do you see from the slave program?

Couple of suggestions in the meantime.

You may need pullup resistors from SDA to +5V and from SCL to +5V. Try 4.7k for each.

Also, with the slave program running on one of the Arduinos, run this I2C scanner program on the other (instead of the master program). What results do you get?

http://forum.arduino.cc/index.php?topic=197360.0

Re: Discovery.

Program #2 (below) Works!

These Programs Are Under "Learning/Examples/Wire Library

  1. Master Writer/Slave Receiver: This Is The Program Which Does Not Work.

  2. Master Reader/Slave Sender: This Works!

No problems with #2 so, would expect #1 to also work. What is going on?

THE SOLUTION:

Get a good night's sleep, don't modify anything and try again the next day.

Time heals all.

Thank you.

So #1 works as well? That's good to hear.

Benlights,

I am having the same problem as you did. What exactly was going wrong with your code? What did you change?

Any help would be much appreciated!

v/r