From: Andrea Manni Date: Mon, 15 Jun 2015 16:53:27 +0000 (+0200) Subject: bussola X-Git-Url: http://git.piffa.net/web?p=sketchbook_andrea;a=commitdiff_plain;h=813132c440e17cbf21d359a31f89b8d3a8b2a226 bussola --- diff --git a/hardware/bussola/hmc5883l_i2c/hmc5883l_i2c.ino b/hardware/bussola/hmc5883l_i2c/hmc5883l_i2c.ino new file mode 100644 index 0000000..6b67c58 --- /dev/null +++ b/hardware/bussola/hmc5883l_i2c/hmc5883l_i2c.ino @@ -0,0 +1,75 @@ +/* +An Arduino code example for interfacing with the HMC5883 + +by: Jordan McConnell + SparkFun Electronics + created on: 6/30/11 + license: OSHW 1.0, http://freedomdefined.org/OSHW + +Analog input 4 I2C SDA +Analog input 5 I2C SCL + +Link: https://www.sparkfun.com/tutorials/301 +Datasheet e registri: http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/Magneto/HMC5883L-FDS.pdf +*/ + +#include //I2C Arduino Library + +#define address 0x1E //0011110b, I2C 7bit address of HMC5883 + +void setup(){ + //Initialize Serial and I2C communications + Serial.begin(9600); + Wire.begin(); + + //Put the HMC5883 IC into the correct operating mode + Wire.beginTransmission(address); //open communication with HMC5883 + Wire.write(0x00); //select mode Measurement Configuration Bits + Wire.write(0x70); // 8 -average, 15 Hz default, normal measuremen + Wire.write(0x02); //select mode register + Wire.write(0x00); //continuous measurement mode + Wire.endTransmission(); +} + +void loop(){ + + int x,y,z; //triple axis data + + //Tell the HMC5883 where to begin reading data + Wire.beginTransmission(address); + Wire.write(0x03); //select register 3, X MSB register + Wire.endTransmission(); + + + //Read data from each axis, 2 registers per axis + Wire.requestFrom(address, 6); + if(6<=Wire.available()){ + x = Wire.read()<<8; //X msb + x |= Wire.read(); //X lsb + z = Wire.read()<<8; //Z msb + z |= Wire.read(); //Z lsb + y = Wire.read()<<8; //Y msb + y |= Wire.read(); //Y lsb + } + + //Print out values of each axis + Serial.print("x: "); + Serial.print(x); + Serial.print(" y: "); + Serial.print(y); + Serial.print(" z: "); + Serial.print(z); + // Calcolo cotangente per gradi: https://it.wikipedia.org/wiki/Arcotangente2 + float radianti = atan2(x,y) ; + if (radianti < 0) {// Se y e'antiorario il segno e' inverso + radianti += 2 * PI ; // inverte il segno +} + + float gradi = radianti * 180 / PI; // Conversione in gradi + gradi += 2.56 ; // Correzione per la declinazione magnetica locale + // Modena dovrebbe essere a +2° 34' positivi EST + Serial.print(" gradi: "); + Serial.println(gradi); + + delay(250); +} diff --git a/hardware/bussola/magsensor/magsensor.ino b/hardware/bussola/magsensor/magsensor.ino index a414f7f..d6ac1af 100644 --- a/hardware/bussola/magsensor/magsensor.ino +++ b/hardware/bussola/magsensor/magsensor.ino @@ -76,7 +76,7 @@ void setup(void) displaySensorDetails(); } -void loop(void) +void loop() { /* Get a new sensor event */ sensors_event_t event; diff --git a/multitasking/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino b/multitasking/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino index 2744341..fa55a1b 100644 --- a/multitasking/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino +++ b/multitasking/BlinkWithoutDelay_6_class/BlinkWithoutDelay_6_class.ino @@ -42,8 +42,8 @@ public: }; // Instanziamo i due led dalla classe -Lampeggiatore ledA(13, 1000); -Lampeggiatore ledB(12, 500); +Lampeggiatore ledA(1, 1000); +Lampeggiatore ledB(2, 500); void setup() { } diff --git a/serial/i2c/master_writer_1/master_writer_1.ino b/serial/i2c/master_writer_1/master_writer_1.ino index 4692b52..4af9aaa 100644 --- a/serial/i2c/master_writer_1/master_writer_1.ino +++ b/serial/i2c/master_writer_1/master_writer_1.ino @@ -12,22 +12,15 @@ Invio dati via I2C void setup() { - Wire.begin(); // join i2c bus (address optional for master) + Wire.begin(); // Entra sul canale I2C come master } byte x = 0; void loop() { - // Wire.beginTransmission(4); // transmit to device #4 - // Wire.write("DAto inviato dal master"); // sends five bytes - // - // Wire.endTransmission(); // stop transmitting - // - // - // delay(500); - Wire.beginTransmission(4); // transmit to device #4 - Wire.write("Tora Tora!"); // sends five bytes + Wire.beginTransmission(4); //Trasmette al device #4 + Wire.write("Tora Tora!"); // sends one byte Wire.endTransmission(); // stop transmitting diff --git a/serial/i2c/slave_receiver_1/slave_receiver_1.ino b/serial/i2c/slave_receiver_1/slave_receiver_1.ino index 34040d6..2222c43 100644 --- a/serial/i2c/slave_receiver_1/slave_receiver_1.ino +++ b/serial/i2c/slave_receiver_1/slave_receiver_1.ino @@ -13,9 +13,10 @@ char input ; void setup() { - Wire.begin(4); // join i2c bus with address #4 - Wire.onReceive(receiveEvent); // register event - + Wire.begin(4); // Entra sul canale I2C come slave ID 4 + Wire.onReceive(receiveEvent); // Al verificarsi dell'evento Wire.onReceive + // richiama la funzione receiveEvent() + // Debug seriale Serial.begin(9600); // start serial for output Serial.println("Slave / RX Debug:"); @@ -24,22 +25,21 @@ void setup() void loop() { - delay(100); -// Nel Loop non succede niente, tutta l'azione e nella funzione receiveEvent() +// Nel Loop non succede niente, tutta l'azione e' nella funzione receiveEvent() // Innescata dall'evento Wire.onReceive } -// function that executes whenever data is received from master -// this function is registered as an event, see setup() +// Funzioni void receiveEvent(int howMany) +// Eseguita ogni volta che si riceve dati dal Master { - Serial.print("Lo slave ha ricevuto il seguente messaggio: "); - while( Wire.available()) // loop through all but the last + Serial.print("Lo slave ha ricevuto il seguente messaggio: \""); + while ( Wire.available()) // Scansiona tutti i dati ricevuti { input = Wire.read(); // receive byte as a character Serial.print(input); // print the character } // print the integer - Serial.println(""); + Serial.println("\""); // \ e' l'escape character }