I’m trying to build an old-school 7-segment display to show speed, based around an Attiny85, a MAX7219, and a choice of magnetic sensor - either an A3144 Hall sensor, or a reed switch.
I built a prototype with a Nano and a reed switch, and it worked. Now I’m trying to rebuild that with an Attiny, and simply can’t get any meaningful reading from either type of sensor on either of the two input pins I have available.
Here’s the circuit diagram:
And here’s the stripped-down dummy code I’m using:
// THIS WORKS WHEN GROUNDING PIN 3 AS A BUTTON
//COUNTS UP TO 1023 (A23) WHEN NO BUTTON ATTACHED
#include "LedControl.h"
LedControl lc=LedControl(0,2,1,1);
int ledRefreshRate=500; //rate in millis to update the screen
int time;
unsigned long delaytime=250;
const int buttonPin = 3;
int buttonPushCounter1 = 0; // counts the button pushes
int buttonState1 = 0; // tracks the button state
int lastButtonState1 = 0; // last state of the button
int buttonState = 0; // Button status
int lastButtonState = LOW; //Button set to LOW
long lastDebounceTime = 0; // Debounce status
long debounceDelay = 50; // Debounce timer
void setup() {
lc.shutdown(0,false);
lc.setIntensity(0,8);
lc.clearDisplay(0);
pinMode(buttonPin, INPUT); // Sets button as input
}
void printNumber(int v) {
int ones;
int tens;
int hundreds;
boolean negative=false;
// if(v < -999 || v > 999)
// return;
if(v<0) {
negative=true;
v=v*-1;
}
ones=v%10;
v=v/10;
tens=v%10;
v=v/10; hundreds=v;
if(negative) {
//print character '-' in the leftmost column
lc.setChar(0,0,'-',false);
}
else {
//print a blank in the sign column
lc.setChar(0,0,' ',false);
}
//Now print the number digit by digit
if (millis() - time >= ledRefreshRate)
{
time = millis();
if (!hundreds == 0) {
lc.setDigit(0,1,(byte)hundreds,false);
}
lc.setDigit(0,2,(byte)tens,false);
lc.setDigit(0,3,(byte)ones,false);
}
}
void loop() {
// All the debound button press stuff
int reading = digitalRead(buttonPin);
buttonState1 = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState1 != lastButtonState1) {
if (buttonState1 == HIGH) {
buttonPushCounter1++;
if (buttonPushCounter1 == 1023) {
buttonPushCounter1 = 1;}
}
else {
}
}
lastButtonState1 = buttonState1;
}
}
lastButtonState = reading;
printNumber(buttonPushCounter1);
}
EXPECTED BEHAVIOUR:
whenever the reed switch or hall sensor detect the presence of a magnet, the display should increase the count (buttonPushCounter1) by 1.
ACTUAL BEHAVIOUR:
Depending on how I connect the reed switch or the Hall sensor, the numbers on the Arduino either count infinitely up to the maximum value on their own, with no input from the sensor, or the arduino STOPS counting the moment the sensor detects a signal.
I seem to be making a fundamental error with either digitalread or analogread (the Hall sensor board supports both) in combination with whatever weirdness the ATTINY85 has on the two pins I have available - PB3 and PB4.
At this point I’m open to any and all suggestions because I’m tearing my hair out. I don’t think I can even get Serial comms on the board because I’m using those pins to communicate with the MAX7219.
For reference, this is the Hall sensor:
And this is the reed switch:





