Advertisement
safwan092

Untitled

Oct 25th, 2023
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1.  
  2. #include <Wire.h>
  3. #include <Adafruit_GFX.h>
  4. #include <Adafruit_SH1106.h>
  5. #include <RTClib.h>
  6. #include "DHT.h"
  7.  
  8. ////////////////////////////////////////////
  9.  
  10.  
  11. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  12. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  13. #define OLED_RESET 4
  14. Adafruit_SH1106 display(OLED_RESET);
  15. RTC_DS3231 rtc;
  16. String timee;
  17. String dayyy;
  18. String tem;
  19. #define DHTPIN 2
  20. #define DHTTYPE DHT11
  21. DHT dht(DHTPIN, DHTTYPE);
  22.  
  23.  
  24. const int MPU = 0x69;
  25. int16_t Tmp, GyX, GyY, GyZ;
  26. ///////////////////////////////////////////////
  27.  
  28. void setup() {
  29.  
  30. Serial.begin(9600);
  31. dht.begin();
  32. //////////////////
  33. initRTC();
  34. display.begin(SH1106_SWITCHCAPVCC, 0x3C);
  35. display.display();
  36. delay(2000); // wait for initializing
  37.  
  38. ////////////////////
  39. display.clearDisplay(); // clear display
  40. delay(2000);
  41. display.display();
  42. display.setTextSize(2); // text size
  43. display.setTextColor(WHITE); // text color
  44. display.setCursor(0, 10); // position to display
  45.  
  46. //////////////////////////
  47. Wire.begin();
  48. Wire.beginTransmission(MPU);
  49. Wire.write(0x6B);
  50. Wire.write(0);
  51. Wire.endTransmission(true);
  52. }
  53.  
  54. /////////////////////////////////////////
  55. void loop() {
  56. //Gyroscope();
  57. DateTime now = rtc.now();
  58. float t = dht.readTemperature();
  59. //16828
  60.  
  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. display.setTextSize(2); // text size
  78. display.setTextColor(WHITE); // text color
  79. // Clear specific areas on the display
  80. display.fillRect(0, 0, display.width(), display.height(), BLACK);
  81. display.fillRect(0, 20, display.width(), display.height(), BLACK);
  82. display.fillRect(30, 40, display.width(), display.height(), BLACK);
  83. display.display();
  84. delay(500);
  85. display.setCursor(0, 0);
  86. display.println(timee);
  87. display.setCursor(0, 20);
  88. display.println(dayyy);
  89. display.setCursor(30, 40);
  90. display.println(tem);
  91. display.display();
  92. delay(2000);
  93. /*
  94. if ( GyX > 3000 && GyX < 7000 ) {
  95. //oledDisplayCenter(timee, dayyy, tem);
  96. }
  97. else {
  98. //display.clearDisplay();
  99. display.display();
  100. delay(1000);
  101. }
  102. */
  103. }
  104.  
  105. //////////////////////////////////
  106. void oledDisplayCenter(String text, String text2 , String text3 ) {
  107. int16_t x1;
  108. int16_t y1;
  109. uint16_t width;
  110. uint16_t height;
  111. //oled.setCursor(0, 0);
  112. //oled.getTextBounds(text, 0, 0, &x1, &y1, oled.width() / 2, oled.height() / 2);
  113. //oled.println(text);
  114. // display on horizontal and vertical center
  115. display.clearDisplay(); // clear display
  116. display.setCursor(0, 0);
  117. display.println(text);
  118. display.setCursor(10, 20);
  119. display.println(text2);
  120. display.setCursor(30, 40);
  121. display.println(text3);
  122. display.display();
  123. delay(2000);
  124. }
  125. //////////////////////////////////////////
  126.  
  127. void initRTC() {
  128. if (! rtc.begin()) {
  129. Serial.println("Couldn't find RTC");
  130. Serial.flush();
  131. while (1) delay(10);
  132. }
  133.  
  134. if (rtc.lostPower()) {
  135. Serial.println("RTC lost power, let's set the time!");
  136. rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  137. }
  138. }
  139.  
  140. void Gyroscope() {
  141. Wire.beginTransmission(MPU);
  142. Wire.write(0x3B);
  143. Wire.endTransmission(false);
  144. Wire.requestFrom(MPU, 12, true);
  145. GyX = Wire.read() << 8 | Wire.read();
  146. GyY = Wire.read() << 8 | Wire.read();
  147. GyZ = Wire.read() << 8 | Wire.read();
  148. Serial.print("Gyroscope: ");
  149. Serial.print("X = "); Serial.print(GyX);
  150. Serial.print(" | Y = "); Serial.print(GyY);
  151. Serial.print(" | Z = "); Serial.println(GyZ);
  152. Serial.println(" ");
  153. delay(333);
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement