Reply To: interface an Arduino Uno as an i2c slave to EV3?

HomeForumsMonoBrick EV3 Firmwareinterface an Arduino Uno as an i2c slave to EV3?Reply To: interface an Arduino Uno as an i2c slave to EV3?

#4443
Author Image
Helmut Wunder
Participant

ps,
an Arduino Sketch code for i2c communication is already available, but maybe it has to be changed to work reliably with the corresponding C# code:

#include <Wire.h>

#define SLAVE_ADDRESS 0x04
int number = 0;
int state = 0;
 
void setup() {
    pinMode(13, OUTPUT);
    Serial.begin(9600);         // start serial for output
    // initialize i2c as slave
    Wire.begin(SLAVE_ADDRESS);
 
    // define callbacks for i2c communication
    Wire.onReceive(receiveData);
    Wire.onRequest(sendData);
 
    Serial.println("Ready!");
}
 
void loop() {
    delay(100);  
}
 
// callback for received data
void receiveData(int byteCount){ // ... maybe add a delay(15) to go easy on the i2c bus... ???
 
    while(Wire.available()) {
        number = Wire.read();
        Serial.print("data received: ");
        Serial.println(number);
 
        if (number == 1){
 
            if (state == 0){
                digitalWrite(13, HIGH); // set the LED on
                state = 1;
            }
            else{
                digitalWrite(13, LOW); // set the LED off
                state = 0;
            }
         }
     }
}
 
// callback for sending data
void sendData(){
    Wire.write(number);
}

so any help for Mono/C# code will be appreciated!
Thanks in advance!

Posted in

Make a donation