Hardware:
Arduino Uno
SparkFun microSD Shield (https://www.sparkfun.com/products/9802)
MicroMag Magnetometer (https://www.sparkfun.com/products/244)
Both shield and magnetometer above use SPI transmission, so I thought a differing slave select for each slave would allow communication to both in one program.
Raw Code:
#define SCLK 7
//#define MISO 12
//#define MOSI 11
#define SSMAG 4
#define DRDY 3
#define RESET 2
#define SSSD 8
#include <SoftwareSerial.h>
#include <math.h>
#include <SdFat.h>
SdFat sd;
SdFile myFile;
int total = 1;
int mag;
int x = 0;
int y = 0;
int z = 0;
void setup(){
if (!sd.begin(SSSD)) sd.initErrorHalt();
Serial.begin(9600);
pinMode(10, OUTPUT);
pinMode(SSMAG, OUTPUT);
pinMode(SSSD, OUTPUT);
pinMode(RESET, OUTPUT);
pinMode(MOSI, OUTPUT);
pinMode(MISO, INPUT);
pinMode(DRDY, INPUT);
pinMode(SCLK, OUTPUT);
Serial.print("#\tx\ty\tz\t|B|\n");
if (!myFile.open("mag.txt", O_WRITE | O_CREAT | O_AT_END)){
sd.errorHalt("opening mag.txt for write failed");
}
float real = mag*0.03;
myFile.println('#\tB (uT)');
myFile.close();
}
void loop(){
Serial.print(int(total));
Serial.print('\t');
x=readaxis(0);
y=readaxis(1);
z=readaxis(2);
getMag(x,y,z);
writeSD(mag);
total++;
}
void send_bit(int _high){
digitalWrite(MOSI, _high);
delayMicroseconds(300);
digitalWrite(SCLK, HIGH);
delayMicroseconds(300);
digitalWrite(SCLK, LOW);
delayMicroseconds(300);
}
int receive_bit(){
digitalWrite(SCLK, HIGH);
delayMicroseconds(300);
int bit = digitalRead(MISO);
delayMicroseconds(300);
digitalWrite(SCLK, LOW);
delayMicroseconds(300);
return bit;
}
float readaxis(int _axis){
digitalWrite(RESET, LOW);
delayMicroseconds(300);
digitalWrite(RESET, HIGH);
delayMicroseconds(300);
digitalWrite(RESET, LOW);
delayMicroseconds(300) ;
send_bit(LOW);
send_bit(HIGH);
send_bit(HIGH);
send_bit(LOW);
send_bit(LOW);
send_bit(LOW);
//the last two bits select the axis
if (_axis == 0){ //x axis
send_bit(LOW);
send_bit(HIGH);
}
else if (_axis == 1){ //y axis
send_bit(HIGH);
send_bit(LOW);
}
else{ //z axis
send_bit(HIGH);
send_bit(HIGH);
}
while (digitalRead(DRDY) == LOW){
}
long runningtotal = 0;
long sign = receive_bit();
for (int i = 14; i >= 0; i = i - 1){
long thisbit = receive_bit();
thisbit = thisbit << i;
runningtotal = runningtotal | thisbit;
}
if (sign == 1){
runningtotal = runningtotal - 32768;
}
if (_axis == 0){
x = runningtotal;
Serial.print(x,DEC);
Serial.print("\t");
}
else if (_axis == 1){
y = runningtotal;
Serial.print(y,DEC);
Serial.print("\t");
}
else{
z = runningtotal;
Serial.print(z,DEC);
Serial.print("\t");
}
return runningtotal;
}
int getMag(float x, float y, float z){
int perc = 0;
mag = sqrt((x*x)+(y*y)+(z*z));
Serial.println(int(mag));
return mag;
}
int writeSD(int mag){
digitalWrite(SSSD, LOW);
digitalWrite(SSMAG, HIGH);
if (!myFile.open("mag.txt", O_WRITE | O_CREAT | O_AT_END)) {
sd.errorHalt("opening mag.txt for write failed");
}
float real = mag*0.03;
myFile.print(int(total));
myFile.print('\t');
myFile.println(real, DEC);
myFile.close();
digitalWrite(SSSD, HIGH);
digitalWrite(SSMAG, LOW);
}
Functions send_bit, receive_bit & readaxis are used to get magnetometer readings and writeSD used to send data to the microSD card.
When this code is run, these are the results on the Serial, while nothing is sent to the microSD.
# x y z |B|
1
Anyone help? Much appreciated.
(Magnetometer pin layout and .ino file attached)
shield_magno2.ino (3.19 KB)