// Rubisync Sensor Interface for Arduino 0018 (MAD Burster)
// Copyright 2010 Glenn Powers <glennpowers@gmail.com>
// http://meaning.com/wiki/Rubisync
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 3 of the License.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// includes micromag 3-axis magnetic field sensor reading code from:
// http://wiring.org.co/learning/basics/magneticfield.html
// Pin Assignments
int SCLK = 6; // magnetometer pin 1
int MISO = 5; // magnetometer pin 2
int MOSI = 4; // magnetometer pin 3
int SSNOT = 9; // magnetometer pin 4
int DRDY = 8; // magnetometer pin 5
int RESET = 7; // magnetometer pin 6
// variables to store sensor data
int magXval;
int magYval;
int magZval;
int x;
int y;
int z;
unsigned long magXtime;
unsigned long magYtime;
unsigned long magZtime;
void setup()
{
//analogReference(INTERNAL); // ser ADC voltage ref to 3.3 internal
Serial.begin(115200); // setup serial
pinMode(SSNOT, OUTPUT);
pinMode(RESET, OUTPUT);
pinMode(MOSI, OUTPUT);
pinMode(SCLK, OUTPUT);
pinMode(MISO, INPUT);
pinMode(DRDY, INPUT);
digitalWrite(SSNOT, LOW);
}
void loop()
{
magXval = readAxis(1); // read the y-axis magnetic field value
magXtime = micros(); // microseconds since program began, ~70 min epoch
Serial.print("1,"); // Sensor Name
Serial.print(magXval); // Sensor Value
Serial.print(","); // separator
Serial.print(magXtime); // time
Serial.println(""); // print carriage return
magYval = readAxis(2); // read the y-axis magnetic field value
magYtime = micros(); // microseconds since program began, ~70 min epoch
Serial.print("2,"); // Sensor Name
Serial.print(magYval); // Sensor Value
Serial.print(","); // separator
Serial.print(magYtime); // time
Serial.println(""); // print carriage return
magZval = readAxis(3); // read the z-axis magnetic field value
magZtime = micros(); // microseconds since program began, ~70 min epoch
Serial.print("3,"); // Sensor Name
Serial.print(magZval); // Sensor Value
Serial.print(","); // separator
Serial.print(magZtime); // time
Serial .println(""); // print carriage return
}
// specific commands for the magnetic sensor
void sendBit(int bit){
// send the bit on the RISING edge of the clock
digitalWrite(MOSI, bit);
delayMicroseconds(2);
digitalWrite(SCLK, HIGH);
delayMicroseconds(2);
digitalWrite(SCLK, LOW);
delayMicroseconds(2);
}
int receiveBit(){
// receive the data on the FALLING edge of the clock
digitalWrite(SCLK, HIGH);
delayMicroseconds(2);
int bit = digitalRead(MISO);
delayMicroseconds(2);
digitalWrite(SCLK, LOW);
delayMicroseconds(2);
return bit;
}
float readAxis(int axis){
// send eight bits, wait until the data is ready then receive 16 bits
// pulse the reset
digitalWrite(RESET, LOW);
delayMicroseconds(2);
digitalWrite(RESET, HIGH);
delayMicroseconds(2);
digitalWrite(RESET, LOW);
delayMicroseconds(2);
// send the command byte
// set the time to read the magnetic sensors (ASIC period) as /2048
sendBit(LOW);
sendBit(HIGH);
sendBit(HIGH);
sendBit(LOW);
sendBit(LOW);
sendBit(LOW);
// the last two bits select the axis
if (axis == 0){ // x axis
sendBit(LOW);
sendBit(HIGH);
}
else if (axis == 1){ // y axis
sendBit(HIGH);
sendBit(LOW);
}
else{ // z axis
sendBit(HIGH);
sendBit(HIGH);
}
// wait until the DRDY line is high
while(digitalRead(DRDY) == LOW) {
}
long total = 0;
// receive result
// the leftmost bit mark the number as positive or negative
long sign = receiveBit();
// the remaining bits are converted to an integer
for (int i = 14; i >= 0; i = i - 1){
long thisbit = receiveBit();
thisbit = thisbit << i;
total = total | thisbit;
}
if(sign == 1) {
total = total - 32768;
}
// set and return the appropriate variable
if (axis == 0){
x = total;
}
else if (axis == 1){
y = total;
}
else{
z = total;
}
return total;
}
void error(char *str)
{;
Serial.print("error: ");
Serial.println(str);
}