A equipe do Solucionática apresenta mais um representante da categoria dos sensores eletrônicos. Uma bússola digital, hoje quase onipresente nos smartphones, consegue medir a intensidade e direção do campo magnético por meio de um magnetorresistor. O princípio de funcionamento é esse: um campo magnético aplicado ao sensor faz com que a resistência deste varie proporcionalmente (dentro de certa faixa) com a intensidade do campo aplicado.
Um Electronic Brick foi comprado por menos de R$10,00. Procure em sites de vendas especializados e podem encontrar o mesmo modelo até mais barato. Uma aplicação direta da bússola digital é na implementação de um sistema de navegação autônoma para robôs.
PROCESSING (COMPUTER) SIDE
/**
* Serial Call-Response
* by Tom Igoe.
* Modified by Solucionática on Oct,23th * 2013.
*
* Sends a byte out the serial port, and reads 3 bytes in.
* Sets foregound color, xpos, and ypos of a circle onstage
* using the values returned from the serial port.
* Thanks to Daniel Shiffman and Greg Shakar for the improvements.
*
* Note: This sketch assumes that the device on the other end of the serial
* port is going to send a single byte of value 65 (ASCII A) on startup.
* The sketch waits for that byte, then sends an ASCII A whenever
* it wants more data.
*/
import processing.serial.*;
Serial myPort; // The serial port
int[] serialInArray = new int[3]; // Where we'll put what we receive
int serialCount = 0; // A count of how many bytes we receive
int xpos, ypos,zpos; // Starting position of the ball
boolean firstContact = false;
int inByte;
// Whether we've heard from the microcontroller
void setup() {
//size(256, 256); // Stage size
//noStroke(); // No border on the next thing drawn
frameRate(4);
size(640, 360, P3D);
stroke(204,50,0);
//for(int i=0; i<num; i++) {
// colors[i] = color(255 * (i+1)/num);
// Set the starting position of the ball (middle of the stage)
xpos = width/2;
ypos = height/2;
zpos = height/2;
// Print a list of the serial ports, for debugging purposes:
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[1];
myPort = new Serial(this, portName, 9600);
//lights();
}
void draw() {
background(0,0,26);
//fill(fgcolor);
// Draw the shape
translate (320,180,0);
rotateX(map(xpos,0,255,0,2*PI));
rotateY(map(ypos,0,255,0,2*PI));
rotateZ(map(zpos,0,255,0,2*PI));
box(100);
//ellipse(xpos, ypos, zpos, 20); //efeito interessante
delay(20);
}
void serialEvent(Serial myPort) {
int inByte = myPort.readChar();
// if this is the first byte received, and it's an A,
// clear the serial buffer and note that you've
// had first contact from the microcontroller.
// Otherwise, add the incoming byte to the array:
if (firstContact == false) {
if (inByte == 'A') {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
myPort.write('A'); // ask for more
}
}
else {
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;
// If we have 3 bytes:
if (serialCount > 2 ) {
xpos = serialInArray[0];
ypos =serialInArray[1];
zpos =serialInArray[2];
// print the values (for debugging purposes only):
println(xpos + "\t" + ypos + "\t" + zpos);
// Send a capital A to request new sensor readings:
myPort.write('A');
// Reset serialCount:
serialCount = 0;
}
}
}
WIRING (ARDUINO) SIDE
/*
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
Mofified by Solucionática on: 10/23/13.
*/
#include <Wire.h> //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(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();
establishContact();
}
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
x=constrain(x,-300,300);
y=constrain(y,-300,300);
z=constrain(z,-300,300);
int xx=map(x,-300,300,0,255);
int yy=map(y,-300,300,0,255);
int zz=map(z,-300,300,0,255);
Serial.write(xx);
Serial.write(yy);
Serial.write(zz);
delay(250);
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A'); // send a capital A
delay(300);
}
}
RESULTADOS:
(VIDEO EM BREVE)