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