Paso 6: Firmware
El zumbador piezo Zumba automáticamente a ~ 1Hz. Puedes subir el código a continuación si desea escuchar el potencial zumbido del zumbador.
#include <Wire.h> #define Addr 0x1E // 7-bit address of HMC5883 compass boolean buzzer = 0; //storage variable for direction int x; int y; int z; //calibrate these int xVal = -185; int yVal = 300; int zVal = -115; int tolerance = 90;//adjust to change sensitivity void setup() { Wire.begin(); Serial.begin(9600); // Set operating mode to continuous Wire.beginTransmission(Addr); Wire.write(byte(0x02)); Wire.write(byte(0x00)); Wire.endTransmission(); pinMode(7,OUTPUT); } void loop() { Serial.print("X="); Serial.print(x); Serial.print(", Y="); Serial.print(y); Serial.print(", Z="); Serial.println(z); // Initiate communications with compass Wire.beginTransmission(Addr); Wire.write(byte(0x03)); // Send request to X MSB register Wire.endTransmission(); 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(); } if (abs(xVal-x)<tolerance){// if (abs(yVal-y)<tolerance){ if (abs(zVal-z)<tolerance){ buzzer = 1; } else{ buzzer = 0; } } else{ buzzer = 0; } } else{ buzzer = 0; } if (buzzer){ digitalWrite(7,HIGH); } else{ digitalWrite(7,LOW); } delay(500); }
Encontré este zumbido a estar un poco mucho pulsada energía enciende y apaga el timbre para aquietar un poco. Vea el código siguiente:
#include <Wire.h> #define Addr 0x1E // 7-bit address of HMC5883 compass boolean buzzer = 0; //storage variable for direction int x; int y; int z; //calibrate these int xVal = -185; int yVal = 300; int zVal = -115; int tolerance = 90;//adjust to change sensitivity void setup() { Wire.begin(); Serial.begin(9600); // Set operating mode to continuous Wire.beginTransmission(Addr); Wire.write(byte(0x02)); Wire.write(byte(0x00)); Wire.endTransmission(); pinMode(7,OUTPUT); } void loop() { Serial.print("X="); Serial.print(x); Serial.print(", Y="); Serial.print(y); Serial.print(", Z="); Serial.println(z); // Initiate communications with compass Wire.beginTransmission(Addr); Wire.write(byte(0x03)); // Send request to X MSB register Wire.endTransmission(); 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(); } if (abs(xVal-x)<tolerance){ if (abs(yVal-y)<tolerance){ if (abs(zVal-z)<tolerance){ buzzer = 1; } else{ buzzer = 0; } } else{ buzzer = 0; } } else{ buzzer = 0; } if (buzzer){ digitalWrite(7,HIGH); delay(100); digitalWrite(7,LOW); } delay(500); }