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)