Files
Arduino/ATMEGA_COMPAS_433/ATMEGA_COMPAS_433.ino
Jérôme Delacotte 7b30d6e298 first commit
2025-03-06 11:15:32 +01:00

134 lines
3.3 KiB
C++
Executable File

#include <Wire.h>
#include <RCSwitch.h>
#include <HMC5883L.h>
HMC5883L compass;
RCSwitch mySwitch = RCSwitch();
#define Addr 0x1E // 7-bit address of HMC5883 compass
const unsigned long activation = 111269; // Radiateur
const unsigned long idRad=5883;
const unsigned long desactivation = 962111; // Fin radiateur
const unsigned int delai = 11;
#define DEBUG
void setup() {
#ifdef DEBUG
Serial.begin(9600);
Serial.println("\nCompas radio");
#endif
pinMode(13, OUTPUT);
mySwitch.enableTransmit(9);
//mySwitch.setRepeatTransmit(3);
delay(100); // Power up delay
// Wire.begin();
//
// // Set operating mode to continuous
// Wire.beginTransmission(Addr);
// Wire.write(byte(0x02));
// Wire.write(byte(0x00));
// Wire.endTransmission();
// Initialize Initialize HMC5883L
while (!compass.begin())
{
delay(3000);
for (int i = 1; i<10; i++) {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(20); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(20);
}
}
// Set measurement range
compass.setRange(HMC5883L_RANGE_1_3GA);
// Set measurement mode
compass.setMeasurementMode(HMC5883L_CONTINOUS);
// Set data rate
compass.setDataRate(HMC5883L_DATARATE_30HZ);
}
void loop() {
int x, y, z;
// // Initiate communications with compass
// Wire.beginTransmission(Addr);
// Wire.write(byte(0x03)); // Send request to X MSB register
// Wire.endTransmission();
//qxc
// Wire.requestFrom(Addr, 6); // Request 6 bytes; 2 bytes per axis
// if(Wire.available() <=6) { // If 6 bytes available
// x = Wire.read() << 8 | Wire.read();
// z = Wire.read() << 8 | Wire.read();
// y = Wire.read() << 8 | Wire.read();
// }
Vector mag = compass.readRaw();
// Ranges
//-539:506 / 1045 / 522.5 / OFF : 16.5
//-645:464 / 1109 / 554.5 / OFF : -90.5
//-470:499 / 969 / 484.5 / OFF : 14.5
x = ((mag.XAxis + 16.5) + 522.5) / 10.45;
y = ((mag.YAxis -90.5) + 554.5) / 11.09;
z = ((mag.ZAxis +14.5) + 484.5) / 9.69;
#ifdef DEBUG
// Print raw values
Serial.print("X=");
Serial.print(x);
Serial.print(", Y=");
Serial.print(y);
Serial.print(", Z=");
Serial.println(z);
#endif
// Serial.println(z);
// mySwitch.send(activation, 24);
//delay(delai); //delayMicroseconds
myMessageSend(idRad,1000 + x);
myMessageSend(idRad,2000 + y);
myMessageSend(idRad,3000 + z);
//mySwitch.send(desactivation, 24);
//delay(delai);
// digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
// delay(100); // wait for a second
// digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(10); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(10);
//delay(1000);
}
void myMessageSend(long id, long value) {
#ifdef DEBUG
Serial.print("Send id="); Serial.print(id);
Serial.print(" value="); Serial.println(value);
#endif
// mySwitch.send(id, 24); //"000000000001010100010001");
//delay(delai);
mySwitch.send(value, 24); //"000000000001010100010001");
delay(delai);
//delay(5000);
//delayMicroseconds(TWOTIME*8);
}