I2C Master/Slave Master Output Problem

Hi
I am using two arduinos board an Ethernet as Master and a Duemilanove as slave.On the slave I have connected a button.What I want is that when I press on the button a 2 is send to the master and when I release it a 3 is send all via I2C.The problem is that on the master output when the button is not pressed I have 0 as ouput when I press the button the output is 2 and when released 3.
Also when I request 2 bytes from the slave and send 2 bytes with the slave with an array I first have 0 and then 255 as ouput followed by what I actually send.
Please help me I don’t figure out what the problem is.

Here is my Slave code

#include <Wire.h>

int address = 8;

const int pushButtonPin = 2;


void setup() {
    Wire.begin(address);
  pinMode(pushButtonPin, INPUT); 
  Wire.onRequest(onRequest);
}

void loop() { 
  delay(100); 
}


void onRequest(){
 static bool previousState = LOW;                
                                                   
  bool currentState = digitalRead(pushButtonPin);  
  if (currentState != previousState) {            
    if (currentState == HIGH) {                      
     byte SendMidi  =  2;                   
  Wire.write(SendMidi);       
    } else {                                        
       byte   SendMidi =  3;
  Wire.write(SendMidi);     
    }
    previousState = currentState;                 
  }
}

MasterCode

#include <Wire.h>

void setup() {
  // put your setup code here, to run once:
  Wire.begin();
  Serial.begin(9600);
  
}

void loop() {
 // put your main code here, to run repeatedly:
     Wire.requestFrom(8,1);
      while(Wire.available()){
      byte c = Wire.read();
      Serial.println(c);         
}
       delay(1000);
}

Output

your output:

in your slave code:

romaric_nkr:

void onRequest(){

static bool previousState = LOW;               
                                                 
  bool currentState = digitalRead(pushButtonPin); 
  if (currentState != previousState) {           
    if (currentState == HIGH) {                     
    byte SendMidi  =  2;                 
  Wire.write(SendMidi);     
    } else {                                       
      byte  SendMidi =  3;
  Wire.write(SendMidi);   
    }
    previousState = currentState;               
  }
}

only replies 2 or 3 IF the was a change in the state of your button from the last read.

if not then there is no reply; hence the zero’s you are receiving.

something like this as your slave code should fix it:

#include <Wire.h>

int address = 8;

const int pushButtonPin = 2;


void setup() {
    Wire.begin(address);
    pinMode(pushButtonPin, INPUT);
    Wire.onRequest(onRequest);
}

void loop() {
  // do nothing
}


void onRequest(){             
 byte SendMidi; 
                                               
  bool currentState = digitalRead(pushButtonPin); 
          
  if (currentState == HIGH) {                     
     SendMidi  =  2;                   
     Wire.write(SendMidi);       
  } 
  else {                                       
     SendMidi =  3;
     Wire.write(SendMidi);     
  }
}

Thanks the problem is fixed.

romaric_nkr: Thanks the problem is fixed.

Wire.onRequest(onRequest);

onRequest is a keyword, and you are using this for the name of a user-defined handler; whereas, the Arduino Reference Manual suggests to use myHandler. Arduino Programmers generally use requestEevent.

Wire.onRequest(requestEvent);

void requestEvent(int howMany)
{

}