Master/Slave communication

My main code for Master Arduino is :

#include <Wire.h>
  void setup()
 {
    

     Wire.begin();
    delay(3000);
}

void loop() 
{



serial.begin(9600);




if (address == 0x01)
{
  Wire.beginTranmission(0x01);
  Wire.send(byte*);
  Wire.endTransmission();
}

else if (address == 0x02)
{

  Wire.beginTransmission(0x02);
  Wire.send(byte*);
  Wire.endTransmission();
}

else if (address == 0x03)
{

  Wire.beginTransmission(0x03);
  Wire.send(byte*);
  Wire.endTransmission();
}

else if (address ==0x04)
{

  Wire.beginTransmission(0x04);
  Wire.send(byte*);
  Wire.endTransmission();
}

else 
{ 
Serial.println("Lathos dief8unsh");

}

 return 0;
}

I want my master Arduino to send data to 4 slaves Arduino. I want my 4 slaves to have some id to sent the data to the one that i want. I couldn't find any examples for that purpose so can anyone help me with this?

Surely your slaves already have an ID such as 0x01, 0x02 and 0x03

Just use Wire.begin(); with the appropriate address in the receiver code

My problem is with command :

 Wire.send(byte*);

I want to sent data to slave to read it. Is my command wrong?

You need to pass the bytes to send, "byte*" is a type, not a value

Wire.send(byte*);

What data did you expect that command to send ?

I need to send every time 1 txt file of max size 30 KB. How can it be done with command Wire.write () or Wire.send() ?
Can anyone help me with code?

Let us follow these steps to make I2C Bus (aka TWI Bus) Master-Slave Setup working:

1. Build the following connection between 2 UNOs.

2. Upload the following sketch in the flash of UNO-1. Bring in the Serial Monitor of UNO-1.

#include<Wire.h>

void setup() 
{
  Serial.begin(9600);
  Wire.begin();
  pinMode(8, INPUT_PULLUP); //DPin-8 as input with int. pull-up
  
  while(digitalRead(8) !=LOW)//checking if K1 is closed
    ;

  Wire.beginTransmission(0x21);  //sending command/data to Slave
  Wire.write(0x02);   //command code to blink L of slave
  Wire.write(0x05); //data to blink L of UNO-2 (Slave)for 5 times
  Wire.endTransmission();
}

void loop() 
{
  
}

3. Upload the following sketch in the flash of UNO-2 (the Slave).

#include<Wire.h>

void setup()
{
  Wire.begin(0x21);  //UNO-2's slave address
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Wire.onReceive(receiveEvent); //MCU enters into this SUR once a data byte arrives in TWI Logic
}

void loop()
{

}

void receiveEvent(int howmany)
{
  byte x1 = Wire.read();  //command byte 0x02 is saved in X1
  byte x2 = Wire.read();  //data byte 0x05 is saved in x2
  sei();   //allows calling Arduino's delay) subroutine

  if (x1 == 0x02)
  {
    do
    {
      digitalWrite(13, HIGH);
      delay(1000);
      digitalWrite(13, LOW);
      delay(1000);
      x2--;
    }
    while (x2 != 0);
    x1 = 0x00;
  }
}

4. Press button K1 at the Master side. Check that L (built-in LED) of UNO-2 is blinking for 5 times at 1-sec interval.

This is the end of a simple function check of TWI Bus protocol.

sei();   //allows calling Arduino's delay) subroutine

??

UKHeliBob:

sei();   //allows calling Arduino's delay) subroutine

??

equivalent to

  interrupts();

MarkT:
equivalent to

  interrupts();

I know what it does, but why have it in the code, and why that comment ?

Bob, he is using delay() in the receiveEvent() interrupt handler.

The tutorial was going so well up to that point! Enabling interrupts in an interrupt handler which is non-reentrant is asking for trouble.

Spending 10 seconds (or more) inside the interrupt? Really stupid.

Really stupid.

I am fully aware about this! This is a demonstrative setup; a real field application will spend minimum amount of time in the ISR; there will be no question of re-enabling the interrupt structure for calling the 1-sec time delay SUR.

Bob, he is using delay() in the receiveEvent() interrupt handler.

Yes, but it actually an interrupt handler or an event handler ?

Good point Bob. I never looked that closely at the code. I expect that it's inside the interrupt as there's no operating system that would read a flag set by the TWI interrupt handler to call an event handler.

GolamMostafa:
I am fully aware about this! This is a demonstrative setup; a real field application will spend minimum amount of time in the ISR; there will be no question of re-enabling the interrupt structure for calling the 1-sec time delay SUR.

So don't put really stupid code on the forum. This is a resource that's going to hang around for many years. With the good diagrams and good explanations, I was going to keep this one to direct future questions this way. But with that poor example teaching bad habits, it's going to make more problems than it solves.

I just hope that the future readers of this read past your nice diagrams and get to this part of the discussion.

So don't put really stupid code on the forum.

It is very very relative. Information does not arrive at hand over night. One collects ideas; from ideas comes the message; from message comes the data; from data comes the information. The objective of the setup was to blink the built-in LED (L) of the Slave for 5 times at 1-sec interval. The poster was required to do whatever he needed whether it was stupidity or not to achieve his declared objective. It is the pleasure for the part of the OP and the poster to see that the setup is working as planned and expected. It could not satisfy your very fine and fertile level of intellectualism -- sorry for that! (Bad things are always rejected by the readers; it does not require 3rd party's advocacy.)

pipertzisdimitris:
I need to send every time 1 txt file of max size 30 KB. How can it be done with command Wire.write () or Wire.send() ?
Can anyone help me with code?

What Wire.send() command? Is this some other wire library? I see a write() command.....

The write() command sends bytes where (byte *) is a pointer to byte variable, not a bye value.

The IDE Examples in the Wire Library section has working examples of basic Wire operations.

GolamMostafa:
I am fully aware about this! This is a demonstrative setup; a real field application will spend minimum amount of time in the ISR; there will be no question of re-enabling the interrupt structure for calling the 1-sec time delay SUR.

It demonstratively demonstrates multiple idiocies to try and fob a "crafty" trick.

It demonstratively demonstrates multiple idiocies to try and fob a "crafty" trick.

That's why the French people say that the rules are for monkeys. These are the monkeys that are going to design/build commercial circuits based on the demonstrative examples?

GolamMostafa:
That's why the French people say that the rules are for monkeys.

And "the French people" who say this apply that to commercial circuit design?

Or are they saying that about something else?

Hey, France is the home of postmodernists, the biggest intellectual FRAUDS in the world.

My question was how can i send a txt file from master to a slave with specific ID location of a slave Arduiono. I want to send a txt file from my pc, using COM port, to master Arduino and then the master to send it in the right ID.My sample code is

#include <Wire.h>


byte data ;

int slaveAdd ;

  void setup()
 {
    
Serial.begin(9600);
Wire.begin();
delay(3000);
}
void loop() 
{



if (slaveAdd == 0x01)
{
  Wire.beginTransmission(0x01);
  Wire.write(data);
  Wire.endTransmission();
}

I am a new programmer and i would appreciate which corrections.