Troubles with slave write on master.

Good day. My name is EDGAR and I have troubles when I try to read digital pins llike inputs in the slave and send to the master like a requestEvent.

I can send the data and change only with IF.

but, when I try to read One by one the pins like Inputs and compare to HIGH to LOW for send 0 or 1 with Wire.write only send 0.

I have almost 6 hours trying and now I don't know where I find the solution.

And there are my codes, 2 options of my Slave:

Thanks a lot.

Master:

int lugar;
int i,k,j;

#include <Wire.h>

void setup() {
Wire.begin();
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
}

void loop() {

for(i=2; i<6; i++)
{
lugar=digitalRead(i);
if(lugar==HIGH)
{
i=i-1;
Serial.print("complete ");
Serial.println(i);
Serial.println(lugar);
i=i+1;
}
else
{
i=i-1;
Serial.print("Empty ");
Serial.println(i);
Serial.println(lugar);
i=i+1;
}
}
if(i>13)
{
Slave1 ();
}
delay(1000);
}

void Slave1 ()
{
for(i=14;i<26;i++)
{
Wire.requestFrom(1,1);
while (Wire.available())
{
int c = Wire.read();
if(c==0)
{
i=i-1;
Serial.print("Empty ");
Serial.println(i);
Serial.println(c);
i=i+1;
}
else
{
i=i-1;
Serial.print("complete ");
Serial.println(i);
Serial.println(c);
i=i+1;
}
}
delay(1000);
}
}

Slave: change only with the if nad j=1, or j=1.

int i,j=0,k;
int lugar1;

#include <Wire.h>

void setup() {
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
Wire.begin(1);
Wire.onRequest(requestEvent);
Serial.begin(9600);
}

void loop()
{
delay(600);
}

void requestEvent()
{
if(j==0)
{
Wire.write(0);
j=1;
}
else
{
Wire.write(1);
j=0;
}
}

SLAVE: trying to read digital pins but nothing happens.

int i,j=0,k;
int lugar1, y;

#include <Wire.h>

void setup() {
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
Wire.begin(1);
Wire.onRequest(requestEvent);
Serial.begin(9600);
}

void loop()
{
for(i=2;i<14;i++)
{
lugar1=digitalRead(i);
if(lugar1==LOW)
{
j=0;
requestEvent();
}
else
{
j=1;
requestEvent();
}
}
delay(600);
}

void requestEvent()
{
if(j==0)
{
Wire.write(0);
}
else
{
Wire.write(1);
}
delay(500);
}

COM_I2C_Mstr_R2.ino (1008 Bytes)

COM_I2C_Slv_R2.ino (541 Bytes)

I don't understand what exactly your problem is. BTW, you forgot the code tags around your code so it might have been mangled by the forum system.

Keep in mind that requestEvent() is called in interrupt context, so every global variable used there must be declared volatile, otherwise the compiler might optimize it away.

@ cahido

Please, try to paraphrase your statements by editing your original post. Paraphrasing refers to the rearrangement of the words/phrases of your sentences so that the readers can understand what you have wanted to convey.

In the mean time, kindly go through the following check list and let the Forum know your opinion. The Forum will certainly help/guide you in the right way so that you can solve your problem yourself.

1. You have two Arduino Boards, and these are Arduino UNOs.
2. The two UNOs are connected by I2C Bus. Have you connected 2x4.7k or 2x2.2k pull-up resistors with the SCL (A5) and SDA (A4) lines of the TWI Bus?

3. UNO-1 is the AMster, and UNO-2 is the Slave; mark them with labels.
4. You have (say, 2) input switches connected with the input pins of UNO-2.
5. You want to read the input data of UNO-2 and shown them on the Serial Monitor connected with UNO-1.

BTW: Please, answer to my question with reference to your this statement -- I can send the data and change only with IF.

Q1: Who is 'I' here? Is it yourself or UNO-1 (the Master)?
Q2: What does 'IF' mean?

cahido, when you run into problem, you should take a step back and make it a very simple test sketch.
Do you want to display with the Master what the pins of the Slave are ?
Is it with two Arduino Uno boards ? It will not work with an Arduino Leonardo as Slave, because pin 2 and 3 are the I2C bus.

@pylon and @GolamMostafa gave good answers, but I see too many problems with your sketch. It will take a lot of time to correct your sketch. Therefor I decided to give you a start with these examples:

Master

#include <Wire.h>

void setup() 
{
  Serial.begin(9600);

  Wire.begin();
}
 

void loop()
{
  Wire.requestFrom( 1, 1);
  if( Wire.available() == 1)
  {  
    byte inputBits = Wire.read();
  
    Serial.println( "-------------");
    for( int i=2; i<=6; i++)
    {
      Serial.print( "pin ");
      Serial.print( i);
      Serial.print( " = ");
      if( bitRead( inputBits, i))
      {
        Serial.print( "High");
      }
      else
      {
        Serial.print( "Low");
      }
      Serial.println();
    }
  }
  else
  {
    Serial.println( "Slave not connected");
  }
  delay( 2000);
}

Slave

#include <Wire.h>

volatile byte inputBits;
 
void setup() 
{
  Serial.begin(9600);

  for( int i=2; i<=6; i++)
  {
    pinMode(i, INPUT);
  }
  
  Wire.begin(1);
  Wire.onRequest(requestEvent);
}
 

void loop()
{
  for( int i=2; i<=6; i++)
  {
    int bitValue = digitalRead( i) == HIGH ? 1 : 0;
    bitWrite( inputBits, i, bitValue);
  }
}


void requestEvent()
{
  Wire.write( inputBits); 
}

@cahido

Where are you? Wake up and join the Forum! We have been waiting for you. You made the post by the morning, and now it is evening.

Look at the post; @Koepel has posted a beautiful program for you; it contains a lot of interesting styles of programming. Please, make this program working. We assure that we look into your original program once you have made @Koepel's programm working.

We will also explain syntax and semantics of every line of the @Koepel program. After that we will let you ask 5 or more questions on @Koepel program.

@GloamMostafa: This is a forum and not a chat. Please act accordingly. Thank you.

GolamMostafa:
@ cahido

Please, try to paraphrase your statements by editing your original post. Paraphrasing refers to the rearrangement of the words/phrases of your sentences so that the readers can understand what you have wanted to convey.

In the mean time, kindly go through the following check list and let the Forum know your opinion. The Forum will certainly help/guide you in the right way so that you can solve your problem yourself.

1. You have two Arduino Boards, and these are Arduino UNOs.
2. The two UNOs are connected by I2C Bus. Have you connected 2x4.7k or 2x2.2k pull-up resistors with the SCL (A5) and SDA (A4) lines of the TWI Bus?

3. UNO-1 is the AMster, and UNO-2 is the Slave; mark them with labels.
4. You have (say, 2) input switches connected with the input pins of UNO-2.
5. You want to read the input data of UNO-2 and shown them on the Serial Monitor connected with UNO-1.

BTW: Please, answer to my question with reference to your this statement -- I can send the data and change only with IF.

Q1: Who is 'I' here? Is it yourself or UNO-1 (the Master)?
Q2: What does 'IF' mean?

Thanks a lot.

OK.

when I use IF for change the value of "j" from "0" to "1" and with that send with Wire.write(0) and Wire.write(1) in alternated way.

@GloamMostafa: This is a forum and not a chat. Please act accordingly. Thank you.

Liberty and discipline must go together; but, we fail to maintain it in many occasions owing to the very inherent nature of the human being and thus an inevitable chaos appears in the long run, which can only be controlled by the tough tidings of the scriptures!!

Koepel:
cahido, when you run into problem, you should take a step back and make it a very simple test sketch.
Do you want to display with the Master what the pins of the Slave are ?
Is it with two Arduino Uno boards ? It will not work with an Arduino Leonardo as Slave, because pin 2 and 3 are the I2C bus.

@pylon and @GolamMostafa gave good answers, but I see too many problems with your sketch. It will take a lot of time to correct your sketch. Therefor I decided to give you a start with these examples:

Master

#include <Wire.h>

void setup()
{
  Serial.begin(9600);

Wire.begin();
}

void loop()
{
  Wire.requestFrom( 1, 1);
  if( Wire.available() == 1)
  { 
    byte inputBits = Wire.read();
 
    Serial.println( "-------------");
    for( int i=2; i<=6; i++)
    {
      Serial.print( "pin ");
      Serial.print( i);
      Serial.print( " = ");
      if( bitRead( inputBits, i))
      {
        Serial.print( "High");
      }
      else
      {
        Serial.print( "Low");
      }
      Serial.println();
    }
  }
  else
  {
    Serial.println( "Slave not connected");
  }
  delay( 2000);
}




**Slave**


#include <Wire.h>

volatile byte inputBits;

void setup()
{
  Serial.begin(9600);

for( int i=2; i<=6; i++)
  {
    pinMode(i, INPUT);
  }
 
  Wire.begin(1);
  Wire.onRequest(requestEvent);
}

void loop()
{
  for( int i=2; i<=6; i++)
  {
    int bitValue = digitalRead( i) == HIGH ? 1 : 0;
    bitWrite( inputBits, i, bitValue);
  }
}

void requestEvent()
{
  Wire.write( inputBits);
}

I'm using 2 ARDUINO UNO, for that communication but when I try to send the value with Wire.write 0 or 1 i can't choose one of them.

Liberté, égalité, fraternité

Liberty and discipline must go together; but, we fail to maintain it in many occasions owing to the very inherent nature of the human being and thus an inevitable chaos appears in the long run, which can only be controlled by the tough tidings of the scriptures!!

Please contribute to the subject and keep your opinions about life to yourself or post social media instead. .

Koepel:
cahido, when you run into problem, you should take a step back and make it a very simple test sketch.
Do you want to display with the Master what the pins of the Slave are ?
Is it with two Arduino Uno boards ? It will not work with an Arduino Leonardo as Slave, because pin 2 and 3 are the I2C bus.

@pylon and @GolamMostafa gave good answers, but I see too many problems with your sketch. It will take a lot of time to correct your sketch. Therefor I decided to give you a start with these examples:

At this moment I can Read Pins[2,3,4,5,6,7] from Slave and in the Master all of them, but I can't read the Pins[8,9,10,11,12,13], all the time show me LOW inclusive if I have directly the 5VCC.

I know that these pins are in other PORT.
but how can read that pins.

Can you Help me again?

@Koepel, @pylon and @GolamMostafa

Have you thought about it how to do that ?
A 'byte' is 8 bits. If you want more than 8 pins, then it will not fit into the 8 bits.

Do you want two bytes (16 bits) or do you want an array of 12 bytes ?
Some prefer the I2C_Anything. A include file, "I2C_Anything.h" needs to be added to your project. Can you read the Master and Slave code to see how it is used. Do you like the way it is used ?

Can you show your sketch ? to show what you have tried ?

Koepel:
Have you thought about it how to do that ?
A 'byte' is 8 bits. If you want more than 8 pins, then it will not fit into the 8 bits.

Do you want two bytes (16 bits) or do you want an array of 12 bytes ?
Some prefer the I2C_Anything. A include file, "I2C_Anything.h" needs to be added to your project. Can you read the Master and Slave code to see how it is used. Do you like the way it is used ?

Can you show your sketch ? to show what you have tried ?

Thanks for your answer.
I try ro read the 12 BITS or 12 PINS that We can put like INPUT.
the sketch are simple

I need read if the switch that stay connected at the pin is Open or Close.

I'm trying to use two arduino UNO for read 24 switchs 12 Master and 12 Slave; but at this moment I only can read 18 with your help.

This is not a sketch writing service. On this forum you show what you have tried and we tell where the problem is and in which direction to go. I decided to give you a working sketch, because that was easier then to continue with what you had. Can you try to answer a few of my questions ?

"Have you thought about it how to do that ?"
"Do you want two bytes (16 bits) or do you want an array of 12 bytes ?"
"Do you like the way it is used ?"
"Can you show your sketch ?"

Koepel:
This is not a sketch writing service. On this forum you show what you have tried and we tell where the problem is and in which direction to go. I decided to give you a working sketch, because that was easier then to continue with what you had. Can you try to answer a few of my questions ?

"Have you thought about it how to do that ?"
"Do you want two bytes (16 bits) or do you want an array of 12 bytes ?"
"Do you like the way it is used ?"
"Can you show your sketch ?"

Let me sleep and later I will show the sketch that I want to do.

1.- Electronic way to do, I have no troubles.
2.- ok I want read two bytes (16 bits), PORTD D(6 bits input output) PORT C(6 bits input output) from slave.

//MASTER
 
#include <Wire.h>

int lugar;
int i,k,j; 
 
void setup() 
{
  // Unimos este dispositivo al bus I2C
  Wire.begin();
  Serial.begin(9600);
  for(i=2;i<14;i++)
  {
   pinMode(i, INPUT);
  }
 }
 

void loop()
{
   for(i=2; i<14; i++)
  {
   lugar=digitalRead(i);
   if(lugar==HIGH)
  {
    i=i-1;
    Serial.print("complete ");
    Serial.println(i);
    
   i=i+1;
  }
   else
   {
    i=i-1;
    Serial.print("Empty ");
    Serial.println(i);
    i=i+1;
   }
   delay(800);
  }
  if(i>13)
  {
  Slave ();
  }
} 

void Slave () 
{
    Wire.requestFrom(1,1);    
    if (Wire.available()==1) 
    {
    byte inputBits = Wire.read();
    for(int i=2;i<14;i++)
    {
        
    if(bitRead(inputBits,i))
    {
      i=i+11;
      Serial.print("Complete ");  
      Serial.println(i);
      i=i-11;
    }
  else
  {
    i=i+11;
    Serial.print("Empty ");     
    Serial.println(i);
    i=i-11;
   }
   delay(800);
 }
 Serial.println("-------------------------------------");
}
else
{
 Serial.println("Not Comunication Between MSTR SLV"); 
}
delay(2000);
}
//SLAVE


#include <Wire.h>

volatile byte inputBits;

void setup()
{
  Serial.begin(9600);

  for( int i=2; i<14; i++)
  {
    pinMode(i, INPUT);
  }
  Wire.begin(1);
  Wire.onRequest(requestEvent);
}
 

void loop()
{
  for( int i=2; i<14; i++)
  {
    int bitValue = digitalRead( i) == HIGH ? 1 : 0;
    bitWrite( inputBits,i,bitValue);
  }
}


void requestEvent()
{
  Wire.write(inputBits);
}