I am working on a wheel mobile robot with multiple sensors which are IMU, two encoders, four ultrasonic sensors, and using two Arduino: Uno and Mega. The IMU, encoders, and ultrasonic sensors are connected to Arduino Mega while the motor controller with every other sensor attached to the robot is connected to the Arduino-Uno. The Uno is attached to the robot by the manufacturer and nearly all the pins have been used or attached to a function on the robot. To use I2C to communicate these two Arduinos I connect SDL to SDL, SDA to SDA, GND, and VCC. Do I need to connect two separate Arduino USB cables to the Arduino simultaneously? Also, do I need to attach these lines of code from each Arduino before I can further write the codes? Attached below the slave and then the master code
// Include the required Wire library for I2C<br>
#include <Wire.h>
int LED = 13;
int x = 0;
void setup() {
// Define the LED pin as Output
pinMode (LED, OUTPUT);
// Start the I2C Bus as Slave on address 9
Wire.begin(8);
// Attach a function to trigger when something is received.
Wire.onReceive(receiveEvent);
}
void receiveEvent(int bytes) {
x = Wire.read(); // read one character from the I2C
}
void loop() {
//If value received is 0 blink LED for 200 ms
if (x == 0) {
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
delay(200);
}
//If value received is 3 blink LED for 400 ms
if (x == 1) {
digitalWrite(LED, HIGH);
delay(400);
digitalWrite(LED, LOW);
delay(400);
}
if (x == 2){
digitalWrite(LED,HIGH);
}
}
this is the master code
#include <Wire.h>
// Include the required Wire library for I2C<br>#include
int x = 0;
void setup() {
// Start the I2C Bus as Master
Wire.begin();
}
void loop() {
Wire.beginTransmission(9); // transmit to device #9
Wire.write(x); // sends x
Wire.endTransmission(); // stop transmitting
Wire.beginTransmission(8);
Wire.write(x);
Wire.endTransmission();
x++; // Increment x
if (x > 3) x = 0; // `reset x once it gets 6
delay(500);
}