Help creating display code for my rotations counter

Hi All,

This is my first post. I read the guide, but I apologize if I left anything out. Here's my situation--I'm a graduate student trying to establish mouse running wheels for an experiment. I've researched and found out a lot about code, but I'm still lacking. I found a way to count revolutions, but I've been trying (with no success) to get this readout on my OLED.

Here's what I need:

A simple revolutions counter. I've been using

 pinMode(2,INPUT_PULLUP);
  digitalWrite(2,HIGH);
  attachInterrupt(digitalPinToInterrupt(2), 0, FALLING);  // hall pin on interrupt 0 = pin 2
  digitalWrite(2,HIGH);
  pinMode(2,INPUT_PULLUP);

And this definitely works. Now I need a value to my 128X32. Any help would be greatly appreciated. Similarly, if there is a better way to detect changes in the hall sensor, I'm all ears.

Thanks, and I apologize in advance if this isn't specific enough. I'm happy to add more.

Where's the part of your code that actually does the counting and tries to display it? Should be a simple matter to do a sort of

myDisplay.print(counter);

.... kind of thing.

I have little experience with interrupts, but doesn't this line:

attachInterrupt(digitalPinToInterrupt(2), 0, FALLING);  // hall pin on interrupt 0 = pin 2

... mean there's an ISR function called "0"?

(Can functions have names like that? But let's say they can...)

In the ISR, which aren't supposed / allowed to do much, do you increment a counter?

georgeRyder:
Where's the part of your code that actually does the counting and tries to display it? Should be a simple matter to do a sort of

myDisplay.print(counter);

.... kind of thing.

I have little experience with interrupts, but doesn't this line:

attachInterrupt(digitalPinToInterrupt(2), 0, FALLING);  // hall pin on interrupt 0 = pin 2

... mean there's an ISR function called "0"?

(Can functions have names like that? But let's say they can...)

In the ISR, which aren't supposed / allowed to do much, do you increment a counter?

Hi george,

I suppose that is part of the problem. I'm not quite sure how to code the detection of the magnet by the hall sensor into a running value. The next problem is how to then get that value onto my LED.

I've been able to print out onto the COM3 display that the hall sensor detects the magnet (i followed this tutorial: https://maker.pro/arduino/tutorial/how-to-use-a-hall-effect-sensor-with-arduino).

However, simply "detecting" an event by the sensor is not sufficient for my purposes, I need a running integer readout on the OLED.

Hello bgolson09,
Welcome to the forum. For posting your code correctly on your first post ++Karma.

You don't need interrupts for what you are doing. I created the following code as a general example of reading inputs from buttons (close enough to what you are doing), incrementing a counter and sending the result to the serial monitor. Try it then modify it to your needs.

/* Simple button debounce for 4 buttons. Increments a count and sends the updated count to the serial monitor once per button press */
/* Tested on an Uno */
/* Connect simple push to make buttons between 0V and pin 2, 0V and pin 3, 0V and pin 4 and between 0V and pin 5 */

#define noOfButtons 4     //Exactly what it says; must be the same as the number of elements in buttonPins
#define bounceDelay 20    //Minimum delay before regarding a button as being pressed and debounced
#define minButtonPress 3  //Number of times the button has to be detected as pressed before the press is considered to be valid

const int buttonPins[] = {2, 3, 4, 5};      // Input pins to use, connect buttons between these pins and 0V
unsigned long previousMillis[noOfButtons];  // Timers to time out bounce duation for each button
uint8_t pressCount[noOfButtons];            // Counts the number of times the button is detected as pressed, when this count reaches minButtonPress button is regared as debounced 
uint8_t testCount[noOfButtons];             //Test count, incremented once per button press

void setup() {
  uint8_t i;
  uint32_t baudrate = 115200;
  Serial.begin(baudrate);
  Serial.println("");
  Serial.print("Serial port connected: ");
  Serial.println(baudrate);
  for (i = 0; i < noOfButtons; ++i) {
    pinMode(buttonPins[i], INPUT_PULLUP);
    Serial.print("Testcount ");
    Serial.print(i);
    Serial.print(" = ");
    Serial.println(testCount[i]);
  }
}

void loop() {
  debounce();
  delay(10);     //Your other code goes here instead of this delay. DO NOT leave this delay here, it's only for demonstration.
}

void debounce() {
  uint8_t i;
  unsigned long currentMillis = millis();
  for (i = 0; i < noOfButtons; ++i) {
    if (digitalRead(buttonPins[i])) {             //Input is high, button not pressed or in the middle of bouncing and happens to be high
        previousMillis[i] = currentMillis;        //Set previousMillis to millis to reset timeout
        pressCount[i] = 0;                        //Set the number of times the button has been detected as pressed to 0
      }
    else {
      if (currentMillis - previousMillis[i] > bounceDelay) {
        previousMillis[i] = currentMillis;        //Set previousMillis to millis to reset timeout
        ++pressCount[i];
        if (pressCount[i] == minButtonPress) {
          ++testCount[i];
          Serial.print("Button ");
          Serial.print(i);
          Serial.print(" testcount = ");
          Serial.println (testCount[i]);
        }
      }
    }
  }
}