Stap 6: Firmware
De piëzo-zoemer gonst automatisch bij ~ 1Hz. U kunt de onderstaande code uploaden als u wilt horen de bruisende mogelijkheden van de zoemer.
#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); }
Ik vond deze zoemen om een beetje veel dus ik pulsed power in- en uitschakelen om de zoemer om het een beetje rustig. Zie de onderstaande code:
#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); }