From 3850aaca40e99d107a9ea0272c9436610cec40bd Mon Sep 17 00:00:00 2001 From: Raghav <62105787+MythicalCow@users.noreply.github.com> Date: Mon, 2 Aug 2021 16:16:44 -0400 Subject: [PATCH] Add files via upload --- communication/August1_I2C_MasterDuino.ino | 22 ++++ communication/August1_I2C_SlaveDuino.ino | 135 ++++++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 communication/August1_I2C_MasterDuino.ino create mode 100644 communication/August1_I2C_SlaveDuino.ino diff --git a/communication/August1_I2C_MasterDuino.ino b/communication/August1_I2C_MasterDuino.ino new file mode 100644 index 0000000..30864be --- /dev/null +++ b/communication/August1_I2C_MasterDuino.ino @@ -0,0 +1,22 @@ + +#include + +void setup() { + Wire.begin(); // join i2c bus (address optional for master) + Serial.begin(115200); // start serial for output +} + +void loop() { + Wire.requestFrom(8, 26); // request 100 bytes from slave device #8 + byte index = 0; + String data = ""; + while (Wire.available()) { // slave may send less than requested + char c = Wire.read(); // receive a byte as character + //Serial.print(c); + data += c; + } + Serial.println("Rotation Data: " + data); + Serial.println(); + + delay(10); +} diff --git a/communication/August1_I2C_SlaveDuino.ino b/communication/August1_I2C_SlaveDuino.ino new file mode 100644 index 0000000..9f8ebde --- /dev/null +++ b/communication/August1_I2C_SlaveDuino.ino @@ -0,0 +1,135 @@ +#include +#include +#include + +Adafruit_MPU6050 mpu; + +String x = ""; + +volatile int Val; // variable used by the master to sent data to the slave + +void setup() { + Wire.begin(8); // Slave id #8 + Wire.onRequest(requestEvent); // function to run when asking for data + //Wire.onReceive(receiveEvent); // what to do when receiving data + Serial.begin(115200); // serial for displaying data on your screen + + while (!Serial) + delay(10); // will pause Zero, Leonardo, etc until serial console opens + + Serial.println("Adafruit MPU6050 test!"); + + // Try to initialize! + if (!mpu.begin()) { + Serial.println("Failed to find MPU6050 chip"); + while (1) { + delay(10); + } + } + Serial.println("MPU6050 Found!"); + + mpu.setAccelerometerRange(MPU6050_RANGE_8_G); + Serial.print("Accelerometer range set to: "); + switch (mpu.getAccelerometerRange()) { + case MPU6050_RANGE_2_G: + Serial.println("+-2G"); + break; + case MPU6050_RANGE_4_G: + Serial.println("+-4G"); + break; + case MPU6050_RANGE_8_G: + Serial.println("+-8G"); + break; + case MPU6050_RANGE_16_G: + Serial.println("+-16G"); + break; + } + mpu.setGyroRange(MPU6050_RANGE_500_DEG); + Serial.print("Gyro range set to: "); + switch (mpu.getGyroRange()) { + case MPU6050_RANGE_250_DEG: + Serial.println("+- 250 deg/s"); + break; + case MPU6050_RANGE_500_DEG: + Serial.println("+- 500 deg/s"); + break; + case MPU6050_RANGE_1000_DEG: + Serial.println("+- 1000 deg/s"); + break; + case MPU6050_RANGE_2000_DEG: + Serial.println("+- 2000 deg/s"); + break; + } + + mpu.setFilterBandwidth(MPU6050_BAND_21_HZ); + Serial.print("Filter bandwidth set to: "); + switch (mpu.getFilterBandwidth()) { + case MPU6050_BAND_260_HZ: + Serial.println("260 Hz"); + break; + case MPU6050_BAND_184_HZ: + Serial.println("184 Hz"); + break; + case MPU6050_BAND_94_HZ: + Serial.println("94 Hz"); + break; + case MPU6050_BAND_44_HZ: + Serial.println("44 Hz"); + break; + case MPU6050_BAND_21_HZ: + Serial.println("21 Hz"); + break; + case MPU6050_BAND_10_HZ: + Serial.println("10 Hz"); + break; + case MPU6050_BAND_5_HZ: + Serial.println("5 Hz"); + break; + } + + Serial.println(""); +} + +void loop() { + /* Get new sensor events with the readings */ + sensors_event_t a, g, temp; + mpu.getEvent(&a, &g, &temp); + x = ""; + + /* Print out the values */ + + //x += "Acceleration X: "; + /* + x += String(a.acceleration.x, 3); + x += ", Y: "; + x += String(a.acceleration.y, 3); + x += ", Z: "; + x += String(a.acceleration.z, 3); + x += " m/s^2 "; + */ + + x += String(g.gyro.x, 3); + x += " "; + x += String(g.gyro.y, 3); + x += " "; + x += String(g.gyro.z, 3); + x += " rad/s"; + + while(x.length() < 26) + { + x += " "; + } + Serial.println(x.length()); + Serial.println(x); + delay(10); +} + +// function: what to do when asked for data +void requestEvent() { + byte data[26]; + for (byte i=0;i<26;i++) + { + data[i] = (byte)x.charAt(i); + } + Wire.write(data,sizeof(data)); +}