J-M-L:
Quelle radio?
Quelles librairies ?
Quel code ?
Quelle alimentation ?
Détail des connexions ?
Un micro-pro ça n’existe pas - c’est un Pro Micro—> quelle version (3.3V ou 5V? 8MHz ou 16MHz?) ?
Radio : Emetteur :CHJ-R315A ; Recepteur :CHJ-9921
Librairie : Récepteur :RadioHead, SPI, Mouse ; Emetteur :RadioHead, SPI, Wire
Code : Emetteur :
#include <RH_ASK.h>
#include <SPI.h>
#include <Wire.h>
//<============================== RADIO =================================
RH_ASK rf_driver;
String str_out;
String str_rotX, str_rotY, str_rotZ;
String str_gForceX, str_gForceY, str_gForceZ;
//<======================================================================
//<============================== ACCEL ==================================
long accelX, accelY, accelZ;
float gForceX, gForceY, gForceZ;
long gyroX, gyroY, gyroZ;
float rotX, rotY, rotZ;
//========================================================================>
int mouseClickFlag;
void setup() {
Serial.begin(115200);
Wire.begin();
setupMPU();
if (!rf_driver.init())
Serial.println("init failed");
}
void loop() {
recordAccelRegisters();
recordGyroRegisters();
printData();
sendData();
}
//<=========================== ACCEL ========================== ACCEL =========================== ACCEL ==============================
void setupMPU(){
Wire.beginTransmission(0b1101000); //This is the I2C address of the MPU (b1101000/b1101001 for AC0 low/high datasheet sec. 9.2)
Wire.write(0x6B); //Accessing the register 6B - Power Management (Sec. 4.28)
Wire.write(0b00000000); //Setting SLEEP register to 0. (Required; see Note on p. 9)
Wire.endTransmission();
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x1B); //Accessing the register 1B - Gyroscope Configuration (Sec. 4.4)
Wire.write(0x00000000); //Setting the gyro to full scale +/- 250deg./s
Wire.endTransmission();
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x1C); //Accessing the register 1C - Acccelerometer Configuration (Sec. 4.5)
Wire.write(0b00000000); //Setting the accel to +/- 2g
Wire.endTransmission();
}
void recordAccelRegisters() {
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x3B); //Starting register for Accel Readings
Wire.endTransmission();
Wire.requestFrom(0b1101000,6); //Request Accel Registers (3B - 40)
while(Wire.available() < 6);
accelX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
accelY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
accelZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
processAccelData();
}
void processAccelData(){
gForceX = accelX / 16384.0;
gForceY = accelY / 16384.0;
gForceZ = accelZ / 16384.0;
}
void recordGyroRegisters() {
Wire.beginTransmission(0b1101000); //I2C address of the MPU
Wire.write(0x43); //Starting register for Gyro Readings
Wire.endTransmission();
Wire.requestFrom(0b1101000,6); //Request Gyro Registers (43 - 48)
while(Wire.available() < 6);
gyroX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX
gyroY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY
gyroZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ
processGyroData();
}
void processGyroData() {
rotX = gyroX / 131.0;
rotY = gyroY / 131.0;
rotZ = gyroZ / 131.0;
}
void printData() {
Serial.print("Gyro (deg)");
Serial.print("\t");
Serial.print(" X= ");
Serial.print(rotX);
Serial.print("\t");
Serial.print(" Y= ");
Serial.print(rotY);
Serial.print("\t");
Serial.print(" Z= ");
Serial.print(rotZ);
Serial.print("\t");
Serial.print(" Accel (g)");
Serial.print(" X=");
Serial.print(gForceX);
Serial.print(" Y=");
Serial.print(gForceY);
Serial.print(" Z=");
Serial.println(gForceZ);
}
//===================================================================================================================================>
//<============================================================ RADIO ===============================================================
void sendData() {
str_rotX = String(rotX);
str_rotY = String(rotY);
str_rotZ = String(rotZ);
str_gForceX = String(gForceX);
str_gForceY = String(gForceY);
str_gForceZ = String(gForceZ);
str_rotX.remove(4);
str_rotY.remove(4);
str_rotZ.remove(4);
str_gForceX.remove(4);
str_gForceY.remove(4);
str_gForceZ.remove(4);
str_out = str_rotX + "," + str_rotY + "," + str_rotZ + "," + str_gForceX + "," + str_gForceY + "," + str_gForceZ;
static char *msg = str_out.c_str();
rf_driver.send((uint8_t *)msg, strlen(msg));
rf_driver.waitPacketSent();
Serial.println(str_out);
}
Recepteur:
#include <RH_ASK.h>
#include <SPI.h>
#include <Mouse.h>
//<============================== MOUSE =================================
int vertZero, horiZero; // Stores the initial value of each axis, usually around 512
int vertValue, horzValue; // Stores current analog output of each axis
const int sensitivity = 200; // Higher sensitivity value = slower mouse, should be <= about 500
int mouseClickFlag_R, mouseClickFlag_L;
//=======================================================================>
//<============================== RADIO =================================
String str_out;
String str_rotX, str_rotY, str_rotZ;
String str_gForceX, str_gForceY, str_gForceZ;
RH_ASK rf_driver;
//=======================================================================>
float rotX, rotY, rotZ;
float gForceX, gForceY, gForceZ;
void setup() {
Serial.begin(115200);
if (!rf_driver.init())
Serial.println("init failed");
vertZero = rotX;
horiZero = rotY;
}
void loop() {
dataReception();
mouseProcessing();
}
//====================================== dataReception() =======================================
void dataReception() {
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
if (rf_driver.recv(buf, &buflen))
{
str_out = String((char*)buf);
//rf_driver.printBuffer("Message: ", buf, buflen);
Serial.println(str_out);
str_rotX = str_out.substring(0, 4);
str_rotY = str_out.substring(5, 9);
str_rotZ = str_out.substring(10, 14);
str_gForceX = str_out.substring(15, 19);
str_gForceY = str_out.substring(20, 24);
str_gForceZ = str_out.substring(25, 29);
rotX = str_rotX.toFloat();
rotY = str_rotY.toFloat();
rotZ = str_rotZ.toFloat();
gForceX = str_gForceX.toFloat();
gForceY = str_gForceY.toFloat();
gForceZ = str_gForceZ.toFloat();
Serial.print("Gyro (deg)");
Serial.print("\t");
Serial.print(" X= ");
Serial.print(rotX);
Serial.print("\t");
Serial.print(" Y= ");
Serial.print(rotY);
Serial.print("\t");
Serial.print(" Z= ");
Serial.print(rotZ);
Serial.print("\t");
Serial.print(" Accel (g)");
Serial.print(" X=");
Serial.print(gForceX);
Serial.print(" Y=");
Serial.print(gForceY);
Serial.print(" Z=");
Serial.println(gForceZ);
}
}
//======================================================================================================
//========================================= mouseProcessing() ==========================================
void mouseProcessing() {
gForceZ = str_gForceZ.toFloat();
gForceX = str_gForceX.toFloat();
vertValue = gForceZ; // read vertical offset
horzValue = gForceX; // read horizontal offset
if (vertValue != 0) {
Mouse.move(0, vertValue, 0); // move mouse on y axis
}
if (horzValue != 0) {
Mouse.move(horzValue, 0, 0); // move mouse on x axis
}
if (mouseClickFlag_R == 1) {
Mouse.press(MOUSE_RIGHT);
}
else if (mouseClickFlag_R == 0) {
Mouse.release(MOUSE_RIGHT);
}
if (mouseClickFlag_L == 1) {
Mouse.press(MOUSE_LEFT);
}
else if (mouseClickFlag_L == 0) {
Mouse.release(MOUSE_LEFT);
}
}
Alimentation : 5V
Connexion :
Récepteur Pro Micro
Vcc --> Vcc
DATA --> 16
GND --> GND
Emetteur Nano
Vcc --> Vcc
DATA --> 11
GND --> GND
Pro Micro 5V ; 16MHz