Tak je to hotové,přidal jsem do nastavení tři řádky,nechápal jsem tu barvu na pozadí z jednoho důvodu ,už byla zadána v void setupu,pro to jsem to přehlížel......
Kód: Vybrat vše
/**************************************************************************
Zapojení:
Arduino nano
Oled 0.96 i2c SDA-A4
SCL-A5
RST 4
DS1307 SDA-A4
SCK-A5
LDR A0 (modul)
Vystup pro rele A1,A2 (modul)
2x tlačitko 9,8
**************************************************************************/
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define SSD1306_LCDHEIGHT 64
#define button1 9 // Tlačítko B1 pin 9
#define button2 8 // Tlačítko B1 pin 8
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
// 'skoda Logo', 128x64px
const unsigned char myBitmap [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbf, 0xfa, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x80, 0x01, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x0b, 0x50, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0xff, 0xff, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x8f, 0xe0, 0x3f, 0xc2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x3d, 0x00, 0x00, 0xf1, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x70, 0x07, 0x80, 0x3c, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0xc0, 0x7f, 0xc0, 0x0e, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x27, 0x80, 0x3f, 0xe0, 0x03, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x1e, 0x1f, 0xf8, 0x03, 0xc8, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0xcc, 0x3f, 0x87, 0xfc, 0x00, 0xc4, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x18, 0x7f, 0xe7, 0xfe, 0x00, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x78, 0xff, 0xf1, 0xfe, 0x00, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x02, 0x70, 0x7f, 0xf9, 0xfe, 0x00, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x06, 0x60, 0x0f, 0xff, 0xff, 0x00, 0x0c, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x04, 0xc7, 0x07, 0xff, 0xff, 0x80, 0x0c, 0xc0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x04, 0xc7, 0xc3, 0xff, 0xff, 0x80, 0x0e, 0x40, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x09, 0xcf, 0xf9, 0xff, 0xff, 0x80, 0x06, 0x40, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x09, 0x8f, 0xff, 0xff, 0xff, 0x80, 0x06, 0x40, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x09, 0x8f, 0xff, 0xff, 0xff, 0xc0, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x09, 0x1f, 0xff, 0xff, 0xf7, 0x80, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x13, 0x9f, 0xff, 0xff, 0xff, 0xc0, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x13, 0x01, 0xff, 0xff, 0xff, 0xc0, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x3f, 0xff, 0xff, 0xc0, 0x03, 0xa0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x1f, 0xff, 0xff, 0x80, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x13, 0x80, 0x07, 0xff, 0xff, 0xc0, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x03, 0xff, 0x8f, 0x80, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x13, 0x80, 0x01, 0xff, 0x17, 0x80, 0x07, 0x20, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x09, 0x80, 0x00, 0xff, 0x07, 0x80, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x09, 0x80, 0x01, 0xff, 0x87, 0x00, 0x06, 0x40, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x09, 0xc0, 0x00, 0xff, 0xcf, 0x80, 0x06, 0x40, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x04, 0xcf, 0xf8, 0xff, 0xfb, 0x7c, 0x0e, 0x40, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x04, 0xe7, 0xff, 0xff, 0xfe, 0x7e, 0x0c, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x02, 0x67, 0xff, 0xff, 0xff, 0xd7, 0x9c, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x02, 0x71, 0xff, 0xff, 0xff, 0xff, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x38, 0xf8, 0x00, 0x00, 0x7c, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x18, 0x60, 0x00, 0x00, 0x28, 0x72, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x8e, 0x00, 0x00, 0x00, 0x00, 0xe4, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x00, 0x00, 0x03, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x23, 0x80, 0x00, 0x00, 0x07, 0x98, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0xe0, 0x00, 0x00, 0x0e, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x78, 0x00, 0x00, 0x3c, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x3f, 0x00, 0x01, 0xf1, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0xf1, 0x5f, 0xc2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc1, 0xff, 0xfc, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x0d, 0x40, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x05, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xbf, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x12, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x22, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0xe7, 0x1e, 0x3f, 0xf1, 0xff, 0x01, 0xf0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xf0, 0x07, 0x3c, 0x70, 0x39, 0xe3, 0x83, 0xf8, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xe0, 0x06, 0x70, 0x70, 0x39, 0xe1, 0x83, 0xb8, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0xe7, 0xf0, 0x70, 0x39, 0xe1, 0x87, 0x1c, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x3f, 0xe7, 0xf8, 0x70, 0x39, 0xe1, 0x8f, 0x1c, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x67, 0x1c, 0x70, 0x39, 0xe3, 0x8f, 0xfe, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff, 0xf6, 0x1e, 0x3f, 0xf1, 0xff, 0x0e, 0xae, 0x00, 0x00, 0x00, 0x00
};
void setup(void) {
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.drawBitmap(0, 0, myBitmap, 128, 64, WHITE);
display.display();
delay(4000);
pinMode(A0,INPUT); //Analog A2 in LDR Pin
pinMode(A1,OUTPUT); //Světla On/Off Pin - potkavací
pinMode(A2,OUTPUT); //Světla On/Off Pin - tlumená
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
delay(1000);
display.clearDisplay();
display.display();
}
char Time[] = " : : ";
byte i, second, minute, hour;
void DS1307_display(){
// Convert BCD to decimal
second = (second >> 4) * 10 + (second & 0x0F);
minute = (minute >> 4) * 10 + (minute & 0x0F);
hour = (hour >> 4) * 10 + (hour & 0x0F);
Time[7] = second % 10 + 48;
Time[6] = second / 10 + 48;
Time[4] = minute % 10 + 48;
Time[3] = minute / 10 + 48;
Time[1] = hour % 10 + 48;
Time[0] = hour / 10 + 48;
draw_text(16, 5, Time, 2);
}
void blink_parameter(){
byte j = 0;
while(j < 10 && digitalRead(button1) && digitalRead(button2)){
j++;
delay(25);
}
}
byte edit(byte x_pos, byte y_pos, byte parameter){
char text[3];
sprintf(text,"%02u", parameter);
while(!digitalRead(button1)); // Počkejte, až uvolníte tlačítko B1
while(true){
while(!digitalRead(button2)){ // Pokud je stisknuto tlačítko B2
parameter++;
if(i == 3 && parameter > 23) // Pokud jsou hodiny> 23 ==> hodiny = 0
parameter = 0;
if(i == 4 && parameter > 59) // Pokud minuty> 59 ==> minuty = 0
parameter = 0;
sprintf(text,"%02u", parameter);
draw_text(x_pos, y_pos, text, 2);
delay(200); // Počkejte 200ms
}
draw_text(x_pos, y_pos, " ", 2);
blink_parameter();
draw_text(x_pos, y_pos, text, 2);
blink_parameter();
if(!digitalRead(button1)){ // Pokud je stisknuto tlačítko B1
i++; // Přírůstek „in“ pro další parametr
return parameter; // Vraťte hodnotu parametru a ukončete
}
}
}
void draw_text(byte x_pos, byte y_pos, char *text, byte text_size) {
display.setCursor(x_pos, y_pos);
display.setTextSize(text_size);
display.print(text);
display.display();
}
void loop() {
display.drawLine(0, 0, 127, 0, WHITE);
display.drawLine(0, 23, 127, 23, WHITE);
display.drawLine(127, 0, 127, 63, WHITE);
display.drawLine(127, 63, 0, 63, WHITE);
display.drawLine(0, 63, 0, 0, WHITE);
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(3, 26);
display.println("[");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(3, 46);
display.println("[");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(113, 26);
display.println("]");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(113, 46);
display.println("]");
if(!digitalRead(button1)){ // Pokud je stisknuto tlačítko B1
i = 0;
while(!digitalRead(button1)); // Počkejte na uvolnění tlačítka B1
while(true){
while(!digitalRead(button2)){ // Při stisknutém tlačítku B2
delay(200); // Wait 200 ms
}
if(!digitalRead(button1)) // Pokud je stisknuto tlačítko B1
blink_parameter(); // Bliká dané číslo
display.setTextColor(WHITE, BLACK);
break;
}
hour = edit(16, 5, hour); // Upravit hodiny
minute = edit(52, 5, minute); // Upravit minuty
// Convert decimal to BCD
minute = ((minute / 10) << 4) + (minute % 10);
hour = ((hour / 10) << 4) + (hour % 10);
// Write data to DS1307 RTC
Wire.beginTransmission(0x68); // Start I2C protokol s adresou DS1307
Wire.write(0); // Adresa odeslat registr
Wire.write(0); // Resetujte sekundy a spusťte oscilátor
Wire.write(minute); // Napiše minutu
Wire.write(hour); // Napiše hodinu
Wire.endTransmission(); // Zastavení přenosu a uvolnění I2C sběrnici
delay(200); // Počkejte 200ms
}
Wire.beginTransmission(0x68); // Start I2C protokol s adresou DS1307
Wire.write(0); // Odeslat registrační adresu
Wire.endTransmission(false); // I2C restart
Wire.requestFrom(0x68, 7); // Vyžáda si 7 bajtů od DS1307 a uvolněte I2C sběrnici na konci čtení
second = Wire.read(); // Čtení sekund z registru 0
minute = Wire.read(); // Přečtěte si zápis z registru 1
hour = Wire.read(); // Přečíst hodinu z registru 2
DS1307_display(); // Zobrazit čas
delay(50); // Počkejte 50ms
//Head Light On/Off
int digital=analogRead(A0); // LDR in
display.println(digital);
if(digital>=700){
digitalWrite(A1,LOW); // Zapne světla - potkavací
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(35, 26);
display.println("Zapla");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(30, 44);
display.println("svetla");
digitalWrite(A2,LOW); // Zapne světla - tlumená
delay(100);
} else{
digitalWrite(A1,HIGH); // Vypne světla - potkavací
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(35, 26);
display.println("Denni");
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(25, 44);
display.println("sviceni");
digitalWrite(A2,HIGH); // Vypne světla - tlumená
delay(100);
}
}