Teraz przejdę do analizy kodu na platformę Arduino. Pełen kod programu dostępny jest sekcji Do pobrania.
Na początku programu dołączone są wszystkie niezbędne biblioteki potrzebne do działania poszczególnych modułów.
1 2 3 4 5 |
#include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include "RTClib.h" |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Konfiguracja wyświetlacz OLED #define OLED_MOSI 9 #define OLED_CLK 10 #define OLED_DC 11 #define OLED_CS 12 #define OLED_RESET 13 Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS); #if (SSD1306_LCDHEIGHT != 64) #error("Height incorrect, please fix Adafruit_SSD1306.h!"); #endif // Konfiguracja RTC RTC_DS1307 rtc; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
void setup() { Serial.begin(9600); if (! rtc.begin()) { Serial.println("Nie znaleziono modułu RTC!"); while (1); } if (! rtc.isrunning()) { Serial.println("RTC nie jest uruchomiony!"); // following line sets the RTC to the date & time this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // This line sets the RTC with an explicit date & time, for example to set // January 21, 2014 at 3am you would call: // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0)); } display.begin(SSD1306_SWITCHCAPVCC); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
void loop() { DateTime now = rtc.now(); String Czas = ""; String Data = ""; // Tworzenie czasu Czas += String(now.hour()); Czas += ':'; Czas += String(now.minute()); Czas += ':'; Czas += String(now.second()); // Tworzenie Daty Data += String(now.day()); Data += '.'; Data += String(now.month()); Data += '.'; Data += String(now.year()); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0,0); display.println(" MIKROKONTROLER.PL " ); display.setCursor(20,18); display.setTextSize(2); display.setTextColor(WHITE); display.println(Czas); display.setCursor(10,36); display.println(Data); display.display(); delay(1000); display.clearDisplay(); } |