Skip to content

Instantly share code, notes, and snippets.

@andfoy
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save andfoy/9a26ced88dcc2ccdad0c to your computer and use it in GitHub Desktop.

Select an option

Save andfoy/9a26ced88dcc2ccdad0c to your computer and use it in GitHub Desktop.
#define NOFIELD 505L // Analog output with no applied field, calibrate this
// Uncomment one of the lines below according to device in use A1301 or A1302
// This is used to convert the analog voltage reading to milliGauss
#define TOMILLIGAUSS 1953L // For A1301: 2.5mV = 1Gauss, and 1024 analog steps = 5V, so 1 step = 1953mG
// #define TOMILLIGAUSS 3756L // For A1302: 1.3mV = 1Gauss, and 1024 analog steps = 5V, so 1 step = 3756mG
int ticks = 0;
unsigned long time_e;
void setup()
{
Serial.begin(9600);
time_e = millis();
}
long DoMeasurement()
{
// measure magnetic field
int raw = analogRead(0); // Range : 0..1024
// Uncomment this to get a raw reading for calibration of no-field point
// Serial.print("Raw reading: ");
// Serial.println(raw);
long compensated = raw - NOFIELD; // adjust relative to no applied field
long gauss = compensated * TOMILLIGAUSS / 1000; // adjust scale to Gauss
//Serial.print(gauss);
//Serial.print(" Gauss ");
//if (gauss > 0) Serial.println("(South pole)");
//else if(gauss < 0) Serial.println("(North pole)");
//else Serial.println();
return gauss;
}
void loop()
{
long measure_1 = DoMeasurement();
delay(100);
long measure_2 = DoMeasurement();
if(measure_1 < 0 && measure_2 > 0)
{
ticks++;
}
Serial.print("Número vueltas: ");
Serial.print(ticks);
Serial.print("Tiempo");
Serial.print(time_e-millis())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment