Skip to main content
2 of 5
added 99 characters in body
Ola A
  • 11
  • 3

Arduino, Problem med interrupt och SSD1306 OLED

I'm not that good at this. Calculates RPM through Peripheral Speed and Interrupt. Everything works fine when I show this in the IDE Serial.print with out OLED. When I connect a SSD1306 OLED I2C, 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 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);

// Funkar fint med seriell visning.
// Anslutningar för Nano.
// Hall sensorn är ansluten: (Signal ut) --- (D2).
// Displayen är ansluten: (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);  //Beräknar varvtalet

  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);             // Textstorlek.
 display.setTextColor(SSD1306_WHITE);// Vit text.
 display.setCursor(0,0);             // Startposition
 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(){
  
}
Ola A
  • 11
  • 3