Hi guys noob here;
A while ago I decided to get rid of my cars only electronic display showing clock (old 4-digit 7-segment) and make a new and updated one. I worked hard on it but I reached my limit with code being in that state. My info panel gets stuck constantly and the data I am trying to pull from various sensors seems to be not working. Please take a look and help me to be a better arduino user. Any help would be greatly appreciated, I am open for all types of suggestions.
#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>
#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> voltaj_olcum_2;
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
};
const unsigned char saatimge[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00,
0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x20, 0x00,
0x20, 0x00, 0x20, 0x00, 0x20, 0x00, 0x10, 0x00, 0x0c, 0x00, 0x03, 0x00, 0x00, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
int currentMillis = 0;
int previousMillis = 0;
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_AVERAGE, 10);
ic_olcum.begin(SMOOTHED_EXPONENTIAL, 4);
dis_olcum.begin(SMOOTHED_EXPONENTIAL, 4);
sensors.begin();
myRTC.setDS1302Time(0, dakika, saat, 0, 0, 0, 0);
delay(1000);
display.clearDisplay();
acilis();
}
void loop() {
int ana = digitalRead(anaButon);
int yukari = digitalRead(upButon);
int asagi = digitalRead(downButon);
if (ana == LOW) {
currentPage = (currentPage + 1) % Page_Count;
display.clearDisplay();
Serial.println("touch");
delay(50);
executeCurrentFunction();
delay(50);
}
if (yukari == LOW) {
saat = saat + 1;
if (saat > 23)
saat = 0;
Serial.print("yukarı");
delay(50);
}
if (asagi == LOW) {
dakika = dakika + 1;
if (dakika > 59)
dakika = 0;
Serial.print("asagi");
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, 0, kabin, 11, 32, 1);
display.setCursor(28, 8);
display.setTextSize(3);
display.print(I1, 1);
display.print(" C");
display.display();
lastPage = 3;
delay(50);
}
double temp_ic() {
double ic_sic = analogRead(ic);
ic_sic = log(10000.0 * ((1024.0 / ic - 1.0)));
ic_sic = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * ic_sic * ic_sic)) * ic_sic);
ic_sic = ic_sic - 273.15;
ic_olcum.add(ic_sic);
I1 = ic_olcum.get();
}
void dis_sicaklik() {
temp_dis();
display.drawBitmap(0, 0, 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, 0, guc, 11, 32, 1);
display.setTextSize(3);
display.setCursor(20, 8);
display.print(V1, 1);
display.setCursor(110, 8);
display.print(" V");
display.display();
lastPage = 5;
delay(100);
}
void zaman_temp() {
temp_dis();
RTCSaat();
display.drawBitmap(0, 0, saatimge, 11, 32, 1);
display.drawBitmap(64, 0, termometre, 11, 32, 1);
display.setTextSize(2);
display.setCursor(16, 10);
if (myRTC.hours < 10) { display.print("0"); }
display.print(myRTC.hours);
if (myRTC.minutes < 10) { display.print("0"); }
display.print(myRTC.minutes);
display.setCursor(80, 10);
display.print(dis_sic);
//display.print("C");
display.display();
lastPage = 1;
}
void duble_temp() {
temp_ic();
temp_dis();
display.drawBitmap(0, 0, kabin, 11, 32, 1);
display.drawBitmap(64, 0, termometre, 11, 32, 1);
display.setTextSize(2);
display.setCursor(16, 10);
display.print(I1);
//display.print("C");
display.setCursor(80, 10);
display.print(D1);
//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();
}
My goal in this project is to have 6 separate screens
1- clock
2- clock/ exterior temp
3- exterior temp
4- interior temp
5- exterior/interior temp
6- voltage feed from battery
I also want to use a DS 1302 module to keep track of time and adjust the time using only two buttons. Button 1 will increase the hour component whereas Button 2 will increase the minute.