Skip to content

Instantly share code, notes, and snippets.

@vurdalakov
Created May 30, 2022 05:55
Show Gist options
  • Save vurdalakov/07f200cc5c640998e105f5e0bb84c973 to your computer and use it in GitHub Desktop.
Save vurdalakov/07f200cc5c640998e105f5e0bb84c973 to your computer and use it in GitHub Desktop.
How to get CPM value from the RadSens Geiger counter board with RadSensBoard library
#ifndef __COUNTS_PER_MINUTE_H__
#define __COUNTS_PER_MINUTE_H__
class CountsPerMinute
{
int m_currentCpm;
int m_maximumCpm;
int m_intervalsPerMinute;
int* m_intervalCounts;
int m_currentInterval;
int m_initialInterval;
public:
CountsPerMinute() { }
void init(int intervalsPerMinute)
{
this->m_currentCpm = 0;
this->m_maximumCpm = 0;
this->m_intervalsPerMinute = intervalsPerMinute;
this->m_intervalCounts = new int[this->m_intervalsPerMinute];
for (int i = 0; i < this->m_intervalsPerMinute; i++)
{
this->m_intervalCounts[i] = 0;
}
this->m_currentInterval = 0;
this->m_initialInterval = 0;
}
bool isReady() { return this->m_initialInterval >= this->m_intervalsPerMinute; }
int getCurrentCpm() { return this->m_currentCpm; }
int getMaximumCpm() { return this->m_maximumCpm; }
void add(int count)
{
if (this->isReady())
{
this->m_currentCpm = this->m_currentCpm - this->m_intervalCounts[m_currentInterval] + count;
}
else
{
this->m_currentCpm += count;
this->m_initialInterval++;
}
if (this->m_maximumCpm < this->m_currentCpm)
{
this->m_maximumCpm = this->m_currentCpm;
}
this->m_intervalCounts[m_currentInterval] = count;
m_currentInterval++;
if (m_currentInterval >= m_intervalsPerMinute)
{
m_currentInterval = 0;
}
}
};
#endif // __COUNTS_PER_MINUTE_H__
// RadSensBoard library:
// https://github.com/vurdalakov/radsensboard
#include <Arduino.h>
#include <RadSensBoard.h>
#include <CountsPerMinute.h>
#define SECONDS_PER_INTERVAL 5
RadSensBoard radSensBoard;
CountsPerMinute cpm;
void setup()
{
radSensBoard.init();
cpm.init(60 / SECONDS_PER_INTERVAL);
}
void loop()
{
int counts = radSensBoard.getPulseCount();
radSensBoard.resetPulseCount();
cpm.add(counts);
if (cpm.isReady())
{
serial.print("CPM: ");
serial.println(cpm.getCurrentCpm());
serial.print("Max CPM: ");
serial.println(cpm.getMaximumCpm());
}
delay(SECONDS_PER_INTERVAL * 1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment