// Define the pin for the hall effect sensor const int hallEffectPin = 31; // Variable to store the count of magnet detections int magnetCount = 0; // Flag to track whether the sensor was already triggered bool sensorTriggered = false; void setup() { // Initialize serial communication Serial.begin(9600); // Set the hall effect sensor pin as input pinMode(hallEffectPin, INPUT); } void loop() { // Read the state of the hall effect sensor int sensorState = digitalRead(hallEffectPin); // If the sensor detects a magnet (LOW state) and it wasn't triggered before if (sensorState == LOW && !sensorTriggered) { // Increment the magnet count magnetCount++; // Print the current magnet count Serial.print("Magnet detected! Count: "); Serial.println(magnetCount); // Set the sensor triggered flag to true sensorTriggered = true; } // If the sensor is not detecting a magnet (HIGH state) // reset the sensor triggered flag to false if (sensorState == HIGH) { sensorTriggered = false; } // // A short delay to prevent rapid counting due to sensor noise // delay(100); }