i2c programming question...EMERGENCY

hello mates;

i got a trouble about two arduinos comminication. all codes and results are at the attachments, please have a look at that :wink:

ı want to do comminication between two arduinos via master/slave protocol(i2c). i can get "hello" message but when i want to try get number, for example i want to write at serial port from 0 to 100 around every values , i always get 0..31 around every values but not 32+ . i also tried to change master get byte codes and it didnt work !

if you have any example about sending value slave to master with that comminication protocol, please share with me..

The only emergency I see is a big issue on OSI Level 8, providing source code in jpgs and faulty comments in the code.

imho you are mixing ups char with int .

Qwertyqwq:
if you have any example about sending value slave to master with that comminication protocol, please share with me..

Have you wanted to say that you need an example which describes how to send a data byte from an active slave (the 2nd Arduino UNO) to master (the first Arduino UNO) using I2C Bus? If so, these are the clues which you must frame within the setup() and loop() functions of the IDEs of both Master and Slave to come up with a working program (s).

i2cy.png

1. Assume the 7-bit slave address is given by the symbolic name : slaveAddress.

2. Now, tell the Master to check that the Slave is present. The codes are;

Wire.beginTransmission(slaveAddress);
busStatus = Wire.endTransmission()
if (busStatus != 0x00)
{
   Serial.print("Slave is not present...!");
   while(1); //wait for ever
}

3. Tell the Master to command (arbitrary command code is 0x14) the Slave to keep one byte data (say, 0x23) in its buffer from which the Master will read it later. The codes are:

Wire.beginTransmission(slaveAddress);
Wire.write(0x14);         //0x14 is the arbitrary command code for the Slave.
Wire.endTransmission()

4. Once the code 0x14 of Step-3 goes to the Slave (the 2nd UNO), the slave will be immediately interrupted and will go to the following routine and will read the arrived data byte. If the arrived data byte is really 0x14, it will set a flag.

void receiveEvent(int howmany)
{
   byte x = Wire.read();
   if (x == 0x14)
   {
      bool flag = HIGH;
   }
   else
   {
       ;
    }
}

5. For the Slave to execute the routine of Step-4. the following declaration must be included in the setup() function of the Slave.

Wire.onReceive(receiveEvent);

6. Tell the Master to issue 'data read request" command to the Slave for reading 1-byte data which is already present in the loca buffer of the Slave. The codes are:

Wire.requestFrom(slaveAddress, 1);

7. After the execution of the above command, the Slave is immediately interrupted. The Slave goes to the following routine and writes the data byte (0x23) (after checking flag status) into its local buffer. The codes are:

void sendEvent(int howmany)
{
   if(flag == HIGH)
   {
       Wire.write(0x23);
    }
    else
    {
       ;
     }
}

8. For the Slave to execute the routine of Step-7, the following declaration must be included in the setup() function of the Slave.

Wire.onRequest(sendEvent);

9. After the execution of the instruction of Step-6 and 7, the data (0x23) from the local buffer of the Slave comes into the local buffer of the Master. Now, tell the Master to read the data byte (0x23) from its local buffer and show on the Serial Monitor. The codes are:

byte y = Wire.read();
Serial.println(y, HEX);

i2cy.png

thank you for all reply. I ll fix it .

and many thanks to GolamMostafa.. :slight_smile:

We would like to participate in fixing your problem. Given below the program structures for both Master-UNO and Slave-UNO. Please pick up codes from Post#2 and put them at the appropriate place. Kindly, post back the filled up programs so that we can see what you have done.

Master-UNO Codes:

#include<Wire.h>

void setup()
{
   Serial.begin(9600);
   Wire.begin();
   //pick up codes from Post#2 and place them here if necessary
}

void loop()
{
   //pick up codes from Post#2 and place them here if necessary
}

Slave-UNO Codes:

#include<Wire.h>
#define slaveAddress 0x20

void setup()
{
   Serial.begin(9600);
   Wire.begin(slaveAddress);
   //pick up codes from Post#2 and place them here if necessary
}

void loop()
{
   //pick up codes from Post#2 and place them here if necessary
}