We are having difficult problems communicating over i2C using the following in our code
Wire.begin(SLAVE_ADDRESS)
and inside of the adafruit_motorshield library there is a Wire.begin(). We believe this is causing conflict between the two. The motorshield would overwrite the settings for returning the communication. We get the result of sending the first two characters of its serial message before crashing.
This will print "da" to the serial not the data received. We can only assume that there is a conflict with iC2 or Wire.begin() being called here and within the communication of the Adafruit_MotorShield.cpp which also calles Wire.begin().
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#define SLAVE_ADDRESS 0x04
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_MotorShield AFMSbot(0x61);
Adafruit_MotorShield AFMStop(0x62);
Adafruit_DCMotor *myMotor = AFMStop.getMotor(1);
Adafruit_DCMotor *myMotor2 = AFMStop.getMotor(2);
Adafruit_DCMotor *myMotor3 = AFMStop.getMotor(3);
Adafruit_DCMotor *myMotor4 = AFMStop.getMotor(4);
Adafruit_DCMotor *myMotor5 = AFMSbot.getMotor(4);
Adafruit_DCMotor *mylight = AFMSbot.getMotor(3);
int incomingByte = 0; // for incoming serial data
int light = 0;
int number = 0;
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Adafruit Motorshield v2 - DC Motor test!");
Wire.pins(4,5)
// initialize i2c as slave
Wire.begin(SLAVE_ADDRESS);
// define callbacks for i2c communication
Wire.onReceive(receiveData);
Wire.onRequest(sendData);
Serial.println(" Ready!");
AFMS.begin(1000); // OR with a different frequency, say 1KHz
AFMSbot.begin(); // Start the bottom shield
AFMStop.begin(); // Start the top shield
}
void loop() {
delay(100);
}
// callback for received data
void receiveData(int byteCount){
while(Wire.available()) {
number = Wire.read();
Serial.print("data received: ");
Serial.println(number);
uint8_t i;
if (Serial.available() == false) {
Serial.println(light);
Serial.println(number);
if (number==1) {
if (light==0) {
Serial.println("light on");
mylight->run(FORWARD);
mylight->setSpeed(255);
light=1;
}else if (light==1) {
Serial.println("light off");
mylight->run(RELEASE);
light=0;
}
}
} else{
Serial.println("serial exists");
}
}
}
// callback for sending data
void sendData(){
Wire.write(number);
}
arm_test.ino (2.54 KB)