/*
Why the heart icon? Because I thought it was cool
and I want to use it in the future with some heartbeat sensor.
I needed to keep it somewhere and it was here. ;)
*/
#include <TinyWireM.h>
#include <Tiny4kOLED.h>
const unsigned char img_heart_small[] PROGMEM = {
0x00, 0x00, 0xc0, 0xe0, 0xe0, 0xe0, 0xc0, 0x80, 0x80, 0x80, 0xc0, 0xe0, 0xe0, 0xe0, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00, 0x00, 0x00
};
const unsigned char img_heart_big[] PROGMEM = {
0xe0, 0xf0, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0, 0xe0, 0xe0, 0xe0, 0xf0, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0, 0xe0, 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00
};
const unsigned char img_thermometer[] PROGMEM = {
0x00, 0xfe, 0x03, 0xfe, 0x50,
0x00, 0xff, 0x00, 0xff, 0x55,
0x60, 0x9f, 0x80, 0x9f, 0x65,
};
//================================================================================================
void splash() {
oled.clear();
oled.setCursor(1, 0);
oled.print(F(" ATtiny85 SSD1306"));
oled.setCursor(1, 1);
oled.print(F(" Low Voltage Cutoff"));
oled.setCursor(35, 7);
//oled.print(F("wokwi.com"));
}
//================================================================================================
void heartBeat() {
static char big = 1;
static long startTime = 0;
long currentTime;
// Get current time
currentTime = millis();
// Update if 200ms passed
if ((currentTime - startTime) > 200) {
startTime = currentTime;
big = 1 - big;
if (big) {
oled.bitmap(20, 4, 37, 6, img_heart_big);
} else {
oled.bitmap(20, 4, 37, 6, img_heart_small);
}
}
}
//================================================================================================
void prepareDisplay() {
unsigned int i, k;
unsigned char ch[5];
oled.clear();
oled.begin();
oled.setCursor(1, 0);
oled.print(F("temperature"));
oled.setCursor(75, 0);
oled.print(F("24.0C"));
oled.setCursor(3, 1);
oled.print(F("voltage"));
oled.setCursor(75, 1);
oled.print(F("40.0"));
oled.setCursor(3, 2);
oled.print(F("cutoff"));
oled.setCursor(75, 1);
oled.print(F("40.0"));
//oled.bitmap(105, 4, 110, 7, img_thermometer);
//oled.setCursor(57, 4);
//oled.print(F("24.0C"));
//oled.setCursor(57, 5);
//oled.print(F("40.0%"));
//oled.bitmap(10, 5, 17, 2, img_heart_small);
}
//================================================================================================
int cutoffVoltage = 320;
#define enableVoltage 50
int sensorValue = 1; //initial value to see it has the value 1 or another read value
int sensorValue2 = 1; //initial value to see it has the value 1 or another read value
int sensorValue3 = 1; //initial value to see it has the value 1 or another read value
void setup() {
pinMode(5, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(1, OUTPUT);
oled.begin(128, 32, sizeof(tiny4koled_init_128x64br), tiny4koled_init_128x64br);
// Two fonts are supplied with this library, FONT8X16 and FONT6X8
oled.setFont(FONT6X8);
// To clear all the memory
oled.clear();
oled.on();
splash();
delay(3000);
prepareDisplay();
}
//================================================================================================
void loop() {
static long startTime = 0;
long currentTime;
sensorValue = map(analogRead(0), 953, 115, -24, 80); //Read P5
sensorValue2 = map(analogRead(3), 0, 1023, 0, 500); //Read P5
cutoffVoltage = map(analogRead(2), 0, 1023, 0, 500); //Read P5
// Get current time
currentTime = millis();
// Checks 1 second passed
if ((currentTime - startTime) > 1000) {
if (sensorValue2 > enableVoltage+cutoffVoltage) {
digitalWrite(1, true);
}
if (sensorValue2 < cutoffVoltage) {
digitalWrite(1, false);
}
startTime = currentTime;
// Update temperature
float temperature = sensorValue;
// Set cursor
oled.setCursor(75, 0);
// Print to display
oled.print(temperature, 0);
oled.print(F("c "));
// Update humidity
float humidity = sensorValue2 / 100.00;
// Set cursor
oled.setCursor(75, 1);
// Print to display
oled.print(humidity, 2);
oled.print(F("v "));
//oled.bitmap(105, 4, 110, 7, img_thermometer);
}
float cutoff = cutoffVoltage / 100.00;
// Set cursor
oled.setCursor(75, 2);
// Print to display
oled.print(cutoff) / 100.0;
oled.print(F("v "));
//heartBeat();
}
//================================================================================================