Accept an I2C write command to a register

I am using an Arduino Uno R3 as an i2c Slave. From my master, I am writing a bit value for storage purposes to update the value on an LCD. If I write to another device on the I2C line, the write is effective. However, on the Arduino itself, it sees the value sent but does not update the register.

I can successfully connect to the I2C slave from my master and can read any register but it spits the default FF. I want to change the value of a register say 0x00.

I am verifying the written statement is unsuccessful from my master with a read of the register in question. I am simply trying to store a value that is read from the master and use that same value in another part of a different program.

// Include the required Wire library for I2C<br>
#include <Wire.h>
int LED = 13;
int x = 0;

void setup()
{
  Wire.begin(9);                // join i2c bus with address #9
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
  Wire.setClock(100000);
}

void loop()
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  while(1 < Wire.available()) // loop through all but the last
  {
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);      // print the integer
//  Wire.write()); 
}

What do I need to add to the above code to declare a register and allow writes to it?

Declare a variable for your register.
Interpret the first byte as a register number. If the number matches your register number then store the second byte in your register variable.

That is the part I'm struggling with, C/C++ is not my normal coding suite so I'm a little unfamiliar with the API. I have modified my code to interpret the first byte as an int, but I'm unfamiliar with how to declare a simulated slave address and write it to that register.

If you want to write more Arduino code then it's the best time to start learning C/C++ fundamentals right now.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.