Coding Interrupts on an Adafruit Trinket

Hi there, I’m working on a project that uses a Adafruit Trinket, a hall effect sensor and an OLED screen to do some simple RPM calculations. I’m fairly new to coding, so bare with me.

In plain english, the code should do as follows:

  1. Setup some variables
  2. Use an interrupt pin to look for a rising signal from the Hall effect sensor that calls a function. This function will ++ to two variables, one used for total spins, one to calculate current RPM.
  3. Loop is running that checks if current RPM variable is greater than a value, if so it uses the variable to calculate RPM and displays it, as well as total rotations on the OLED screen.
  4. Program waits for next Hall effect signal.

My issue here lies in the interrupt pin on the Trinket.

According to the pinouts, #0 and #2 are for the SDA/SCL outputs to the OLED. Pin #2 is also the default interrupt for the hall effect sensor.

I’ve tried to shift the interrupt pin to another unused position but the code isnt working. I found some articles that recommend using this

attachInterrupt(digitalPinToInterrupt(3), isr, RISING);

with nothing but errors. I’ve read that the tiny85 might have issues unerstanding “attachInterrupt()” or/and “digitalPinToInterrupt()”.

I also found this article which discusses how to manually pin change interrupts.

This is starting to get a bit over my head, I’d like to know what some folks more knowledgeable than me think. How on earth do I use an interrupt pin and an OLED at the same time???

Here is a copy of my code currently. Note that I’ve suppressed a large amount of it for testing. The Trinket only has about 5.5k Bytes IIRC.

volatile byte half_revolutions;
 int total = 0;
 unsigned int rpm;
 unsigned long timeold;

    //#include <SPI.h>
    //#include <Wire.h>
    //#include <Adafruit_GFX.h>
    //#include <Adafruit_SSD1306.h>

    //#define OLED_RESET 4
    //Adafruit_SSD1306 display(OLED_RESET);


     void setup()
     {
       //Serial.begin(115200);
       //display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)

       //display.display();
       //delay(2000);




     // Clear the buffer.
      //display.clearDisplay();
      //display.setTextSize(2);
      //display.setCursor(0, 0);
      //display.setTextColor(WHITE);



       attachInterrupt(0 , magnet_detect, RISING);
       //attachInterrupt(digitalPinToInterrupt(3), magnet_detect, RISING);//Initialize the intterrupt pin (Arduino digital pin 2)
       half_revolutions = 0;
       rpm = 0;
       timeold = 0;
     }
     void loop()//Measure RPM
     {
       if (half_revolutions >= 13) { 
         rpm = 30*1000/(millis() - timeold)*half_revolutions;

         timeold = millis();
         half_revolutions = 0;
         //Serial.println(rpm,DEC);

         //display.setCursor(0, 0);
         //display.print("RPM: ");
         //display.println(rpm);
         //display.print("Total:");
         //display.println(total);
         //display.display(); //you have to tell the display to...display
         //delay(2000);
         //display.clearDisplay();

       }
     }
     void magnet_detect()//This function is called whenever a magnet/interrupt is detected by the arduino
     {
       total++;
       half_revolutions++;
       //Serial.println("detect");
       //Serial.print(total);

     }

I’m fairly new to coding, so bare with me.

I’m not sure why new programmers always want to take their clothes off, but, if it makes you feel good, go ahead. I’ll keep mine on, though.

I will bear with you, though, as you try to explain your problem.

I’ve tried to shift the interrupt pin to another unused position

With a soldering iron? The external interrupt pins are hardwired to specific positions. You can’t willy-nilly use some other pin.

The Trinket only has about 5.5k Bytes IIRC.

Of what?

The Metro mini is only slightly larger and only slightly more expensive, but it has all the capabilities of a full size Arduino, including separating the external interrupt pins from the I2C pins.

The tiny85 only has a single pin (pin 2) capable of being used with "attachInterrupt()"
You might be able to use a "pin change interrupt" library like GitHub - NicoHood/PinChangeInterrupt: A simple & compact PinChangeInterrupt library for Arduino