Muon Detector, Phase 2 and 3 assistance

OK, Made a Muon Detector, no nanos needed. Muon Detector It works. Flashes an LED when muon detected. Can't sit there and constantly watch for the LED to blink. Need to add a counter. Before that, I needed to see if I could get a clock (via DS3231) working to keep time. With Forum help I was able to do that. Basic clock

Now that I have progressed a bit need help with the final phase- putting everything together so the OLED will display the current time and running counts of muons detected. I got a sketch from AI (I'm not able to create by myself...yet) and it does a basic count but not the way I want it. Long story so made a vid to try to explain: Muon combining everything help

Here is AI generated sketch for counter shown in the video:
OLED SDA pin to A4 on Arduino Nano
OLED SCL pin to A5 on Arduino Nano
The input pulse is connected to digital pin 2 (D2) on the Arduino Nano.

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// Constants for OLED display
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

// Constants for input pin and interrupt
#define INPUT_PIN 2
volatile int pulseCount = 0;

void setup() {
  // Initialize OLED display
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);

  // Initialize input pin
  pinMode(INPUT_PIN, INPUT);
  attachInterrupt(digitalPinToInterrupt(INPUT_PIN), countPulse, RISING);
}

void loop() {
  // Display the pulse count on OLED
  display.clearDisplay();
  display.setCursor(0, 0);
  display.print("Muon Count:");
  display.setCursor(0, 24);
  display.print(pulseCount);
  display.display();
}

void countPulse() {
  pulseCount++;
}
1 Like

Forgive me, but I do not want to watch a 10 minute video to see what you want. Can you please post your requirements here in text?

Since your pulseCount is a multi-byte variable you should have an interrupt guard when you read it. Disable interrupts, copy the value of pulseCount to a variable and re-enable interrupts. Then work with the copy.

  noInterrupts();
  int copyOfPulseCount = pulseCount;
  interrupts();

If an interrupt occurs in the middle of reading a multi-byte variable the returned value can be corrupted.

void loop()
{
   // Display the pulse count on OLED
  display.clearDisplay();
  display.setCursor(0, 0);
  display.print("Muon Count:");
  display.setCursor(0, 24);

  noInterrupts();
  int copyOfPulseCount = pulseCount;
  interrupts();
  
  display.print(copyOfPulseCount);
  display.display();  
}

Amazingly, the code you obtain by this method sometimes works!

Thanks!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.