I2C reads 255 in stead of the 0 I send, i'm dazzled! [Solved!]

Hello,

So I am trying to send 3 bits to my slave, and return them, nothing to hard. However, when the slave sends 3 zero’s, the master reads them as a 0, and 2 time 255.
I’ve been searching the internet for some answers but i’m dazzled.

This is the master code atm:

#include <Wire.h>

byte a = 0b0;
byte b = 0b0;
byte c = 0b0;

byte d = 0b0;
byte e = 0b0;
byte f = 0b0;

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

void loop()
{
  Request();
  delay(500);
  
  /* if(d == 0b0)
  {
  (a = 0b1);
  }else{
    (a=0b0);
  }
  if(e == 0b0)
  {
  (b = 0b1);
  }else{
    (b=0b0);
  }
  if(f == 0b0)
  {
  (c = 0b1);
  }else{
    (c=0b0);
  }
  */
  
  Send();
  delay(500);
}
  
  void Send()
  {
  Wire.beginTransmission(2);
  Wire.write(a);
  Wire.write(b);
  Wire.write(c);
  Wire.endTransmission();
  Serial.print("a is ");
  Serial.println(a);
  Serial.print("b is ");
  Serial.println(b);
  Serial.print("c is ");
  Serial.println(c);
  Serial.println(" ");
  }
  
  void Request()
  {
    Wire.requestFrom(2, 3);
    d=Wire.read();
    e=Wire.read();
    f=Wire.read();
    Serial.print("d is ");
    Serial.println(d);
    Serial.print("e is ");
    Serial.println(e);
    Serial.print("f is ");
    Serial.println(f);
    Serial.println(" ");
  }

note that I have declared all 6 bytes 0b0, what i tough to be the bit-wise 0, i removed all the statements that change these bits.

Then the slave code:

#include <Wire.h>
byte a = 0b0;
byte b = 0b0;
byte c = 0b0;

byte d = 0b0;
byte e = 0b0;
byte f = 0b0;

void receiveEvent(int howMany);

void setup()
{
  Wire.begin(2);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
  Serial.begin(9600);
}

void loop()
{
  delay(100);
  
  
}

void receiveEvent(int howMany)
{
  a = Wire.read();
  b = Wire.read();
  c = Wire.read();
  Serial.print("a is ");
  Serial.println(a);
  Serial.print("b is ");
  Serial.println(b);
  Serial.print("c is ");
  Serial.println(c);
  Serial.println(" ");
  
  /*d = a;
  e = b;
  f = c;
  */
}

void requestEvent()
{
  Wire.write(d);
  Wire.write(e);
  Wire.write(f);
  Serial.print("d is ");
  Serial.println(d);
  Serial.print("e is ");
  Serial.println(e);
  Serial.print("f is ");
  Serial.println(f);
  Serial.println(" ");
}

Same goes for this code, all 6 bytes declared 0b0, and no changes happen.

As far as i can see, the master asks for the first 3 bytes, which read 0, and then sends his 3 bytes, which also read 0.

However the serial interface (reading from my master) reads:
d is 0
e is 255
f is 255

a is 0
b is 0
c is 0

d is 0
e is 255
f is 255

a is 0
b is 0
c is 0

The slave reads all zero’s, no problem.

Any help is much appreciated, once I get this working the bits will be used to control my robot by comparing the bits to set standards and so compute the next task.

First,
that
(code)
is
(awful);

You really, and I do mean REALLY, need to understand when parentheses are needed and when the just look dumb. Hint, yours are not needed in the master.

On the other hand, commented out code is obviously not part of the problem, so why did you post it? You have a delete key. Use it.

  Wire.beginTransmission(2);
  Wire.write(a);
  Wire.write(b);
  Wire.write(c);
  Wire.endTransmission();

Have you read anything about how the Wire library works? Begin, ONE write, end. You're done.

If you want to write more than one value, you need an array.

First,
Thanks for the reply.

This code may be awful, but its a quick test to see if i can get the bits i want, in the place i want. When weird values where returned i tried cutting some code out really quick, without removing it from my test to see if it solved the problem.
Your answer was just what I needed to continue, and finally put the rest of the code in place, i thank you for that, your tone however is most unpleasant…

There is one thing i’m still wondering about tough, and that is why did the rest of the values where transferred without any problems? I have been toying around with I2C for a while now and this is the first time this has happened to me.

I got it working like a charm, no special data of any kind is being send with this code, but it gets the information where it should be!

Master code:

#include <Wire.h>

byte a[] = {0,0,0};
byte d[] = {0,0,0};

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

void loop()
{
  Request();
  delay(500);
  
  Send();
  delay(500);
}
  
  void Send()
  {
  Wire.beginTransmission(2);
  Wire.write(a, 3);
  Wire.endTransmission();
  Serial.print("a is ");
  Serial.println(a[0]);
  Serial.println(a[1]);
  Serial.println(a[2]);
  Serial.println(" ");
  }
  
  void Request()
  {
    Wire.requestFrom(2, 3);
    byte index = 0;
    while(Wire.available() > 0 && index < 3)
    {
      d[index] = Wire.read();
      index++;
    }
    Serial.print("d is ");
    Serial.println(d[0]);
    Serial.println(d[1]);
    Serial.println(d[2]);
    Serial.println(" ");
  }

Slave code:

#include <Wire.h>
byte a[] = {0,0,0};
byte d[] = {0,0,0};

void receiveEvent(int howMany);

void setup()
{
  Wire.begin(2);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
  Serial.begin(9600);
}

void loop()
{
  delay(100);
}

void receiveEvent(int howMany)
{
  byte index = 0;
  while(Wire.available() > 0 && index < 3)
  {
    a[index] = Wire.read();
  }
  Serial.print("a is ");
  Serial.println(a[0]);
  Serial.println(a[1]);
  Serial.println(a[2]);
  Serial.println(" ");
}

void requestEvent()
{
  Wire.write(d, 3);
  
  Serial.print("d is ");
  Serial.println(d[0]);
  Serial.println(d[1]);
  Serial.println(d[2]); 
  Serial.println(" ");
}

I hope at least someone else wont have my problem :slight_smile:

Have you read anything about how the Wire library works? Begin, ONE write, end. You're done.

Wrong. You can have many writes between the beginTransmission() and the endTransmission() calls, else they wouldn't exist. The data is sent during the endTransmission() call so the write()s go to a buffer (which has limited space) but it's very common that you have to write multiple bytes and there's no need to use an array here (although you might use one).