Go Down

Topic: [MERGED] Master/Slave communication (Read 2197 times) previous topic - next topic

pipertzisdimitris

May 15, 2018, 09:01 am Last Edit: May 18, 2018, 03:11 pm by surbyte
My main code for Master Arduino is :


Code: [Select]

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

UKHeliBob

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
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

pipertzisdimitris

My problem is with command :

Code: [Select]
Wire.send(byte*);


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

MarkT

You need to pass the bytes to send, "byte*" is a type, not a value
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

UKHeliBob

Code: [Select]
Wire.send(byte*);
What data did you expect that command to send ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

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?

GolamMostafa

#6
May 15, 2018, 12:32 pm Last Edit: May 15, 2018, 01:11 pm by GolamMostafa
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.
Code: [Select]
#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).
Code: [Select]
#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.


UKHeliBob

Code: [Select]

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

??
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

MarkT

Code: [Select]

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

??
equivalent to
Code: [Select]

  interrupts();
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

UKHeliBob

equivalent to
Code: [Select]

  interrupts();

I know what it does, but why have it in the code, and why that comment ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

MorganS

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.
"The problem is in the code you didn't post."

GolamMostafa

#11
May 15, 2018, 06:01 pm Last Edit: May 15, 2018, 06:23 pm by GolamMostafa
Quote
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.

UKHeliBob

Quote
Bob, he is using delay() in the receiveEvent() interrupt handler.
Yes, but it actually an interrupt handler or an event handler ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

MorganS

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.

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.
"The problem is in the code you didn't post."

GolamMostafa

#14
May 15, 2018, 06:59 pm Last Edit: May 15, 2018, 07:10 pm by GolamMostafa
Quote
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.)

 

Go Up