I have an Arduino Uno connected to an Arduino Nano. I already had I2C communication between them, but I then connected a color sensor to the same I2C bus. I pretty much just extended the I2C bus, and so they are connected in parallel. I tested the physical connections with a multi-meter, and they are still good. Although it already worked, I attached the code just in case. Thank you so much for your help!
Master Writer Code:
#include <AFMotor.h>
#include <Servo.h>
#include <Wire.h>
#define DEGREES_CONSTANT 366
#define HELLO_BYTE 0
#define BYE_BYTE 1
AF_Stepper motor(200, 1);
Servo servo;
String readIn="";
void setup() {
Serial.begin(9600);
Serial.println("This is the M&M Master");
motor.setSpeed(10); // 10 rpm
servo.attach(9);
Wire.begin();
}
void singleStep()
{
Serial.println("Single coil steps");
motor.step(100, FORWARD, SINGLE);
motor.step(100, BACKWARD, SINGLE);
}
void doubleStep()
{
Serial.println("Double coil steps");
motor.step(100, FORWARD, DOUBLE);
motor.step(100, BACKWARD, DOUBLE);
}
void interleave()
{
Serial.println("Interleave coil steps");
motor.step(100, FORWARD, INTERLEAVE);
motor.step(100, BACKWARD, INTERLEAVE);
}
void microstep()
{
Serial.println("Micrsostep steps");
motor.step(100, FORWARD, MICROSTEP);
motor.step(100, BACKWARD, MICROSTEP);
}
void servoForward(int deg)
{
servo.write(0);
int t=(deg/90)*DEGREES_CONSTANT;
delay(t);
servo.write(90);
}
void sendI2C(byte b) //WHERE STUFF IS SENT
{
Wire.beginTransmission(2);
Wire.write(b);
Wire.endTransmission();
}
void loop() {
readIn=Serial.readString();
if(readIn=="single"){
singleStep();
} else if(readIn=="double"){
doubleStep();
} else if(readIn=="interleave"){
interleave();
} else if(readIn=="micro"){
microstep();
} else if(readIn=="speed")
{
readIn="";
Serial.println("What speed?");
while(readIn=="")
{
readIn=Serial.readString();
}
int s=readIn.toInt();
Serial.println("Speed Set");
motor.setSpeed(s);
}else if(readIn=="servfrwrd")
{
readIn="";
Serial.println("Degrees?");
while(readIn=="")
{
readIn=Serial.readString();
}
int s=readIn.toInt();
Serial.println("Degrees Set");
servoForward(s);
}else if(readIn=="hello")
{
sendI2C(HELLO_BYTE);
Serial.println("Sent");
}else if(readIn=="bye")
{
sendI2C(BYE_BYTE);
Serial.println("Sent");
}
}
Slave Reader Code:
#include <Wire.h>
void setup() {
Wire.begin(2);
Wire.onReceive(receiveEvent);
Serial.begin(9600);
Serial.println("This is the M&M Slave");
pinMode(7,OUTPUT);
digitalWrite(7,LOW);
}
void loop() {
delay(100);
}
void receiveEvent(int howMany) {
byte x = Wire.read();
Serial.println(x);
switch (x)
{
case 0:
Serial.println("Hello");
break;
case 1:
Serial.println("Bye");
break;
}
}