//https://www.mischianti.org/2021/07/14/ssd1306-oled-display-draw-images-splash-and-animations-2/ LOGO CONVERTER
// state funkció és meghivás switch-able
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNo or NANO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define NUMFLAKES 10 // Number of snowflakes in the animation example
#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16
static const unsigned char PROGMEM logo_bmp[] =
{ 0b00000000, 0b11000000,
0b00000001, 0b11000000,
0b00000001, 0b11000000,
0b00000011, 0b11100000,
0b11110011, 0b11100000,
0b11111110, 0b11111000,
0b01111110, 0b11111111,
0b00110011, 0b10011111,
0b00011111, 0b11111100,
0b00001101, 0b01110000,
0b00011011, 0b10100000,
0b00111111, 0b11100000,
0b00111111, 0b11110000,
0b01111100, 0b11110000,
0b01110000, 0b01110000,
0b00000000, 0b00110000 };
const uint8_t PROGMEM batman[] =
{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfb, 0xff, 0xc1, 0x80, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xff, 0x81, 0xff, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x7f, 0x81, 0xff, 0xe0, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x1f, 0x01, 0xff, 0xf8, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x0c, 0x03, 0xff, 0xfe, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x03, 0xff, 0xff, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x03, 0xff, 0xff, 0xc0, 0x3f, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xfc, 0x06, 0x00, 0x07, 0xff, 0xff, 0xe0, 0x1f, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xf8, 0x0f, 0x00, 0x1f, 0xff, 0xff, 0xf0, 0x0f, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xfc, 0x0f, 0x80, 0x7f, 0xff, 0xff, 0xf8, 0x07, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xfc, 0x07, 0xe3, 0xff, 0xff, 0xff, 0xf8, 0x07, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xfe, 0x03, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x03, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x03, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0x3f, 0xff, 0xff, 0x80, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xc1, 0xff, 0xf0, 0x03, 0xff, 0xff, 0x80, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x81, 0xff, 0xc0, 0x01, 0xff, 0xff, 0x80, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x81, 0xff, 0x80, 0x00, 0x7f, 0xff, 0xc0, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0xff, 0x00, 0x00, 0x3f, 0xff, 0x00, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xfc, 0x03, 0xfe, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x80, 0x07, 0xfc, 0x03, 0xf0, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfe, 0x00, 0x07, 0xfc, 0x0f, 0xf8, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfe, 0x00, 0x07, 0xf8, 0x1f, 0xfc, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfe, 0x00, 0x0f, 0xf8, 0x1f, 0xfe, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfe, 0x00, 0x3f, 0xf0, 0x3f, 0xfe, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfe, 0x07, 0xff, 0xf0, 0x3f, 0xff, 0x00, 0x03, 0xe0, 0x3f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfe, 0x07, 0xff, 0xf0, 0x3f, 0xff, 0x00, 0x3f, 0xe0, 0x3f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfe, 0x07, 0xff, 0xf0, 0x3f, 0xff, 0x00, 0xff, 0xe0, 0x3f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfe, 0x03, 0xff, 0xf0, 0x3f, 0xfe, 0x03, 0xff, 0xe0, 0x3f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfe, 0x00, 0x3f, 0xf8, 0x3f, 0xfe, 0x07, 0xff, 0xe0, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfe, 0x00, 0x0f, 0xf8, 0x1f, 0xfe, 0x07, 0xff, 0xe0, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xfe, 0x00, 0x0f, 0xf8, 0x0f, 0xfc, 0x0f, 0xff, 0xe0, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0x00, 0x07, 0xfc, 0x07, 0xf8, 0x0f, 0xff, 0xe0, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xe0, 0x07, 0xfc, 0x03, 0xe0, 0x1f, 0xff, 0xc0, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0xfe, 0x01, 0xe0, 0x3f, 0xff, 0xc0, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0xff, 0x01, 0xc0, 0x7f, 0xff, 0xc0, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x83, 0xff, 0x81, 0xe0, 0x7f, 0xff, 0x80, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x81, 0xff, 0x83, 0xe0, 0x7f, 0xff, 0x80, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x81, 0xff, 0x03, 0xe0, 0x7f, 0xff, 0x81, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x81, 0xff, 0x03, 0xf0, 0x3f, 0xff, 0x01, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x81, 0xfe, 0x07, 0xf0, 0x3f, 0xfe, 0x01, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xfe, 0x07, 0xf8, 0x1f, 0xfe, 0x03, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xfe, 0x03, 0xfe, 0x0f, 0xf8, 0x1f, 0xfc, 0x03, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xfe, 0x07, 0xfc, 0x0f, 0xfc, 0x0f, 0xf8, 0x07, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xfc, 0x07, 0xe4, 0x0f, 0xfc, 0x0f, 0xf0, 0x0f, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xf8, 0x0f, 0x80, 0x1f, 0xfc, 0x07, 0xf0, 0x0f, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xf8, 0x0f, 0x00, 0x1f, 0xfe, 0x07, 0xc0, 0x1f, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xfc, 0x04, 0x00, 0x3f, 0xfe, 0x03, 0x80, 0x3f, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x3f, 0xff, 0x03, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x7f, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x18, 0x7f, 0xff, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x3f, 0xff, 0xff, 0x80, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0xff, 0xff, 0xff, 0x80, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xf1, 0xff, 0xff, 0xff, 0x80, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
const float BETA2 = 3950; // should match the Beta Coefficient of the thermistor
int fanPin = 11;
float motor_celsius ; // temp
float unit_celsius ; // temp
float higher_temp; // Higher temp
int tempMin = 10; // the temperature to start the fan 0%
int tempMax = 65; // the maximum temperature when fan is at 100%
int fanLCD;
int fanSpeed;
int led = 8;
int error_code = 0 ; // 0 ok // 1 low temp // 2 high temp
void setup() {
Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.drawBitmap(0, 0, batman, 128, 64, WHITE);
display.display();
delay(500);
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_BLACK); // Draw white text
display.setCursor(90,56);
display.print(F("zoliaz"));
display.setCursor(101,5);
display.print(F("V0.1"));
display.display();
delay(1500);
}
void loop() {
temp_check();
switch (error_code){
case 1 :
analogWrite(fanPin, 255); //SAFE!!! Fan Speed MAX
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(10,10);
display.println("Low temp error!");
display.print("Fan speed:");
display.print(fanLCD);
display.println("%");
display.display();
break;
case 2 :
analogWrite(fanPin, 0); //SAFE!!! Fan Speed MAX
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(10,10);
display.println("High temp error!");
display.print("Fan speed:");
display.print(fanLCD);
display.println("%");
display.display();
break;
case 0 :
temp_screen();
if ((motor_celsius <= 20 ) && (unit_celsius <= 20)) {
analogWrite(fanPin, 255);
}
else if ((motor_celsius <= 35 ) && (unit_celsius <= 35)) {
analogWrite(fanPin, 240);
Serial.print("Fan fokozat: 1");
Serial.print(" ___ ");
}
else if ((motor_celsius <= 45 ) && (unit_celsius <= 45)) {
Serial.print("Fan fokozat: 2");
Serial.print(" ___ ");
}
else if ((motor_celsius <= 55 ) && (unit_celsius <= 55)) {
analogWrite(fanPin, 100);
Serial.print("Fan fokozat: 3");
Serial.print(" ___ ");
}
else if ((motor_celsius <= 65 ) && (unit_celsius <= 65)) {
analogWrite(fanPin, 1);
Serial.print("Fan fokozat: 4");
Serial.print(" ___ ");
//testanimate(logo_bmp, LOGO_WIDTH, LOGO_HEIGHT);
}
else { //Only debug, if is "void temp check" min/max "IF" disable (3pcs if )
analogWrite(fanPin, 255);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10,15);
display.print("Temperature outside the regulated range!");
Serial.println("Temperature outside the regulated range!");
delay(2000);
//display.display();
}
break;
default: //if nothing else matches, do the default
Serial.print("BUG error !");
break;
}
delay (1);
}
void temp_screen(void) {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(10,15); // Start at top-left corner
display.print("BLDC temp: ");
display.print(motor_celsius,1);
display.println("C");
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10,30);
display.print("Unit temp: ");
display.print(unit_celsius,1);
display.println("C");
/*
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(30,56);
display.print("higher: ");
display.print(higher_temp,1);
*/
//display.display();
//delay(5);
}
void temp_check(void) {
int NTCmotor_analogValue = analogRead(A0);
motor_celsius = 1 / (log(1 / (1023. / NTCmotor_analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
int NTCunit_analogValue = analogRead(A1);
unit_celsius = 1 / (log(1 / (1023. / NTCunit_analogValue - 1)) / BETA2 + 1.0 / 298.15) - 273.15;
Serial.print("motor temp: ");
Serial.print(motor_celsius);
Serial.print("_________ ");
Serial.print("unit temp: ");
Serial.print(unit_celsius);
Serial.print("_________ ");
Serial.print("error_code: ");
Serial.println(error_code);
if (motor_celsius <= unit_celsius) {
higher_temp = unit_celsius;
}
else {
higher_temp = motor_celsius;
}
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(5,54);
display.print("Debug higher: ");
display.print(higher_temp,1);
display.println("C");
display.display();
fanLCD = map(motor_celsius, tempMin, tempMax, 0, 100);
// ide mehetne egy switch Case of #zoliaz
/*if( (unit_celsius <= tempMin) || (motor_celsius <= tempMin) ) // if temp is lower than minimum temp OKKK
{
//int fanPin = 0; // fan is not spinning
//analogWrite(fanPin, fanPin);
fanLCD=0;
//digitalWrite(fanPin, 1);
error_code= 1;
}
if( ( (unit_celsius >= tempMin) && (motor_celsius >= tempMin) ) && ( (unit_celsius <= tempMax) && (unit_celsius <= tempMax) ) ) // if temperature is higher than minimum temp
{
fanSpeed = motor_celsius;//map(temp, tempMin, tempMax, 0, 100); // the actual speed of fan//map(temp, tempMin, tempMax, 32, 255);
int fanSpeed =1.5*fanSpeed ;
//fanLCD = map(motor_celsius, tempMin, tempMax, 0, 100); // speed of fan to display on LCD100 //arányositás
//display.print(fanLCD);
display.display();
analogWrite(fanPin, fanSpeed); // spin the fan at the fanPin speed
error_code = 0;
}
if((unit_celsius >= tempMax) || (motor_celsius >= tempMax)) // if temp is higher than tempMax OKKK
{
digitalWrite(fanPin, HIGH); // turn on led
error_code = 2;
}
else // else
{
//display.clearDisplay();
//display.display();
digitalWrite(fanPin, HIGH);
}
*/
}
#define XPOS 0 // Indexes into the 'icons' array in function below
#define YPOS 1
#define DELTAY 2
void testanimate(const uint8_t *bitmap, uint8_t w, uint8_t h) {
int8_t f, icons[NUMFLAKES][3];
// Initialize 'snowflake' positions
for(f=0; f< NUMFLAKES; f++) {
icons[f][XPOS] = random(1 - LOGO_WIDTH, display.width());
icons[f][YPOS] = -LOGO_HEIGHT;
icons[f][DELTAY] = random(1, 6);
Serial.print(F("x: "));
Serial.print(icons[f][XPOS], DEC);
Serial.print(F(" y: "));
Serial.print(icons[f][YPOS], DEC);
Serial.print(F(" dy: "));
Serial.println(icons[f][DELTAY], DEC);
}
for(;;) { // Loop forever...
display.clearDisplay(); // Clear the display buffer
// Draw each snowflake:
for(f=0; f< NUMFLAKES; f++) {
display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, SSD1306_WHITE);
}
display.display(); // Show the display buffer on the screen
delay(1); // Pause for 1/10 second SNOWSPEED
// Then update coordinates of each flake...
for(f=0; f< NUMFLAKES; f++) {
icons[f][YPOS] += icons[f][DELTAY];
// If snowflake is off the bottom of the screen...
if (icons[f][YPOS] >= display.height()) {
// Reinitialize to a random position, just off the top
icons[f][XPOS] = random(1 - LOGO_WIDTH, display.width());
icons[f][YPOS] = -LOGO_HEIGHT;
icons[f][DELTAY] = random(1, 6);
}
}
}
}