Ok guys ! Thanks for your help !
This is the code that´s gonna be my Geigercounter one day. (hopefully)
By the way, I am using a Arduino Nano for this project !!!
Code:
#include <Arduino.h>
#include <U8g2lib.h>
#include <I2C_RTC.h>
#include <avr/interrupt.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
static DS3231 RTC;
//Display SSD1309 128X64 OLED
U8G2_SSD1309_128X64_NONAME0_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
//Radioaktiv immage for Startup
#define cross_width 50
#define cross_height 47
static const unsigned char radioaktiv[] PROGMEM = {
0x00, 0x00, 0xF0, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x03,
0x00, 0x00, 0x00, 0xC0, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0x00, 0xF0, 0x0F,
0xC0, 0x3F, 0x00, 0x00, 0x00, 0xFC, 0x01, 0x00, 0xFE, 0x00, 0x00, 0x00,
0x7E, 0x00, 0x00, 0xF0, 0x01, 0x00, 0x00, 0x1F, 0x00, 0x00, 0xE0, 0x03,
0x00, 0x80, 0x07, 0x00, 0x00, 0x80, 0x07, 0x00, 0xC0, 0x03, 0x00, 0x00,
0x00, 0x0F, 0x00, 0xE0, 0x41, 0x00, 0x00, 0x08, 0x1E, 0x00, 0xF0, 0xE0,
0x00, 0x00, 0x1C, 0x3C, 0x00, 0x70, 0xF0, 0x01, 0x00, 0x3E, 0x38, 0x00,
0x38, 0xF8, 0x03, 0x00, 0x7F, 0x70, 0x00, 0x3C, 0xFC, 0x07, 0x80, 0xFF,
0xF0, 0x00, 0x1C, 0xFC, 0x0F, 0xC0, 0xFF, 0xE0, 0x00, 0x1C, 0xFE, 0x1F,
0xE0, 0xFF, 0xE1, 0x00, 0x0E, 0xFE, 0x3F, 0xF0, 0xFF, 0xC1, 0x01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x00, 0x00, 0x00, 0xC0, 0xFF, 0xFF,
0x0F, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x03, 0x00, 0x00, 0x00, 0x00,
0xF8, 0x3F, 0x00, 0x00, 0x00,
};
//Sculls and Bones immage for Startup
#define cross_width 46
#define cross_height 35
static const unsigned char totenkopf01[] PROGMEM = {
0x00, 0x00, 0xFC, 0x1F, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x3F, 0x00, 0x00,
0x00, 0x80, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xC0, 0xFF, 0xFF, 0x01, 0x00,
0x00, 0xE0, 0xFF, 0xFF, 0x03, 0x00, 0x00, 0xF0, 0xFF, 0xFF, 0x07, 0x00,
0x00, 0xF0, 0xFF, 0xFF, 0x07, 0x00, 0x00, 0xF8, 0xFF, 0xFF, 0x0F, 0x00,
0x00, 0xF8, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0xF8, 0xFF, 0xFF, 0x0F, 0x00,
0x00, 0xF8, 0xFF, 0xFF, 0x0F, 0x18, 0x00, 0xF8, 0xF1, 0xC7, 0x0F, 0x3C,
0x00, 0xF8, 0xE0, 0x83, 0x0F, 0x3E, 0x3C, 0x78, 0xE0, 0x03, 0x0F, 0x3E,
0x7E, 0x78, 0xE0, 0x03, 0x0F, 0x1F, 0x7E, 0xF8, 0xE0, 0x83, 0xCF, 0x3F,
0xFF, 0xF1, 0xF1, 0xC7, 0xE7, 0x3F, 0xFF, 0xF3, 0xFF, 0xFF, 0xE7, 0x3F,
0xFF, 0xF3, 0x7F, 0xFF, 0xF7, 0x19, 0xEE, 0xF3, 0x3F, 0xFE, 0x77, 0x00,
0x80, 0xF3, 0x3F, 0xFE, 0x07, 0x00, 0x00, 0xE0, 0x1F, 0xFC, 0x03, 0x00,
0x00, 0xC0, 0xFF, 0xFF, 0x01, 0x00, 0x00, 0x00, 0xFE, 0x3F, 0x00, 0x00,
0x00, 0x00, 0xFC, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x6D, 0x5B, 0x00, 0x00,
0x00, 0x38, 0x6D, 0x5B, 0x0E, 0x00, 0x00, 0x3E, 0xFF, 0x7F, 0x3E, 0x00,
0xDE, 0x3F, 0xFF, 0x7F, 0xFE, 0x1D, 0xFF, 0x3F, 0xFE, 0x3F, 0xFE, 0x3F,
0xFF, 0x0F, 0xFC, 0x1F, 0xF8, 0x3F, 0xFF, 0x01, 0xF8, 0x0F, 0xC0, 0x3F,
0x7E, 0x00, 0xE0, 0x03, 0x00, 0x3F, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x1E,
0x3C, 0x00, 0x00, 0x00, 0x00, 0x1C,
};
//Batterie state immage flatt
#define cross_width 13
#define cross_height 6
static const unsigned char batt_flatt[] PROGMEM = {
0xFE, 0x1F, 0x02, 0x10, 0x03, 0x10, 0x03, 0x10, 0x02, 0x10, 0xFE, 0x1F,
};
//Batterie state immage 90%
#define cross_width 13
#define cross_height 6
static const unsigned char batt_10[] PROGMEM = {
0xFE, 0x1F, 0x02, 0x18, 0x03, 0x18, 0x03, 0x18, 0x02, 0x18, 0xFE, 0x1F,
};
//Batterie state immage 20%
#define cross_width 13
#define cross_height 6
static const unsigned char batt_20[] PROGMEM = {
0xFE, 0x1F, 0x02, 0x1C, 0x03, 0x1C, 0x03, 0x1C, 0x02, 0x1C, 0xFE, 0x1F,
};
//Batterie state immage 30%
#define cross_width 13
#define cross_height 6
static const unsigned char batt_30[] PROGMEM = {
0xFE, 0x1F, 0x02, 0x1E, 0x03, 0x1E, 0x03, 0x1E, 0x02, 0x1E, 0xFE, 0x1F,
};
//Batterie state immage 40%
#define cross_width 13
#define cross_height 6
static const unsigned char batt_40[] PROGMEM = {
0xFE, 0x1F, 0x02, 0x1F, 0x03, 0x1F, 0x03, 0x1F, 0x02, 0x1F, 0xFE, 0x1F,
};
//Batterie state immage 50%
#define cross_width 13
#define cross_height 6
static const unsigned char batt_50[] PROGMEM = {
0xFE, 0x1F, 0x82, 0x1F, 0x83, 0x1F, 0x83, 0x1F, 0x82, 0x1F, 0xFE, 0x1F,
};
//Batterie state immage 60%
#define cross_width 13
#define cross_height 6
static const unsigned char batt_60[] PROGMEM = {
0xFE, 0x1F, 0xC2, 0x1F, 0xC3, 0x1F, 0xC3, 0x1F, 0xC2, 0x1F, 0xFE, 0x1F,
};
//Batterie state immage 70%
#define cross_width 13
#define cross_height 6
static const unsigned char batt_70[] PROGMEM = {
0xFE, 0x1F, 0xE2, 0x1F, 0xE3, 0x1F, 0xE3, 0x1F, 0xE2, 0x1F, 0xFE, 0x1F,
};
//Batterie state immage 80%
#define cross_width 13
#define cross_height 6
static const unsigned char batt_80[] PROGMEM = {
0xFE, 0x1F, 0xF2, 0x1F, 0xF3, 0x1F, 0xF3, 0x1F, 0xF2, 0x1F, 0xFE, 0x1F,
};
//Batterie state immage 90%
#define cross_width 13
#define cross_height 6
static const unsigned char batt_90[] PROGMEM = {
0xFE, 0x1F, 0xFA, 0x1F, 0xFB, 0x1F, 0xFB, 0x1F, 0xFA, 0x1F, 0xFE, 0x1F,
};
//Batterie state fully charged
#define cross_width 13
#define cross_height 6
static const unsigned char batt_full[] PROGMEM = {
0xFE, 0x1F, 0xFE, 0x1F, 0xFF, 0x1F, 0xFF, 0x1F, 0xFE, 0x1F, 0xFE, 0x1F,
};
//Buzzer immage for Buzzer on/off
#define cross_width 7
#define cross_height 11
static const unsigned char buzz[] PROGMEM = {
0x43, 0x45, 0x49, 0x79, 0x11, 0x11, 0x11, 0x79, 0x49, 0x45, 0x43,
};
//Speaker immage for Speaker on/off
#define cross_width 7
#define cross_height 12
static const unsigned char speaker01[] PROGMEM = {
0x03, 0x07, 0x0F, 0x1F, 0x7F, 0x7F, 0x7F, 0x7F, 0x1F, 0x0F, 0x07, 0x03,
};
int long CounterVar = 0;
int hour = 0;
int minute = 0;
int second = 0;
int RTCoff = 0;
const int button1 = 2;
const int button2 = 3;
const int button3 = 4;
const int speaker = 6;
const int buzzer = 7;
const int flashLED = 12;
const int vbatt = A0;
int Analogval = 0;
void setup(void) {
//Timer1 as Counter:
//noInterrupts();
TCCR1A = 0; // Init Timer1A
TCCR1B = 0; // Init Timer1B
TCCR1B |= B00000111; // External Clock on T1 PinB5 (RISING)
TIMSK1 |= (1 << TOIE1);
TCNT1 = 0;
//interrupts(); // enable all interrupts
u8g2.begin(); // runs Display lib.
RTC.begin(); // startup RTC
RTC_check(); // check RTC comunication and Display state on startup
delay(1500);
intro(); // Display intro Logo radioaktiv/totenkopf on starup
delay(2500);
pinMode(button1,INPUT); //button one :
pinMode(button2,INPUT); //button two:
pinMode(button3,INPUT); //button three:
pinMode(speaker, OUTPUT); //Speaker on/off
pinMode(buzzer, OUTPUT); //Buzzer on/off
pinMode(flashLED, OUTPUT); //FlashLed on/off
}
void loop(void) {
ISR(TIMER1_COMPA_vect) //That´s the spot where the Hamster starts to hobble !
{
CounterVar ++ ;
}
if (RTC.isRunning()) { //If RTC state is OK set RTCoff to 1 Else set 2
RTCoff = 1;
} else {
RTCoff = 2;
}
u8g2.clearBuffer(); // clear the internal memory
battery(); //Display Battery immage in right upper corner
time(); //Display the Time on top left
counting(); //Counting pulses of the tube SBM20
u8g2.drawXBMP(107, 8, 7, 11, buzz); // Draw Bitmap
u8g2.drawXBMP(107, 20, 7, 12, speaker01); // Draw Bitmap
u8g2.sendBuffer(); // transfer internal memory to the display
if (digitalRead(button1) == HIGH) { //test for Buttons and test to set Timer 1 to zero
// clockset();
digitalWrite(flashLED, HIGH);
TCNT1 = 0;
}
else {
digitalWrite(flashLED, LOW);
}
}
void RTC_check() { //Check the RTC and show state on Startup
u8g2.clearBuffer(); // clear the internal memory
if (RTC.isRunning()) {
RTCoff = 1;
u8g2.setFont(u8g2_font_6x13B_tf);
u8g2.setCursor(10,15);
u8g2.print("RTC DS_3231 status:");
u8g2.setFont(u8g2_font_9x15B_tf);
u8g2.setCursor(55,40);
u8g2.print("OK!");
}else {
RTCoff = 2;
u8g2.setFont(u8g2_font_6x13B_tf);
u8g2.setCursor(10,15);
u8g2.print("RTC DS_3231 status:");
u8g2.setFont(u8g2_font_9x15B_tf);
u8g2.setCursor(30,40);
u8g2.print("MISSING!");
}
u8g2.sendBuffer(); // transfer internal memory to the display
}
void time() { // Format RTC time for Display: 00:00:00
char stunden[8];
sprintf(stunden,"%02i",RTC.getHours());
char minuten[8];
sprintf(minuten,"%02i",RTC.getMinutes());
char sekunden[8];
sprintf(sekunden,"%02i",RTC.getSeconds());
// make a string : display.println(zeit) !
String zeit = String(stunden) + ":" + String(minuten) + ":" + String(sekunden);
// switch case for the RTC check
switch (RTCoff) {
case 1:
u8g2.setFont(u8g2_font_courB10_tf); // set font
u8g2.setCursor(10,10); //position for text
u8g2.print(zeit); // print (zeit) to Display
break;
case 2:
u8g2.setFont(u8g2_font_6x13B_tf);
u8g2.setCursor(10,10);
u8g2.print("RTC failure!");
break;
}
}
void counting() { //Counting pulses of tube SBM20 by Timer1
char count[8];
sprintf(count,"%02i",TCNT1);
u8g2.setFont(u8g2_font_courB10_tf); // set font
u8g2.setCursor(55,40); //position for text
u8g2.print(count);
u8g2.setFont(u8g2_font_6x13B_tf);
u8g2.setCursor(20,50);
u8g2.print(CounterVar);
}
void intro() { // Intro on Startup
//show radioaktiv symbol on startup for 2.5 seconds
u8g2.clearBuffer(); // clear the internal memory
u8g2.drawXBMP(12, 8, 50, 47, radioaktiv); // Draw Bitmap
u8g2.drawXBMP(70, 14, 46, 35, totenkopf01); // Draw Bitmap
u8g2.sendBuffer(); // transfer internal memory to the display
}
void clockset() { // Test how to set the RTC by buttons
hour = RTC.getHours();
hour = hour +1;
if (hour > 23) {
hour = 0;
}
RTC.setHours(hour);
delay(400);
}
void battery() { //Monitoring Battery state:
Analogval = analogRead(vbatt);
if (Analogval < 100) {
u8g2.drawXBMP(115, 0, 13, 6, batt_flatt); // Draw Bitmap
}
if (Analogval >= 100) {
u8g2.drawXBMP(115, 0, 13, 6, batt_10); // Draw Bitmap
}
if (Analogval >= 200) {
u8g2.drawXBMP(115, 0, 13, 6, batt_20); // Draw Bitmap
}
if (Analogval >= 300) {
u8g2.drawXBMP(115, 0, 13, 6, batt_30); // Draw Bitmap
}
if (Analogval >= 400) {
u8g2.drawXBMP(115, 0, 13, 6, batt_40); // Draw Bitmap
}
if (Analogval >= 500) {
u8g2.drawXBMP(115, 0, 13, 6, batt_50); // Draw Bitmap
}
if (Analogval >= 600) {
u8g2.drawXBMP(115, 0, 13, 6, batt_60); // Draw Bitmap
}
if (Analogval >= 700) {
u8g2.drawXBMP(115, 0, 13, 6, batt_70); // Draw Bitmap
}
if (Analogval >= 800) {
u8g2.drawXBMP(115, 0, 13, 6, batt_80); // Draw Bitmap
}
if (Analogval >= 900) {
u8g2.drawXBMP(115, 0, 13, 6, batt_90); // Draw Bitmap
}
if (Analogval > 1000) {
u8g2.drawXBMP(115, 0, 13, 6, batt_full); // Draw Bitmap
}
}