Hello folks, I have the majority of the code written that is needed for my project (reading earth magnetic values utilizing the LIS2MDL module).
Backstory: I'll be using this as a magnetometer for subsurface surveying of our farm land, this device will be attached to the rear of our UTV in an electronically isolated manner to help reduce interference from loose EMF.
in the code I wrote for the ERT (Electronic Resistance Tomography) sensor, I was able to add an if/else statement, I had this wired up to a simple switch that allowed me to turn the sensor "off" when I was turning around outside of my grid coordinates. I'm guessing I've done something wrong; as soon as I activate the switch using the code below, the sensor begins reading and doesn't stop unless I power off the board.
Any help is appreciated!
#include <Adafruit_LIS2MDL.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_LIS2MDL lis2mdl = Adafruit_LIS2MDL(12345);
#define LIS2MDL_CLK 13
#define LIS2MDL_MISO 12
#define LIS2MDL_MOSI 11
#define LIS2MDL_CS 10
const int buttonPin = 1;
const int buttonOut = 3;
int buttonState = 0;
void setup(void) {
pinMode(buttonOut, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(buttonOut, HIGH);
Serial.begin(115200);
while (!Serial)
delay(10);
Serial.println("LIS2MDL Magnetometer Test");
Serial.println("");
lis2mdl.enableAutoRange(true);
if (!lis2mdl.begin()) { // I2C mode
Serial.println("Ooops, no LIS2MDL detected ... Check your wiring!");
while (1) delay(10);
}
lis2mdl.printSensorDetails();
}
void loop(void) {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
sensors_event_t event;
lis2mdl.getEvent(&event);
Serial.print(event.magnetic.z, 15);
Serial.print(" ");
Serial.println("uT");
delay(200);
}
else {
Serial.println ("OPEN");
delay(3000);
}