Now the magnet sensor isn't working, its not detecting when I put a magnet in front
// read the pushbutton input pin:
buttonState = digitalRead(Hall);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
++count;
display.clearDisplay();
display.setTextSize(3); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,25); // Start at top-left corner
display.println(count);
display.display();
} else {
// if the current state is LOW then the button went from on to off:
//Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
if (count % 4 == 0) {
digitalWrite(Led, HIGH);
} else {
digitalWrite(Led, LOW);
}
is not going to work for you if your magnet goes slowly (you'll count way more than needed) in front of the sensor or at more than 10 Hz (you'll miss some counts)
you should really implement a state change detection
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const byte sensorPin = 2; // Arduino PIN D2 to Sensor D0
unsigned long count = 0;
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
Serial.begin(115200);
pinMode(sensorPin, INPUT); // not ncessary, it's INPUT by default
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
}
void loop() {
Serial.println((digitalRead(sensorPin) == LOW) ? F("LOW") : F("HIGH"));
// read the pushbutton input pin:
buttonState = digitalRead(sensorPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
++count;
display.clearDisplay();
display.setTextSize(3); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,25); // Start at top-left corner
display.println(count);
display.display();
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
}
Careful on how often you write a new value to the EEPROM. There is a 100,000 Times limit after which things can go wrong.
It may seem a lot but if you write often, say every second, you’ll kill the memory cells you use in less than 28 hours
A smart way to deal with this is to add a big enough capacitor and a small circuit to detect the power failure and use the power from the capacitor to write to the EEPROM before the complete shutdown