arduino Uno/Nano freezes

Hello,

im having some trouble with my code i guess.

Im trying to make tacho, but for some reason my arduino, freezes.
I try same code on Uno and Nano and always the same.

When i connect hall efect sensor to digital pin 2 wont even boot, when i connect it after boot
then code works for up to 10 sec and than freezes.
If i disconect hall effect sensor module and press pins with my fingers numbers are changing.

I have Uno/Nano, 0,96 oled I2c, and hall effect sensor module.

I found most of the code online i add few lines of code to display it on OLED

the code:

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#include <Arduino.h>

#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2

int pin = 2;
unsigned long duration;
int temp;
int points; //number of measuring points

void setup(void){
display.begin(SH1106_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
display.display();
display.clearDisplay();

Serial.begin(9600);
pinMode(pin, INPUT);
digitalWrite (7,HIGH);
display.display();
}
void loop (){
duration = pulseIn(pin,LOW,15000000);
temp=60000000/duration;
points=temp/1;
Serial.println(points);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("RPM ");
display.setCursor(60,0);
display.print(points);
display.display();
display.clearDisplay();
delay(100);

}

I could use some help, i guess something wrong with the code.

Regards,
Alen

Here is your code Auto formatted in the IDE and posted here in code tags

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#include <Arduino.h>





#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2




int pin = 2;
unsigned long duration;
int temp;
int points; //number of measuring points

void setup(void)
{
  display.begin(SH1106_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  display.display();
  display.clearDisplay();
  Serial.begin(9600);
  pinMode(pin, INPUT);
  digitalWrite (7, HIGH);
  display.display();
}
void loop ()
{
  duration = pulseIn(pin, LOW, 15000000);
  temp = 60000000 / duration;
  points = temp / 1;
  Serial.println(points);
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("RPM   ");
  display.setCursor(60, 0);
  display.print(points);
  display.display();
  display.clearDisplay();
  delay(100);
}

Do you see how much easier it is to read and copy ? Please use the same process when posting code in future

What is pin 7 connected to and why do you attempt to set its state to HIGH, bearing in mind that it will default to being an input ? Add a Serial.print() before and after the pulseIn() so that you know whether that is where the code stalls. How is the sensor wired ?

Does a simple “Hello World” program work OK with the display ?

Hello AlenZgb,

How is your UNO powered?

What do you mean by :

(I) press pins with my fingers

Regards,
bidouilleelec

Im new to the arduino IDE, didnt know that i can pust code like this, Im sorry, but it is really easyer to cooy and to view.

As for pin 7 goes, i dont know how did that code end up in there, must be some mistake. Ill correct it.
I have hall sensor module. Its wired 5v, ground and pin D2.

Graphics test sketch preform normal and without problems, no delay and no freezing.

bidouilleelec:
Hello AlenZgb,

How is your UNO powered?

What do you mean by :
Regards,
bidouilleelec

Uno is powered by USB cable from PC.
On the back of the board there are pins from headers, soldered, i put my finger there and numbers are changing. Without hall sensor module D2 pin connected.
Oled is connected 5v,ground,A4,A5 for comunication with resistor for stability.

I hope you understand me, my english isnt that good. Sorry