I need your help with Wire: Slave and Master

Ok here is the thing: I need to be able to send strings (Chunks of Text) from Master to Slave via Wire using two Leonardo Boards.

I have used the examples provided in the IDE: File: Exaples: Wire: master_writer and slave_receiver.

The exaples work fine all together however when I try to change them as my need they just don't work the way I need then to work or the way I spect it ):

I'll provide a little examples with pictures so you ca see the outcome and the little change I made, it cameout weird, it does send the text and then it gets stuck it or frozen and it will only send text 1 time only and I want it to be continiously.

I'm not a pro I know you can tell hahaha.
So please help!

Master example

// 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(8); // transmit to device #8
  Wire.write("x is ");        // sends five bytes
  //Wire.write(x);              // sends one byte
  Wire.endTransmission();    // stop transmitting

 // x++;
  delay(500);
}

============================================================================
Slave Example

// 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(8);                // join i2c bus with address #8
  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.println(c);         // print the character
  }
 //int x = Wire.read();    // receive byte as an integer
//  Serial.println(x);         // print the integer
}



Your last picture shows the intended continuous behavior, because there you read the whole message.
The previous ones only read "all but the last" byte. As mentioned in the code comment.
That's why you get the howMany parameter in your receiveEvent function.
Alternatively you can read all while (Wire.available())

Thanks You for your quick reply that solved part of my problem.

Now I have another problem I need to be able to pull String from BT on Master and send it to Slave can you give me an idea??

We never transfer variable length readable ASCII data over I2C.
If you pack all the variables in a 'struct' and send the struct over I2C, that will be a lot easier.
An array of variables is even easier than a struct.

Why do you want to use the I2C bus ? Both Leonardo boards have a spare hardware Serial1 port at pin 0 and 1. You can use those to transfer data.

I'm sorry to say, but the official examples are bad.
You should not do this:

while (1 < Wire.available()) { // loop through all but the last

The parameter 'howMany' tells how much bytes are received. If the data package is a fixed length, then you can test if 'howMany' is the expected number of bytes.

How long is that String ? The maximum package size is 32 bytes for an Arduino Leonardo.

I have written a few basic notes on Github: Arduino in Target mode.

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

I'm so sorry! ):

There are examples with the Wire library (master_writer / slave_receiver)
And of course @Koepel is right: messages are limited to 32 byte, so variable length texts are not optimal for I2C.
But if this limit is acceptable and there's no need to asynchronously stream "long" text, each I2C transmission can be of different length, if necessary, and can of course contain readable ascii characters, if that's what you really want/need.

Other post/duplicate ALSO DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

See the links below !

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.