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:
- Setup some variables
- 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.
- 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.
- 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);
}