Bonjour,
Je voudrais envoyé les valeur de mon gyroscope d'un arduino uno à un nano par le bus i2c mais le problème c'est que je n'arrive pas a envoyé des valeur négative et les valeur positive ne correspondent.
Voici mon code:
Arduino uno
//Librairie
#include "Wire.h"
#include <MPU6050_light.h>
#include <AccelStepper.h>
# define I2C_SLAVE1_ADDRESS 11
# define I2C_SLAVE2_ADDRESS 12
#define PAYLOAD_SIZE 2
//Pins
int MS1= 2 ;
int dirPin= 4;
int stepPin =5;
int enable =6;
int motorInterfaceType= 1;
int b1=8;
int b2=A0;
AccelStepper stepper(motorInterfaceType, stepPin, dirPin);
MPU6050 mpu(Wire);
//Variables
int P1;
float P2;
int b22=0;
void setup() {
Serial.begin(9600);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enable, OUTPUT);
pinMode(b1,INPUT_PULLUP);
pinMode(b2,INPUT_PULLUP);
Wire.begin();
mpu.setAddress(104);
byte status = mpu.begin();
Serial.print(F("MPU6050 status: "));
Serial.println(status);
while(status!=0){ } // stop everything if could not connect to MPU6050
Serial.println(F("Calculating offsets, do not move MPU6050"));
delay(1000);
// mpu.upsideDownMounting = true; // uncomment this line if the MPU6050 is mounted upside-down
mpu.calcOffsets(); // gyro and accelero
Serial.println("Done!\n");
Serial.println(F("-------------------------------------I am the Master"));
digitalWrite(MS1, HIGH);
stepper.setMaxSpeed(400);
}
void loop() {
mpu.update();
//Serial.println( mpu.getAngleX());
b22=(analogRead(b2));
Serial.print(b22);
while (b22 > 200){
mpu.update();
if (digitalRead(b1)==0){
mpu.update();
P1 = mpu.getAngleX();
Serial.print("P1=");
Serial.println(P1);
delay(500);
break;
}
}
while (analogRead(b2) > 200){
mpu.update();
if (digitalRead(b1)==0){
mpu.update();
P2 = mpu.getAngleX();
Serial.print("P2=");
Serial.println(P2);
delay(500);
break;
}
}
if (P1<0){
Wire.beginTransmission(I2C_SLAVE1_ADDRESS);
Wire.write(((char*)&P1)[0] );
Wire.write(((char*)&P1)[0] );
Wire.endTransmission();}
else{
Wire.beginTransmission(I2C_SLAVE1_ADDRESS);
Wire.write(P1);
Wire.endTransmission();}
Arduino nano:
#include <Wire.h>
# define I2C_SLAVE_ADDRESS 11 // 12 pour l'esclave 2 et ainsi de suite
#define PAYLOAD_SIZE 2
void setup()
{
Wire.begin(I2C_SLAVE_ADDRESS);
Serial.begin(9600);
Serial.println("-------------------------------------I am Slave1");
delay(1000);
Wire.onRequest(requestEvents);
Wire.onReceive(receiveEvents);
}
void loop(){}
int n = 0;
void requestEvents()
{
Serial.println(F("---> recieved request"));
Serial.print(F("sending value : "));
Serial.println(n);
Wire.write(n);
}
void receiveEvents(int numBytes)
{
int i;
Serial.println(F("---> recieved events"));
n=Wire.read();
if(numBytes==2){
((char*)&i)[0] = Wire.read(); // Réception du premier octet
((char*)&i)[1] = Wire.read();}
if(numBytes==1){
i=Wire.read();
}
Serial.print(numBytes);
Serial.println(F("bytes recieved"));
Serial.print(F("recieved value : "));
Serial.println(i);
}
Merci d'avance