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?
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.
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.
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.
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?
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.