kennyho-rass

rukavice

Sep 23rd, 2019
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2. #include <SPI.h>
  3. #include <Adafruit_GFX.h>      //Libraries for the OLED and BMP280
  4. #include <Adafruit_SSD1306.h>
  5. #include <Adafruit_Sensor.h>
  6. #include <Adafruit_BMP280.h>
  7. #include <DS1307RTC.h>
  8.  
  9. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  10. #define SCREEN_HEIGHT 32 // OLED display height, in pixels
  11. #define OLED_RESET    -1 // Reset pin # (or -1 if sharing Arduino reset pin)
  12. #define BMP280_ADRESA (0x76)   // nastavení adresy senzoru
  13. Adafruit_BMP280 bmp;  // inicializace senzoru BMP z knihovny
  14.  
  15. bool a, points;
  16. int  x = 0;
  17.  
  18. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  19. tmElements_t tm;
  20.  
  21.  
  22. // inicializace proměnné pro určení adresy senzoru
  23. // 0x68 nebo 0x69, dle připojení AD0
  24. const int MPU_addr = 0x68;
  25. // inicializace proměnných, do kterých se uloží data
  26. int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
  27.  
  28. const int button0 = A0;  //konstanty co se nemeni pro tlacitka
  29. const int button1 = A1;
  30. const int button2 = A2;
  31.  
  32. const int led1 =  2;   //konstanty co se nemeni pro led
  33. const int led2 =  3;
  34. const int led3 =  4;
  35.  
  36. void setup() {
  37.   pinMode(button0, INPUT_PULLUP);  // nastaví pin kde je tlačítko jako vstup
  38.   pinMode(button1, INPUT_PULLUP);
  39.   pinMode(button2, INPUT_PULLUP);
  40.   pinMode(led1, OUTPUT);
  41.   pinMode(led2, OUTPUT); // nastaví vystup u Led2 na výstup
  42.   pinMode(led3, OUTPUT); // nastaví vystup u Led3 na výstup
  43.   digitalWrite(button0, HIGH); // zapnutí pullup rezistoru,
  44.   digitalWrite(button1, HIGH); // zapnutí pullup rezistoru,
  45.   digitalWrite(button2, HIGH); // zapnutí pullup rezistoru,
  46.   Serial.begin(115200);  // komunikace po sériové lince rychlostí 115200 baud
  47.  
  48.   bmp.begin();                                //Start the bmp
  49.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display
  50.   display.clearDisplay();
  51.   display.display();
  52.   }
  53.  
  54. void loop(bmp) {
  55.   if ((digitalRead(button0)) == LOW) {
  56.     digitalWrite(led1, HIGH);
  57.  
  58.     display.clearDisplay();
  59.     float T = bmp.readTemperature();
  60.     //Read temperature in C
  61.     float P = bmp.readPressure() / 100;
  62.     //Read Pressure in Pa and conversion to hPa
  63.     float A = bmp.readAltitude(1019.66);
  64.     display.setCursor(0, 0);
  65.     display.setTextColor(WHITE);
  66.     display.setTextSize(2);
  67.     display.print("Temp");
  68.  
  69.     display.setCursor(0, 18);
  70.     display.print(T, 1);
  71.     display.setCursor(50, 17);
  72.     display.setTextSize(2);
  73.     display.print("C");
  74.  
  75.     display.setTextSize(1);
  76.     display.setCursor(65, 0);
  77.     display.print("tlak");
  78.     display.setCursor(65, 10);
  79.     display.print(P);
  80.     display.setCursor(110, 10);
  81.     display.print("hPa");
  82.  
  83.     display.setCursor(65, 25);
  84.     display.print("Mnm");
  85.     display.setCursor(90, 25);
  86.     display.print(A, 0);
  87.     display.setCursor(110, 25);
  88.     display.print("m");
  89.  
  90.     display.display();
  91.     // výpis všech dostupných informací ze senzoru BMP
  92.     // výpis teploty
  93.     Serial.print("Teplota: ");
  94.     Serial.print(bmp.readTemperature());
  95.     Serial.println(" stupnu Celsia.");
  96.     // výpis tlaku s přepočtem na hektoPascaly
  97.     Serial.print("Tlak: ");
  98.     Serial.print(bmp.readPressure() / 100.0F);
  99.     Serial.println(" hPa.");
  100.     // vytištění prázdného řádku a pauza po dobu 2 vteřin
  101.     Serial.println();
  102.     delay(100);
  103.   } else {
  104.     //while (1);
  105.     digitalWrite(led1, LOW);
  106.   }
  107.  
  108.  
  109.   if ((digitalRead(button1)) == LOW) {
  110.     digitalWrite(led2, HIGH);
  111.  
  112.     display.clearDisplay();
  113.    
  114.     RTC.read(tm);
  115.     x = tm.Second % 2; //Dividing the seconds value by 2 and store the rest value, it's either 0 or 1
  116.     display.setTextSize(3);
  117.     display.setTextColor(WHITE);
  118.     display.setCursor(0, 10);
  119.     ampm(tm.Hour);
  120.  
  121.     if (x == 0)             //if x=0 we don't display the ":"
  122.       display.print(" ");
  123.     if (x == 1)             //if x=1 we display it, so for every 1s we display or not the ":"
  124.       display.print(":");
  125.  
  126.     display.print(tm.Minute);
  127.     display.setTextSize(1);
  128.     display.setCursor(50, 0);
  129.  
  130.     if (a == true)
  131.       display.print(" am");
  132.     if (a == false)
  133.       display.print(" pm");
  134.  
  135.     display.setCursor(90, 5);
  136.     display.setTextSize(2);
  137.     display.print(tm.Month);
  138.     display.print('/');
  139.     display.print(tm.Day);
  140.     display.setCursor(95, 23);
  141.     display.setTextSize(1);
  142.     display.print(tmYearToCalendar(tm.Year));
  143.     display.display();
  144.     display.clearDisplay();
  145.   } else {
  146.     digitalWrite(led2, LOW);
  147.   }
  148.  
  149.  
  150.  
  151.   if ((digitalRead(button2)) == LOW) {
  152.     digitalWrite(led3, HIGH);
  153.  
  154.    
  155.     // zapnutí přenosu
  156.     Wire.beginTransmission(MPU_addr);
  157.     // zápis do registru ACCEL_XOUT_H
  158.     Wire.write(0x3B);
  159.     Wire.endTransmission(false);
  160.     // vyzvednutí dat z 14 registrů
  161.     Wire.requestFrom(MPU_addr, 14, true);
  162.     AcX = Wire.read() << 8 | Wire.read();
  163.     AcY = Wire.read() << 8 | Wire.read();
  164.     AcZ = Wire.read() << 8 | Wire.read();
  165.     Tmp = Wire.read() << 8 | Wire.read();
  166.     GyX = Wire.read() << 8 | Wire.read();
  167.     GyY = Wire.read() << 8 | Wire.read();
  168.     GyZ = Wire.read() << 8 | Wire.read();
  169.     // výpis surových dat z proměnných na sériovou linku
  170.     Serial.print("AcX = "); Serial.print(AcX);
  171.     Serial.print(" | AcY = "); Serial.print(AcY);
  172.     Serial.print(" | AcZ = "); Serial.print(AcZ);
  173.     // přepočtení teploty dle datasheetu
  174.     Serial.print(" | Temp = "); Serial.print(Tmp / 340.00 + 36.53);
  175.     Serial.print(" | GyX = "); Serial.print(GyX);
  176.     Serial.print(" | GyY = "); Serial.print(GyY);
  177.     Serial.print(" | GyZ = "); Serial.println(GyZ);
  178.     delay(100);
  179.   } else {
  180.     digitalWrite(led3, LOW);
  181.   }
  182.  
  183.  
  184. } // konec tretiho tlacitka
  185.  
  186. void ampm(int Hour){   //ampm function takes the hour value 0-23
  187. if(Hour==0){           //if it's 0 it becomes 12
  188.   Hour=12;
  189.   a=true;               //true if it's am, false otherwise
  190.   display.print(Hour);
  191. }
  192. else if(Hour>0 && Hour<12){
  193.   a=true;               //normal value
  194.   display.print(Hour);
  195. }
  196. else if(Hour==12){
  197.   a=false;               //changing to pm
  198.   display.print(Hour);
  199. }
  200. else if(Hour>12){
  201.   Hour=Hour-12;         //substract 12 to bring it back to 1,2,3,...
  202.   a=false;
  203.   display.print(Hour);
  204. }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment