Skip to content

Instantly share code, notes, and snippets.

@aovestdipaperino
Created August 5, 2022 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aovestdipaperino/8ff9af19554f268c6369a4dedc6828ab to your computer and use it in GitHub Desktop.
Save aovestdipaperino/8ff9af19554f268c6369a4dedc6828ab to your computer and use it in GitHub Desktop.
RPM sensor - first draft
#include <Arduino.h>
#include <U8x8lib.h>
#include <Wire.h>
U8X8_SSD1306_128X64_NONAME_HW_I2C u8x8(/* clock=*/PIN_WIRE_SCL, /* data=*/PIN_WIRE_SDA, /* reset=*/U8X8_PIN_NONE); // OLEDs without Reset of the Display
#define REED_SWITCH D10
unsigned long rpms = 0;
void tick()
{
static uint64_t previousTick = 0;
uint64_t currentTick = millis();
// limit to 12K RPMs (just in case the IRQ handler takes more than 5 millis)
if ((currentTick - previousTick) <5) return;
// Compute RPMs in thousands of RPMs.
rpms = 60 / (currentTick - previousTick);
previousTick = currentTick;
}
void setup(void)
{
attachInterrupt(digitalPinToInterrupt(REED_SWITCH), tick, RISING);
pinMode(REED_SWITCH, INPUT_PULLDOWN_SENSE);
u8x8.begin();
u8x8.setFlipMode(1); // set number from 1 to 3, the screen word will rotary 180
u8x8.setFont(u8x8_font_inb33_3x6_r);
}
void loop(void)
{
static unsigned long prevRpms = 1;
if (prevRpms == rpms) return;
u8x8.clearDisplay();
u8x8.setCursor(0, 0);
u8x8.print(rpms, 10);
prevRpms = rpms;
// Convert in better delay form like in tick().
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment