// Sinyal girişi PB3 (Attiny pin 2
#include "SSD1306_minimal.h"
SSD1306_Mini oled;
/*
0 .. 9 ve SE logosu için üst sıra ve alt sıra bitmapler
Üst ve alt sırada her bir karakter 8 byte
*/
const byte bitmapUpper1[88] =
{
0, 240, 60, 15, 15, 15, 60, 240,
0, 0, 240, 252, 255, 0, 0, 0,
0, 240, 60, 15, 15, 15, 60, 240,
0, 240, 60, 15, 15, 15, 60, 240,
0, 0, 0, 0, 192, 252, 255, 0,
0, 255, 15, 15, 15, 15, 15, 15,
0, 240, 60, 15, 15, 15, 60, 240,
0, 15, 15, 15, 15, 15, 15, 255,
0, 240, 60, 15, 15, 15, 60, 240,
0, 240, 60, 15, 15, 15, 60, 240,
240, 12, 12, 48, 0, 0, 0, 0,
};
const byte bitmapUpper2[88] =
{
0, 255, 0, 0, 0, 0, 0, 255,
0, 0, 0, 0, 255, 0, 0, 0,
0, 0, 0, 192, 192, 192, 240, 63,
0, 0, 0, 192, 192, 192, 192, 63,
0, 0, 192, 252, 15, 0, 255, 0,
0, 63, 60, 60, 60, 252, 240, 192,
0, 255, 240, 240, 240, 240, 192, 0,
0, 0, 0, 0, 0, 240, 63, 3,
0, 15, 252, 240, 240, 240, 252, 15,
0, 63, 240, 192, 192, 192, 192, 255,
15, 48, 192, 0, 0, 252, 12, 12,
};
const byte bitmapLower1[88] =
{
0, 255, 0, 0, 0, 0, 0, 255,
0, 0, 0, 0, 255, 0, 0, 0,
0, 252, 15, 3, 3, 3, 0, 0,
0, 0, 0, 3, 3, 3, 3, 240,
0, 252, 255, 240, 240, 240, 255, 240,
0, 0, 0, 0, 0, 0, 3, 255,
0, 255, 0, 0, 0, 0, 3, 255,
0, 0, 0, 240, 63, 3, 0, 0,
0, 255, 3, 0, 0, 0, 3, 255,
0, 0, 0, 3, 3, 3, 3, 255,
192, 0, 0, 255, 0, 255, 48, 0,
};
const byte bitmapLower2[88] =
{
0, 15, 60, 240, 240, 240, 60, 15,
0, 0, 240, 240, 255, 240, 240, 0,
0, 255, 240, 240, 240, 240, 240, 240,
0, 15, 60, 240, 240, 240, 60, 63,
0, 0, 0, 0, 0, 0, 255, 0,
0, 63, 240, 240, 240, 252, 63, 15,
0, 15, 60, 240, 240, 240, 60, 15,
0, 0, 0, 255, 0, 0, 0, 0,
0, 15, 60, 240, 240, 240, 60, 15,
0, 15, 60, 240, 240, 240, 60, 15,
0, 3, 3, 0, 0, 255, 192, 192,
};
int b[5];
int rpm[5];
unsigned long y;
long avrg;
int devir;
void setup()
{
pinMode(4, OUTPUT);
oled.init(SlaveAddress);
oled.clear();
oled.sendCommand(0xA4); // Resume to RAM content
oled.sendCommand(0xA6); // Normal display
oled.sendCommand(0xAF); // Normal mode
oled.sendCommand(0x81); // Contrast
oled.sendCommand(0xFF); // Max
oled.sendCommand(0x20); // Memory addressing mode
oled.sendCommand(0x02); // Page addressing
oled.sendCommand(0x00); // Column Address Set Low Nibble
oled.sendCommand(0x10); // Column Address Set High Nibble
oled.sendCommand(0xB0); // Page Address Set
}
void loop()
{
for(int n = 3; n >= 0; n--)
{
rpm[n + 1] = rpm[n];
}
rpm[0] = readrpm();
avrg = 0;
for(int n = 0; n < 5; n++)
{
avrg += rpm[n];
}
avrg = avrg / 5;
devir = avrg;
printoled(devir);
}
int readrpm()
{
int r; // RPM değeri
unsigned long timer;
y = millis();
while (PINB & :coolxf: // LOW going pulse bekleniyor
{
if ((millis() - y) > 100) return 0;
}
y = millis();
while (!(PINB & :coolxf:) // HIGH going pulse bekleniyor
{
if ((millis() - y) > 100) return 0;
}
y = millis();
while (PINB & :coolxf: // LOW going pulse bekleniyor
{
if ((millis() - y) > 100) return 0;
}
while (!(PINB & :coolxf:) // HIGH going pulse bekleniyor
{
if ((millis() - y) > 100) return 0;
}
y = millis() - y;
r = 60000 / y;
return r;
}
void printoled(int p)
{
// int p beş haneli string'e dönüştürülecek
// digit[5] leftmost, digit[0] rightmost
p = constrain(p, 0, 99999);
b[0] = p / 10000;
p = p - b[0] * 10000;
b[1] = p / 1000;
p = p - b[1] * 1000;
b[2] = p / 100;
p = p - b[2] * 100;
b[3] = p / 10;
p = p - b[3] * 10;
b[4] = 0;
if(b[0] == 0) b[0] = 255;
if((b[1] == 0) && (b[0] == 255)) b[1] = 255;
if((b[2] == 0) && (b[1] == 255)) b[2] = 255;
if((b[3] == 0) && (b[2] == 255)) b[3] = 255;
oled.sendCommand(0x0:coolxf:; // Column 0
oled.sendCommand(0x11); // Column 0
oled.sendCommand(0xB0); // Page 0
for (int m = 0; m < 5; m++)
{
Wire.beginTransmission(SlaveAddress);
Wire.send(GOFi2cOLED_Data_Mode); // data mode
for (int i = 0; i < 8; i++)
{
if(b[m] != 255)
{
oled.sendData(bitmapUpper1[8 * b[m] + i]);
oled.sendData(bitmapUpper1[8 * b[m] + i]);
}
else
{
oled.sendData(0);
oled.sendData(0);
}
}
Wire.endTransmission();
}
oled.sendCommand(0x0:coolxf:; // Column 0
oled.sendCommand(0x11); // Column 0
oled.sendCommand(0xB1); // Page 1
for (int m = 0; m < 5; m++)
{
Wire.beginTransmission(SlaveAddress);
Wire.send(GOFi2cOLED_Data_Mode); // data mode
for (int i = 0; i < 8; i++)
{
if(b[m] != 255)
{
oled.sendData(bitmapUpper2[8 * b[m] + i]);
oled.sendData(bitmapUpper2[8 * b[m] + i]);
}
else
{
oled.sendData(0);
oled.sendData(0);
}
}
Wire.endTransmission();
}
oled.sendCommand(0x0:coolxf:; // Column 0
oled.sendCommand(0x11); // Column 0
oled.sendCommand(0xB2); // Page 2
for (int m = 0; m < 5; m++)
{
Wire.beginTransmission(SlaveAddress);
Wire.send(GOFi2cOLED_Data_Mode); // data mode
for (int i = 0; i < 8; i++)
{
if(b[m] != 255)
{
oled.sendData(bitmapLower1[8 * b[m] + i]);
oled.sendData(bitmapLower1[8 * b[m] + i]);
}
else
{
oled.sendData(0);
oled.sendData(0);
}
}
Wire.endTransmission();
}
oled.sendCommand(0x0:coolxf:; // Column 0
oled.sendCommand(0x11); // Column 0
oled.sendCommand(0xB3); // Page 3
for (int m = 0; m < 5; m++)
{
Wire.beginTransmission(SlaveAddress);
Wire.send(GOFi2cOLED_Data_Mode); // data mode
for (int i = 0; i < 8; i++)
{
if(b[m] != 255)
{
oled.sendData(bitmapLower2 [8 * b[m] + i]);
oled.sendData(bitmapLower2[8 * b[m] + i]);
}
else
{
oled.sendData(0);
oled.sendData(0);
}
}
Wire.endTransmission();
}
delay(50);
// Aşağıya da logomuzu yazalım
// oled.cursorTo(0, 90);
oled.sendCommand(0x0:coolxf:; // Column
oled.sendCommand(0x13); // Column
oled.sendCommand(0xB4); // Page 4
Wire.beginTransmission(SlaveAddress);
Wire.send(GOFi2cOLED_Data_Mode); // data mode
for (int i = 0; i < 8; i++)
{
oled.sendData(bitmapUpper1[80 + i]);
oled.sendData(bitmapUpper1[80 + i]);
}
Wire.endTransmission();
oled.sendCommand(0x0:coolxf:; // Column
oled.sendCommand(0x13); // Column
oled.sendCommand(0xB5); // Page 5
Wire.beginTransmission(SlaveAddress);
Wire.send(GOFi2cOLED_Data_Mode); // data mode
for (int i = 0; i < 8; i++)
{
oled.sendData(bitmapUpper2[80 + i]);
oled.sendData(bitmapUpper2[80 + i]);
}
Wire.endTransmission();
oled.sendCommand(0x0:coolxf:; // Column
oled.sendCommand(0x13); // Column
oled.sendCommand(0xB6); // Page 6
Wire.beginTransmission(SlaveAddress);
Wire.send(GOFi2cOLED_Data_Mode); // data mode
for (int i = 0; i < 8; i++)
{
oled.sendData(bitmapLower1[80 + i]);
oled.sendData(bitmapLower1[80 + i]);
}
Wire.endTransmission();
oled.sendCommand(0x0:coolxf:; // Column
oled.sendCommand(0x13); // Column
oled.sendCommand(0xB7); // Page 7
Wire.beginTransmission(SlaveAddress);
Wire.send(GOFi2cOLED_Data_Mode); // data mode
for (int i = 0; i < 8; i++)
{
oled.sendData(bitmapLower2[80 + i]);
oled.sendData(bitmapLower2[80 + i]);
}
Wire.endTransmission();
}