Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <RTClib.h>
- #include "DHT.h"
- #include "U8glib.h"
- ////////////////////////////////////////////
- U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE); // I2C / TWI
- #define SCREEN_WIDTH 128 // OLED display width, in pixels
- #define SCREEN_HEIGHT 64 // OLED display height, in pixels
- RTC_DS3231 rtc;
- String timee;
- String dayyy;
- String tem;
- #define DHTPIN 2
- #define DHTTYPE DHT11
- DHT dht(DHTPIN, DHTTYPE);
- const int MPU = 0x69;
- int16_t Tmp, GyX, GyY, GyZ;
- ///////////////////////////////////////////////
- void setup() {
- ////////////////////////////////////////////////////// for OLED u8g library ↓
- // flip screen, if required
- // u8g.setRot180();
- // assign default color value
- if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
- u8g.setColorIndex(255); // white
- }
- else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
- u8g.setColorIndex(3); // max intensity
- }
- else if ( u8g.getMode() == U8G_MODE_BW ) {
- u8g.setColorIndex(1); // pixel on
- }
- else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
- u8g.setHiColorByRGB(255, 255, 255);
- }
- pinMode(8, OUTPUT);
- ////////////////////////////////////////////////////// for OLED u8g library ↑
- Serial.begin(9600);
- dht.begin();
- initRTC();
- Wire.begin();
- Wire.beginTransmission(MPU);
- Wire.write(0x6B);
- Wire.write(0);
- Wire.endTransmission(true);
- }
- /////////////////////////////////////////
- void loop() {
- Gyroscope();
- DateTime now = rtc.now();
- float t = dht.readTemperature();
- timee = "";
- timee += now.hour();
- timee += ':';
- timee += now.minute();
- timee += ':';
- timee += now.second();
- dayyy = "";
- dayyy += (now.year());
- dayyy += '/';
- dayyy += (now.month());
- dayyy += '/';
- dayyy += (now.day());
- tem = t ;
- Serial.println(timee);
- Serial.println(dayyy);
- Serial.println(tem);
- if ( GyX > 2000 && GyX < 13000 ) {
- u8g.firstPage();
- do {
- u8g.setColorIndex(1);
- draw(timee, dayyy, tem, 30, 22);
- } while ( u8g.nextPage() );
- // rebuild the picture after some delay
- delay(50);
- }
- else {
- u8g.firstPage();
- do {
- u8g.setColorIndex(0);
- u8g.drawBox(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); // Draw a filled rectangle covering the entire screen
- } while (u8g.nextPage());
- delay(50);
- }
- }
- void initRTC() {
- if (! rtc.begin()) {
- Serial.println("Couldn't find RTC");
- Serial.flush();
- while (1) delay(10);
- }
- if (rtc.lostPower()) {
- Serial.println("RTC lost power, let's set the time!");
- rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- }
- }
- void Gyroscope() {
- Wire.beginTransmission(MPU);
- Wire.write(0x3B);
- Wire.endTransmission(false);
- Wire.requestFrom(MPU, 12, true);
- GyX = Wire.read() << 8 | Wire.read();
- GyY = Wire.read() << 8 | Wire.read();
- GyZ = Wire.read() << 8 | Wire.read();
- Serial.print("Gyroscope: ");
- Serial.print("X = "); Serial.print(GyX);
- Serial.print(" | Y = "); Serial.print(GyY);
- Serial.print(" | Z = "); Serial.println(GyZ);
- Serial.println(" ");
- delay(333);
- }
- void draw(String T1, String T2, String T3, int x, int y) {
- // graphic commands to redraw the complete screen should be placed here
- u8g.setFont(u8g_font_unifont);
- //u8g.setFont(u8g_font_osb21);
- u8g.setPrintPos(x, y);
- u8g.print(T1);
- u8g.setPrintPos(x, y + 20);
- u8g.print(T2);
- u8g.setPrintPos(x, y + 40);
- u8g.print(T3);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement