I'm not that good at this. Calculates RPM through Peripheral Speed and Interrupt.
Everything works fine when I show this in the IDE with Serial.print
without OLED. When I connect an SSD1306 OLED I2C, the interrupt stops working. No serial.print
.
If I uncomment the display.display()
, Same problem with display.setRotation(90)
: everything is fine and serial.print
starts working, but of course not the OLED.
In the code I have a void
addressing, visadisplay()
. If I put this addressing in void loop ()
then everything works but then there is a risk of unsynchronization, wrong values. I have also removed visadisplay();
without change.
Help...
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Function for serial display.
// Connections for Nano.
// Hall sensor connection: (Signal out) --- (D2).
// Display connection: (SDA) --- (4A) / (SCL) --- (A5)
volatile unsigned long PulsTid = 0UL;
volatile unsigned long StartTid = 0;
volatile unsigned long SlutTid = 0;
volatile unsigned long CalcTid = 0;
volatile float omkrets = 7.5;
volatile float HastighetPerferi = 0;
volatile unsigned long Rpm = 0;
volatile float Vinkel = 0UL;
void isr() { // interrupt service routine
Vinkel = 0;
StartTid = millis();
CalcTid = SlutTid;
detachInterrupt(0); //detaches the interrupt while calculating
PulsTid = (StartTid - SlutTid);
HastighetPerferi = (omkrets / PulsTid);
SlutTid = StartTid;
Rpm = ((HastighetPerferi / (omkrets / 1000) * 60) / 2); // Calculate speed
visadisplay();
attachInterrupt(0, isr, FALLING); //attaching the interrupt again
}
void visadisplay(void) {
Serial.print("Varvtal: ");
Serial.println(Rpm);
display.setRotation(90);
display.clearDisplay();
display.setTextSize(1); // Text size.
display.setTextColor(SSD1306_WHITE); // White text.
display.setCursor(0, 0); // Start position
display.println("RPM: ");
display.setCursor(40, 10);
display.setTextSize(2);
display.print(Rpm);
display.display();
}
void setup() {
Serial.begin(115200);
attachInterrupt(0, isr, FALLING); //attaching the interrupt
//SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
}
void loop(){
}