Hi y'all, a while ago I started to restore an old car and I am trying to add some modern features to the best of my abilities. My car has only one screen and it only shows time. I wanted to display time and temperature and voltage readings from various sensors using an arduino uno with a 128x32 OLED screen. I was able to get only this far. My code was freezing constantly for some reason unknown to me , so I changed it and now it does not refresh the values coming from sensors. I am new to programming as a hobbyist and I would greatly appreciate any help. You can show me new horizons to code better. Cheers!
Mini_Info_Screen:
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Smoothed.h>
#include <virtuabotixRTC.h>
#include <Math.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#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 ic A1
#define anaButon 5
#define upButon 4
#define downButon 3
#define voltajV A0
#define Page_Count 6
int lastPage;
int currentPage = 0;
virtuabotixRTC myRTC(7, 8, 9);
Smoothed<float> voltaj_olcum;
Smoothed<float> ic_olcum;
Smoothed<float> dis_olcum;
float I1;
float D1;
int ic_sic;
int dis_sic;
int t_ic;
float R1 = 10000;
float logR2, R2, T;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
//Tarih ayarları
int saat = 7;
int dakika = 25;
const unsigned char celica[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x0f, 0x80, 0x1f, 0xe0, 0x06, 0x00, 0x03, 0x00, 0x03, 0xe0, 0x00, 0xe0, 0x00, 0x00,
0x00, 0x00, 0x1f, 0x80, 0x1f, 0xe0, 0x06, 0x00, 0x03, 0x00, 0x07, 0xe0, 0x00, 0xe0, 0x00, 0x00,
0x00, 0x00, 0x39, 0x80, 0x18, 0x00, 0x06, 0x00, 0x03, 0x00, 0x0e, 0x60, 0x00, 0xe0, 0x00, 0x00,
0x00, 0x00, 0x70, 0x00, 0x18, 0x00, 0x06, 0x00, 0x03, 0x00, 0x1c, 0x00, 0x01, 0xb0, 0x00, 0x00,
0x00, 0x00, 0x60, 0x00, 0x18, 0x00, 0x06, 0x00, 0x03, 0x00, 0x18, 0x00, 0x01, 0xb0, 0x00, 0x00,
0x00, 0x00, 0xe0, 0x00, 0x18, 0x00, 0x06, 0x00, 0x03, 0x00, 0x38, 0x00, 0x01, 0xb0, 0x00, 0x00,
0x00, 0x00, 0xc0, 0x00, 0x18, 0x00, 0x06, 0x00, 0x03, 0x00, 0x30, 0x00, 0x01, 0x30, 0x00, 0x00,
0x00, 0x00, 0xc0, 0x00, 0x18, 0x00, 0x06, 0x00, 0x03, 0x00, 0x30, 0x00, 0x03, 0x18, 0x00, 0x00,
0x00, 0x00, 0xc0, 0x00, 0x1f, 0xc0, 0x06, 0x00, 0x03, 0x00, 0x30, 0x00, 0x03, 0x18, 0x00, 0x00,
0x00, 0x00, 0xc0, 0x00, 0x1f, 0xc0, 0x06, 0x00, 0x03, 0x00, 0x30, 0x00, 0x03, 0x18, 0x00, 0x00,
0x00, 0x00, 0xc0, 0x00, 0x18, 0x00, 0x06, 0x00, 0x03, 0x00, 0x30, 0x00, 0x06, 0x1c, 0x00, 0x00,
0x00, 0x00, 0xc0, 0x00, 0x18, 0x00, 0x06, 0x00, 0x03, 0x00, 0x30, 0x00, 0x07, 0xfc, 0x00, 0x00,
0x00, 0x00, 0xc0, 0x00, 0x18, 0x00, 0x06, 0x00, 0x03, 0x00, 0x30, 0x00, 0x07, 0xfc, 0x00, 0x00,
0x00, 0x00, 0xe0, 0x00, 0x18, 0x00, 0x06, 0x00, 0x03, 0x00, 0x38, 0x00, 0x06, 0x0c, 0x00, 0x00,
0x00, 0x00, 0x60, 0x00, 0x18, 0x00, 0x06, 0x00, 0x03, 0x00, 0x18, 0x00, 0x0c, 0x0e, 0x00, 0x00,
0x00, 0x00, 0x70, 0x00, 0x18, 0x00, 0x06, 0x00, 0x03, 0x00, 0x1c, 0x00, 0x0c, 0x06, 0x00, 0x00,
0x00, 0x00, 0x38, 0xc0, 0x18, 0x00, 0x06, 0x00, 0x03, 0x00, 0x0e, 0x30, 0x0c, 0x06, 0x00, 0x00,
0x00, 0x00, 0x3f, 0xc0, 0x1f, 0xe0, 0x07, 0xf8, 0x03, 0x00, 0x0f, 0xf0, 0x1c, 0x06, 0x00, 0x00,
0x00, 0x00, 0x0f, 0x80, 0x1f, 0xe0, 0x07, 0xf8, 0x03, 0x00, 0x03, 0xe0, 0x1c, 0x07, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
const unsigned char silouhette[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x03, 0xf0, 0x0e, 0x70, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0xff, 0xf1, 0x82, 0x3e, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0f, 0xff, 0xf1, 0xe1, 0x0f, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x3f, 0xff, 0xf1, 0xf8, 0x80, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0xff, 0xff, 0xf1, 0xfc, 0x60, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x7f, 0xff, 0xf1, 0xf0, 0x7c, 0x00, 0x40, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0x7f, 0xfc, 0x00, 0x00, 0xff, 0xff, 0xff, 0xf0, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xf0, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xe0, 0x80, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1f, 0xf8, 0xff, 0xff, 0xff, 0xf9, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0x00, 0x00, 0x0f, 0xff, 0xf9, 0xff, 0xff, 0xff, 0xf9, 0xff, 0xff, 0xff, 0xfe, 0x08, 0x00,
0x00, 0x00, 0x01, 0xff, 0xff, 0xf9, 0xff, 0xff, 0xff, 0xf9, 0xff, 0xff, 0xff, 0xfe, 0x78, 0x00,
0x00, 0x00, 0x3f, 0xff, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xf8, 0xff, 0xff, 0xff, 0xfe, 0x08, 0x00,
0x00, 0x00, 0x1f, 0xfc, 0x3f, 0xf3, 0xff, 0xff, 0xff, 0xf8, 0xff, 0xff, 0x87, 0xfe, 0x00, 0x00,
0x00, 0x0f, 0x1f, 0xe0, 0x07, 0xf3, 0xff, 0xff, 0xff, 0xf8, 0xff, 0xfc, 0x00, 0xff, 0xf8, 0x00,
0x00, 0x00, 0x1f, 0x80, 0x01, 0xf3, 0xff, 0xff, 0xff, 0xf8, 0xff, 0xf0, 0x00, 0x3f, 0xf8, 0x00,
0x00, 0x20, 0x7f, 0x03, 0xc0, 0xf3, 0xff, 0xff, 0xff, 0xf8, 0xff, 0xe0, 0x78, 0x1f, 0xf8, 0x00,
0x00, 0x0f, 0xfe, 0x0f, 0xf0, 0x73, 0xff, 0xff, 0xff, 0xf8, 0xff, 0xc1, 0xfe, 0x0f, 0xfe, 0x00,
0x00, 0x0f, 0xfe, 0x1f, 0xf8, 0x71, 0xff, 0xff, 0xff, 0xf8, 0xff, 0xc3, 0xff, 0x0f, 0xfe, 0x00,
0x00, 0x4f, 0xfc, 0x3f, 0xfc, 0x31, 0xff, 0xff, 0xff, 0xf8, 0xff, 0xc7, 0xff, 0x87, 0xfe, 0x00,
0x00, 0x3f, 0xfc, 0x7f, 0xfe, 0x39, 0xff, 0xff, 0xff, 0xf8, 0xff, 0x8f, 0xff, 0xc7, 0xfe, 0x00,
0x00, 0x3f, 0xfc, 0x7f, 0xfe, 0x19, 0xff, 0xff, 0xff, 0xf9, 0xff, 0x8f, 0xff, 0xc7, 0x80, 0x00
};
const unsigned char termometre[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00,
0x0b, 0x80, 0x08, 0x00, 0x08, 0x00, 0x0b, 0x80, 0x08, 0x00, 0x08, 0x00, 0x0b, 0x80, 0x08, 0x00,
0x08, 0x00, 0x0b, 0x80, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x1c, 0x00, 0x3e, 0x00, 0x3e, 0x00,
0x3e, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
const unsigned char guc[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x00, 0x04, 0x00,
0x0c, 0x00, 0x0c, 0x00, 0x1c, 0x00, 0x18, 0x00, 0x18, 0x00, 0x3c, 0x00, 0x3f, 0xc0, 0x7f, 0x80,
0x7f, 0x80, 0x7f, 0x00, 0x7f, 0x00, 0x07, 0x00, 0x06, 0x00, 0x06, 0x00, 0x0c, 0x00, 0x0c, 0x00,
0x0c, 0x00, 0x08, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
const unsigned char kabin[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x38, 0x00, 0x3c, 0x00,
0x36, 0x00, 0x93, 0x00, 0x81, 0x80, 0x99, 0x80, 0xb8, 0xc0, 0xe0, 0xc0, 0x60, 0x40, 0x3c, 0x40,
0x3e, 0x40, 0x60, 0x40, 0xe0, 0xc0, 0xf0, 0xc0, 0x98, 0x80, 0x81, 0x80, 0x83, 0x00, 0x06, 0x00,
0x1c, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
int currentMillis = 0;
int screenMillis = 0;
int previousMillis = 0;
int Tc;
void setup() {
pinMode(ic, INPUT);
pinMode(voltajV, INPUT);
pinMode(anaButon, INPUT_PULLUP);
pinMode(upButon, INPUT_PULLUP);
pinMode(downButon, INPUT_PULLUP);
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
display.setTextColor(WHITE, BLACK);
display.setFont();
display.setRotation(0);
voltaj_olcum.begin(SMOOTHED_EXPONENTIAL, 2);
ic_olcum.begin(SMOOTHED_EXPONENTIAL, 2);
dis_olcum.begin(SMOOTHED_EXPONENTIAL, 2);
sensors.begin();
myRTC.setDS1302Time(0, dakika, saat, 0, 0, 0, 0);
//delay(1000);
// lastPage = 2;
display.clearDisplay();
acilis();
}
void loop() {
int ana = digitalRead(anaButon);
int yukari = digitalRead(upButon);
int asagi = digitalRead(downButon);
executeCurrentFunction();
/*
screenMillis = millis();
if (screenMillis - previousMillis >= 3000) {
executeCurrentFunction();
screenMillis = 0;
}
*/
if (ana == LOW) {
currentPage = (currentPage + 1) % Page_Count;
display.clearDisplay();
// executeCurrentFunction();
delay(35);
}
if (yukari == LOW) {
saat = saat + 1;
if (saat > 23)
saat = 0;
Serial.print("yukarı");
delay(35);
}
if (asagi == LOW) {
dakika = dakika + 1;
if (dakika > 59)
dakika = 0;
Serial.print("asagi");
delay(35);
}
delay(50);
}
void executeCurrentFunction() {
switch (currentPage) {
case 0:
zaman();
break;
case 1:
zaman_temp();
break;
case 2:
dis_sicaklik();
break;
case 3:
ic_sicaklik();
break;
case 4:
duble_temp();
break;
case 5:
voltaj();
break;
}
}
void zaman() {
RTCSaat();
//display.drawBitmap(0, 0, saatimge, 11, 32, 1);
display.setCursor(24, 8);
display.setTextSize(3);
if (myRTC.hours <= 9) {
display.print("0");
display.print(myRTC.hours);
} else display.print(myRTC.hours);
display.print(":");
if (myRTC.minutes <= 9) {
display.print("0");
display.print(myRTC.minutes);
} else
display.print(myRTC.minutes);
display.display();
lastPage = 0;
delay(50);
}
void ic_sicaklik() {
temp_ic();
display.clearDisplay();
display.drawBitmap(0, 2, kabin, 11, 32, 1);
display.setCursor(28, 8);
display.setTextSize(3);
display.print(I1, 1);
display.print(" C");
display.display();
//Serial.print(Tc);
Serial.println(I1);
lastPage = 3;
//delay(50);
}
double temp_ic() {
int ic_sic = analogRead(ic);
R2 = R1 * (1023.0 / (float)ic_sic - 1.0);
logR2 = log(R2);
int T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
Tc = T - 273.15;
ic_olcum.add(Tc);
I1 = ic_olcum.get();
}
void dis_sicaklik() {
//temp_dis();
display.drawBitmap(0, 2, termometre, 11, 32, 1);
display.setTextSize(3);
display.setCursor(28, 8);
display.setTextSize(3);
display.print(D1, 1);
display.print(" C");
display.display();
lastPage = 2;
//delay(50);
}
void temp_dis() {
sensors.requestTemperatures();
dis_sic = sensors.getTempCByIndex(0);
dis_olcum.add(dis_sic);
D1 = dis_olcum.get();
}
void voltaj() {
float voltaj_giris = analogRead(voltajV);
float Vcalc = (5.0 / 1024) * voltaj_giris;
Vcalc = Vcalc * 2.995;
voltaj_olcum.add(Vcalc);
float V1 = voltaj_olcum.get();
display.drawBitmap(0, 2, guc, 11, 32, 1);
display.setTextSize(3);
display.setCursor(24, 8);
display.print(V1, 1);
display.setCursor(90, 8);
display.print(" V");
display.display();
lastPage = 5;
//delay(50);
}
void zaman_temp() {
temp_dis();
RTCSaat();
// display.drawBitmap(0, 0, saatimge, 11, 32, 1);
//display.drawBitmap(64, 0, termometre, 11, 32, 1 / 2);
display.setTextSize(2);
display.setCursor(0, 10);
if (myRTC.hours < 10) { display.print("0"); }
display.print(myRTC.hours);
display.print(":");
if (myRTC.minutes < 10) { display.print("0"); }
display.print(myRTC.minutes);
display.setCursor(70, 10);
display.print(dis_sic);
display.setTextSize(1);
display.print("C");
display.display();
lastPage = 1;
}
void duble_temp() {
temp_ic();
temp_dis();
display.drawBitmap(0, 2, kabin, 11, 32, 1);
display.drawBitmap(64, 2, termometre, 11, 32, 1);
display.setTextSize(2);
display.setCursor(16, 10);
display.print(I1, 0);
//display.print("C");
display.setCursor(80, 10);
display.print(D1, 0);
//display.print("C");
display.display();
lastPage = 4;
}
void RTCSaat() {
myRTC.updateTime();
currentMillis = millis();
if (currentMillis - previousMillis >= 2000) {
currentMillis = 0;
myRTC.updateTime();
}
}
void acilis() {
for (int xLoc = 128; xLoc >= 0; (xLoc = xLoc - 32)) {
display.drawBitmap(xLoc, 0, silouhette, 128, 32, 1);
display.display();
delay(35);
display.clearDisplay();
// display.fillScreen(BLACK);
delay(5);
}
delay(750);
display.drawBitmap(0, 0, celica, 128, 32, 1);
display.display();
delay(750);
display.clearDisplay();
currentPage = lastPage;
executeCurrentFunction();
}