Skip to content

Instantly share code, notes, and snippets.

@madeintaiwan
Created September 17, 2013 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madeintaiwan/6602412 to your computer and use it in GitHub Desktop.
Save madeintaiwan/6602412 to your computer and use it in GitHub Desktop.
The file that is currently on an Arduino Mega 2560 or Mega ADK with a serial number of 85235333135351C09291
// This code reads a signal from the HNC6352 in degrees and writes it to a SD card. It uses LED flashes to report success to writing and errors
//Necessary libraries
#include <Wire.h>
#include <SD.h>
// Setup the Compass I2C address
int HMC6352SlaveAddress = 0x42;
int HMC6352ReadAddress = 0x41; //"A" in hex, A command is:
int headingValue;
// Set the CS pin for the on the Ethernet Shield, CS is pin 4.
const int chipSelect = 4;
// Define the LED driving pin
int led_13 = 13;
void setup(){
// "The Wire library uses 7 bit addresses throughout.
//If you have a datasheet or sample code that uses 8 bit address,
//you'll want to drop the low bit (i.e. shift the value one bit to the right),
//yielding an address between 0 and 127."
HMC6352SlaveAddress = HMC6352SlaveAddress >> 1; // I know 0x42 is less than 127, but this is still required
// Test the SD card
if (!SD.begin(chipSelect)) {
digitalWrite(led_13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a tenth of a second
digitalWrite(led_13, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for a tenth of a second
digitalWrite(led_13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a tenth of a second
digitalWrite(led_13, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for a tenth of a second
digitalWrite(led_13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a tenth of a second
digitalWrite(led_13, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for a tenth of a second
digitalWrite(led_13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a tenth of a second
digitalWrite(led_13, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for a tenth of a second
digitalWrite(led_13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a tenth of a second
digitalWrite(led_13, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for a tenth of a second
return;
}
Wire.begin();
// initialize the digital pin as an output.
pinMode(led_13, OUTPUT);
}
// Set the inital counter value
int number = 0;
//Begin the main loop
void loop(){
//"Get Data. Compensate and Calculate New Heading"
Wire.beginTransmission(HMC6352SlaveAddress);
Wire.write(HMC6352ReadAddress); // The "Get Data" command
Wire.endTransmission();
//time delays required by HMC6352 upon receipt of the command
//Get Data. Compensate and Calculate New Heading : 6ms
delay(6);
Wire.requestFrom(HMC6352SlaveAddress, 2); //get the two data bytes, MSB and LSB
//"The heading output data will be the value in tenths of degrees
//from zero to 3599 and provided in binary format over the two bytes."
byte MSB = Wire.read();
byte LSB = Wire.read();
int heading= (MSB << 8) + LSB; //(MSB / LSB sum)
// float headingInt = headingSum / 10;
Serial.print(heading);
Serial.println(" degrees");
// make a string for assembling the data to log:
String dataString = "";
dataString += String(number);
dataString += ",";
dataString += String(heading);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
}
// if the file isn't open, pop up an error:
else {
digitalWrite(led_13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a tenth of a second
digitalWrite(led_13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a tenth of a second
}
number++; // add 1 to the counter
// Double flash pin 13
digitalWrite(led_13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a tenth of a second
digitalWrite(led_13, LOW); // turn the LED off by making the voltage LOW
delay(100); // wait for a tenth of a second
digitalWrite(led_13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a tenth of a second
digitalWrite(led_13, LOW); // turn the LED off by making the voltage LOW
delay(700);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment