I2C Comm between Arduino and Raspberry pi2 and interrupt via digital encoder kit

Hello..

I have some problem about I2C Communication..

Raspberry pi generates one random value and send it to Arduino.

Depends on random value, Arduino controls Boe Bot (Move forward, turn left, turn right).

If collision happens, Arduino sends bigger than 0 values to Raspberry pi. In other case, Arduino sends '0' to Raspberry pi.

Until here, it works fine.

I just added Boe Bot digital Encoder kit for calculating how far boe bot moves.

I used attachInterrupt for counting, but it did not work... because Arduino cannot handle I2C communication and heavy interrupt..

Therefore, I just use pin 11,10 for digitalread and whenever values are changed count is going up.

However, when I add digitalread function, boe bot did not move...

I do not know why....

This is my raspberry pi code (python)

-- coding: utf-8 --

import smbus
import time

for RPI version 1, use "bus = smbus.SMBus(0)"

bus = smbus.SMBus(1)

This is the address we setup in the Arduino Program

address_1 = 0x04#echo

def writeNumber_1(value):
bus.write_byte(address_1, value)

bus.write_byte_data(address, 0, value)

return -1

def readNumber_1():
number = bus.read_byte(address_1)

number = bus.read_byte_data(address, 1)

return number

while True:
var = input("Enter 1 ??9: ")
if not var:
continue

writeNumber_1(var)
print "RPI: Hi Arduino, I sent you ", var

sleep one second

time.sleep(1)

number = readNumber_1()
#number = number.decode("utf-8")
print "Arduino: Hey RPI, I received a digit ", number
print

This is Arduino code
#include <Servo.h>
#include <Wire.h>
#define SLAVE_ADDRESS 0x04

int number=0;
int state=0;
Servo servoLeft;
Servo servoRight;

int Rindex = 0;
int Lindex = 0;
int temp_val_l = 0;
int temp_val_r = 0;
void setup(){
Serial.begin(9600);
// initialize i2c as slave
Wire.begin(SLAVE_ADDRESS);
tone(4,3000,1000);
delay(1000);
pinMode(2,INPUT);
pinMode(3,INPUT);
attachInterrupt(0,rtEncoderInterrupt,CHANGE)
attachInterrupt(1,ltEncoderInterrupt,CHANGE)
servoLeft.attach(13);
servoRight.attach(12);

// define callbacks for i2c communication
Wire.onReceive(receiveData);
Wire.onRequest(sendData);
}

void loop(){
}

void forward(int time){

servoLeft.writeMicroseconds(1700);
servoRight.writeMicroseconds(1300);
//delay(time);
while(Rindex<=16){
}
}
servoLeft.writeMicroseconds(1500);
servoRight.writeMicroseconds(1500);
Rindex = 0;
}

void turnRight(int time){
servoLeft.writeMicroseconds(1300);
servoRight.writeMicroseconds(1300);
//delay(time);
while(Rindex<=16){
}
}
servoLeft.writeMicroseconds(1500);
servoRight.writeMicroseconds(1500);
Rindex = 0;
}

void turnLeft(int time){
servoLeft.writeMicroseconds(1700);
servoRight.writeMicroseconds(1700);
//delay(time);
while(Rindex<=16){
}
}
servoLeft.writeMicroseconds(1500);
servoRight.writeMicroseconds(1500);
Rindex = 0;
}

void backward(int time){
servoLeft.writeMicroseconds(1300);
servoRight.writeMicroseconds(1700);
delay(time);
}

void disableServos(){
servoLeft.detach();
servoRight.detach();
}

// callback for received data
void receiveData(int byteCount){

while(Wire.available()) {
number = Wire.read();
Serial.print("Data received: ");
Serial.println(number);

if (number == 1){
forward(200);
number=0;
}else if(number == 2){
turnLeft(200);
number=0;
}else if(number == 3){
turnRight(200);
number=0;
}else if(number == 4){
backward(600);
number=0;
}else if(number == 5){

}else{
disableServos();
}
}
}

// callback for sending data
void sendData(){
Wire.write(number);
}
void rtEncoderInterrupt()
{
Rindex++;
}
void ltEncoderInterrupt()
{
Lindex++;
}

When Arduino code does not have interrupt, it works fine.

while(Rindex<=16){
}
}
servoLeft.writeMicroseconds(1500);
servoRight.writeMicroseconds(1500);
Rindex = 0;

Instead of above code, if I use delay(time), it works fine.

But when I add interrupt, it did not work.