Advertisement
safwan092

Untitled

Oct 28th, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1.  
  2. #include <Wire.h>
  3. #include <RTClib.h>
  4. #include "DHT.h"
  5. #include "U8glib.h"
  6. ////////////////////////////////////////////
  7. U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE); // I2C / TWI
  8.  
  9.  
  10. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  11. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  12.  
  13. RTC_DS3231 rtc;
  14. String timee;
  15. String dayyy;
  16. String tem;
  17. #define DHTPIN 2
  18. #define DHTTYPE DHT11
  19. DHT dht(DHTPIN, DHTTYPE);
  20.  
  21.  
  22. const int MPU = 0x69;
  23. int16_t Tmp, GyX, GyY, GyZ;
  24. ///////////////////////////////////////////////
  25.  
  26. void setup() {
  27. ////////////////////////////////////////////////////// for OLED u8g library ↓
  28. // flip screen, if required
  29. // u8g.setRot180();
  30.  
  31. // assign default color value
  32. if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
  33. u8g.setColorIndex(255); // white
  34. }
  35. else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
  36. u8g.setColorIndex(3); // max intensity
  37. }
  38. else if ( u8g.getMode() == U8G_MODE_BW ) {
  39. u8g.setColorIndex(1); // pixel on
  40. }
  41. else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
  42. u8g.setHiColorByRGB(255, 255, 255);
  43. }
  44. pinMode(8, OUTPUT);
  45. ////////////////////////////////////////////////////// for OLED u8g library ↑
  46. Serial.begin(9600);
  47. dht.begin();
  48. initRTC();
  49. Wire.begin();
  50. Wire.beginTransmission(MPU);
  51. Wire.write(0x6B);
  52. Wire.write(0);
  53. Wire.endTransmission(true);
  54. }
  55.  
  56. /////////////////////////////////////////
  57. void loop() {
  58. Gyroscope();
  59. DateTime now = rtc.now();
  60. float t = dht.readTemperature();
  61. timee = "";
  62. timee += now.hour();
  63. timee += ':';
  64. timee += now.minute();
  65. timee += ':';
  66. timee += now.second();
  67. dayyy = "";
  68. dayyy += (now.year());
  69. dayyy += '/';
  70. dayyy += (now.month());
  71. dayyy += '/';
  72. dayyy += (now.day());
  73. tem = t ;
  74. Serial.println(timee);
  75. Serial.println(dayyy);
  76. Serial.println(tem);
  77.  
  78.  
  79.  
  80. if ( GyX > 2000 && GyX < 13000 ) {
  81. u8g.firstPage();
  82. do {
  83. u8g.setColorIndex(1);
  84. draw(timee, dayyy, tem, 30, 22);
  85. } while ( u8g.nextPage() );
  86. // rebuild the picture after some delay
  87. delay(50);
  88. }
  89. else {
  90. u8g.firstPage();
  91. do {
  92. u8g.setColorIndex(0);
  93. u8g.drawBox(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); // Draw a filled rectangle covering the entire screen
  94. } while (u8g.nextPage());
  95. delay(50);
  96. }
  97.  
  98. }
  99.  
  100.  
  101. void initRTC() {
  102. if (! rtc.begin()) {
  103. Serial.println("Couldn't find RTC");
  104. Serial.flush();
  105. while (1) delay(10);
  106. }
  107.  
  108. if (rtc.lostPower()) {
  109. Serial.println("RTC lost power, let's set the time!");
  110. rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  111. }
  112. }
  113.  
  114. void Gyroscope() {
  115. Wire.beginTransmission(MPU);
  116. Wire.write(0x3B);
  117. Wire.endTransmission(false);
  118. Wire.requestFrom(MPU, 12, true);
  119. GyX = Wire.read() << 8 | Wire.read();
  120. GyY = Wire.read() << 8 | Wire.read();
  121. GyZ = Wire.read() << 8 | Wire.read();
  122. Serial.print("Gyroscope: ");
  123. Serial.print("X = "); Serial.print(GyX);
  124. Serial.print(" | Y = "); Serial.print(GyY);
  125. Serial.print(" | Z = "); Serial.println(GyZ);
  126. Serial.println(" ");
  127. delay(333);
  128. }
  129.  
  130. void draw(String T1, String T2, String T3, int x, int y) {
  131. // graphic commands to redraw the complete screen should be placed here
  132. u8g.setFont(u8g_font_unifont);
  133. //u8g.setFont(u8g_font_osb21);
  134. u8g.setPrintPos(x, y);
  135. u8g.print(T1);
  136. u8g.setPrintPos(x, y + 20);
  137. u8g.print(T2);
  138. u8g.setPrintPos(x, y + 40);
  139. u8g.print(T3);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement