I'm doing a simple test to understand I2C for a future project. I have a Mega2560 R3 master connected to an Uno R3 slave, where the slave is measuring a potentiometer input and sending the data to my master to control a servo. I can only plug one controller into my computer at a time, so the master has to power the slave. Even with the code uploaded to both controllers, though, it doesn't work. How do I solve this? A photo of the setup and my code are included.
Master Code:
#include <Wire.h>
#include <Servo.h>
Servo myServo;
int angle;
void setup() {
Wire.begin();
myServo.attach(2);
Serial.begin(9600);
}
void loop() {
angle = Wire.read();
Serial.print("angle: ");
Serial.println(angle);
myServo.write(angle);
delay(15);
}
Slave Code:
#include <Wire.h>
int const potPin = A0;
int potVal;
int angle;
void setup() {
Wire.begin(0x08);
Serial.begin(4800);
}
void loop() {
potVal = analogRead(potPin);
Serial.print("potVal: ");
Serial.print(potVal);
angle = map(potVal, 0, 700, 0, 179);
Serial.print(", angle: ");
Serial.println(angle);
Wire.write(angle);
delay(15);
}
My Setup: