#define PCA9548ADDR 0x70 //1110000 #define HMC5883LADDR 0x3D // Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation // is used in I2Cdev.h #include "Wire.h"
// I2Cdev and HMC5883L must be installed as libraries, or else the .cpp/.h files // for both classes must be in the include path of your project #include "I2Cdev.h" #include "HMC5883L.h" #include <SPI.h> #include <SD.h>
// class default I2C address is 0x1E // specific I2C addresses may be passed as a parameter here // this device only supports one I2C address (0x1E) HMC5883L mag;
int16_t mx1, my1, mz1 ;
int16_t mx2, my2, mz2 ;
int16_t mx3, my3, mz3 ; int i=0; int switchPin = 4;
#define LED_PIN 13 bool blinkState = false; const int chipSelect = SS; String dataString = "";
void setup() { Serial.println("1"); pinMode(switchPin, INPUT); // join I2C bus (I2Cdev library doesn't do this automatically) Wire.begin();
// initialize serial communication // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but // it's really up to you depending on your project) Serial.begin(38400); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } {
// initialize device Serial.println("Initializing I2C devices...");
// verify connection Serial.println("Testing device connections..."); Serial.println(mag.testConnection() ? "HMC5883L connection successful" : "HMC5883L connection failed"); Serial.println("0Deg+North : 90Deg+East : 180Deg+South : 270Deg+West"); // configure Arduino LED for pinMode(LED_PIN, OUTPUT); !SD.begin(SS);
} }
void loop(){
if (digitalRead(switchPin) == HIGH) { MAG1(); MAG2(); MAG3();
} } void selectI2CChannels(int channels) { Wire.beginTransmission(PCA9548ADDR); Wire.write(channels); Wire.endTransmission(); } void MAG1 () { selectI2CChannels(0x01); mag.initialize(); // read raw heading measurements from device mag.getHeading(&mx1, &my1, &mz1);
// display tab-separated gyro x/y/z values Serial.print("mag1:\t"); Serial.print(mx1); Serial.print("\t"); Serial.print(my1); Serial.print("\t"); Serial.print(mz1); Serial.print("\t"); // To calculate heading in degrees. 0 degree indicates North float heading = atan2(my1, mx1); if(heading < 0) heading += 2 * M_PI; Serial.print("heading1:\t"); Serial.println(heading * 180/M_PI);
// blink LED to indicate activity blinkState = !blinkState; digitalWrite(LED_PIN, blinkState);
delay(875); dataString = "mx1: "; dataString += String(mx1); dataString += " ; my1: "; dataString += String(my1);
dataString += " ; mz1: "; dataString += String(mz1);
dataString += "....";
File dataFile = SD.open("zapis.txt", FILE_WRITE);
// if the file is available, write to it: if (dataFile) { dataFile.println(dataString); dataFile.close(); // print to the serial port too: Serial.println(dataString); } // if the file isn't open, pop up an error: else { Serial.println("error opening zapis.txt"); } } void MAG2 () { selectI2CChannels(0x02); mag.initialize(); // read raw heading measurements from device mag.getHeading(&mx2, &my2, &mz2);
// display tab-separated gyro x/y/z values Serial.print("mag2:\t"); Serial.print(mx2); Serial.print("\t"); Serial.print(my2); Serial.print("\t"); Serial.print(mz2); Serial.print("\t"); // To calculate heading in degrees. 0 degree indicates North float heading = atan2(my2, mx2); if(heading < 0) heading += 2 * M_PI; Serial.print("heading2:\t"); Serial.println(heading * 180/M_PI);
// blink LED to indicate activity blinkState = !blinkState; digitalWrite(LED_PIN, blinkState);
delay(875); dataString = "mx2: "; dataString += String(mx2); dataString += " ; my2: "; dataString += String(my2);
dataString += " ; mz2: "; dataString += String(mz2);
dataString += "....";
File dataFile = SD.open("zapis.txt", FILE_WRITE);
// if the file is available, write to it: if (dataFile) { dataFile.println(dataString); dataFile.close(); // print to the serial port too: Serial.println(dataString); } // if the file isn't open, pop up an error: else { Serial.println("error opening zapis.txt"); } } void MAG3 () { selectI2CChannels(0x03); mag.initialize(); // read raw heading measurements from device mag.getHeading(&mx3, &my3, &mz3);
// display tab-separated gyro x/y/z values Serial.print("mag3:\t"); Serial.print(mx3); Serial.print("\t"); Serial.print(my3); Serial.print("\t"); Serial.print(mz3); Serial.print("\t"); // To calculate heading in degrees. 0 degree indicates North float heading = atan2(my3, mx3); if(heading < 0) heading += 2 * M_PI; Serial.print("heading3:\t"); Serial.println(heading * 180/M_PI);
// blink LED to indicate activity blinkState = !blinkState; digitalWrite(LED_PIN, blinkState);
delay(875); dataString = "mx3: "; dataString += String(mx3); dataString += " ; my3: "; dataString += String(my3);
dataString += " ; mz3: "; dataString += String(mz3);
dataString += "....";
File dataFile = SD.open("zapis.txt", FILE_WRITE);
// if the file is available, write to it: if (dataFile) { dataFile.println(dataString); dataFile.close(); // print to the serial port too: Serial.println(dataString); } // if the file isn't open, pop up an error: else { Serial.println("error opening zapis.txt"); } }
------------------------ [ Dodano po: 2 minutach ]
Wyświetla 3 wartości natężenia ziemskiego pola magnetycznego z trzech czujników, obsługa multiplexera, no i ten nieszczęsny czytnik kart.
|