i2c problem

Hello,

im working on a project and having problem with my loop. I think the problem starts in readFrom(byte address)

#include <Wire.h>


//define values for slip coding
int escapeChar = 101;
int delimiterChar = 100;

void setup()
{
      Wire.begin(); //join i2c bus (address optinal for master)
      Serial.begin(115200); //start serial for output
      
      //turing on the ADXL345
      writeTo(0x2D, 0);
      writeTo(0x2D, 16);
      writeTo(0x2D, 8);
}

//Writes val to address register
void writeTo(byte address, byte val) 
{
      Wire.beginTransmission(0x53); //start transmission to device
      Wire.send(address); //send register to address
      Wire.send(val); //send value to write
      Wire.endTransmission(); //end transmission
}

//reads bytes
void readFrom(byte address)
{
        Wire.beginTransmission(0x53); //start transmission to device
      Wire.send(address); //sends address to read from
      Wire.endTransmission(); //end transmaission
        Wire.requestFrom(0x53,1); //request bytes from device
      
}
      
void loop()
{
      
      char message = Serial.read();
      if (message == 'r') {
            
            
            slipOut(readFrom(0x32)); //read x lsb
            slipOut(readFrom(0x33)); //read x msb
            slipOut(readFrom(0x34)); //read y lsb
            slipOut(readFrom(0x35)); //read y msb
            slipOut(readFrom(0x36)); //read z lsb
            slipOut(readFrom(0x37)); //read z msb
            
            Serial.print(delimiterChar, BYTE);
      }
}

void slipOut(byte output) //slipout to max/msp
{
      if ((output==escapeChar)||(output==delimiterChar)) Serial.print(escapeChar, BYTE);
      Serial.print(output, BYTE);      
}

Thanks for any help!

Best

It would probably be helpfull if you took the time to actually tell us what your problem is and how it manifests itself.

Hi,

I found it out!

I got two problems:
The first is that I was calling the function slipOut(byte output) on the output of the function readFrom(byte address) but the readFrom function was declared to have no output (void). The second problem is that I was not actually reading the i2c line after requesting the sensor data.

#include <Wire.h>

//define values for slip coding
int escapeChar = 101;
int delimiterChar = 100;

void setup()
{
      Wire.begin(); //join i2c bus (address optional for master)
      Serial.begin(115200); //start serial for output
      
      //turning on the ADXL345
      writeTo(0x2D, 0);
      writeTo(0x2D, 16);
      writeTo(0x2D, 8);
}

//Writes val to address register
void writeTo(byte address, byte val)
{
      Wire.beginTransmission(0x53); //start transmission to device
      Wire.send(address); //send register to address
      Wire.send(val); //send value to write
      Wire.endTransmission(); //end transmission
}

//read bytes
byte readFrom(byte address)
{
      Wire.beginTransmission(0x53); //start transmission to device
      Wire.send(address); //sends address to read from
      Wire.endTransmission(); //end transmaission
      Wire.requestFrom(0x53,1); //request bytes from device
      
      byte temp = Wire.receive();
      return temp;
}

void loop()
{
      char message = Serial.read();
      if (message == 'r') {
            slipOut(readFrom(0x32)); //read x lsb
            slipOut(readFrom(0x33)); //read x msb
            slipOut(readFrom(0x34)); //read y lsb
            slipOut(readFrom(0x35)); //read y msb
            slipOut(readFrom(0x36)); //read z lsb
            slipOut(readFrom(0x37)); //read z msb
            
            Serial.print(delimiterChar, BYTE);
      }
}

void slipOut(byte output) //slipout to max/msp
{
      if ((output==escapeChar)||(output==delimiterChar))
            Serial.print(escapeChar, BYTE);
      Serial.print(output, BYTE);
}