kennyho-rass

Untitled

Sep 25th, 2019
797
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.  
  14. Adafruit_BMP280 bmp;     // inicializace senzoru BME z knihovny
  15. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  16.  
  17. const int MPU_addr = 0x68;  // inicializace proměnné pro určení adresy senzoru
  18. int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;  // inicializace proměnných, do kterých se uloží data
  19.  
  20.  
  21. //konstanty co se nemeni
  22. const int button1 = A0;
  23. const int button2 = A1;
  24. const int button3 = A2;
  25.  
  26. const int led1 =  2;
  27. const int led2 =  3;
  28. const int led3 =  4;
  29.  
  30. bool a, points;
  31. int  x = 0;
  32. tmElements_t tm;
  33.  
  34. void setup() {
  35.   pinMode(button1, INPUT_PULLUP);  // nastaví pin kde je tlačítko jako vstup
  36.   pinMode(button2, INPUT_PULLUP);
  37.   pinMode(button3, INPUT_PULLUP);
  38.   pinMode(led1, OUTPUT);
  39.   pinMode(led2, OUTPUT); // nastaví vystup u Led2 na výstup
  40.   pinMode(led3, OUTPUT); // nastaví vystup u Led3 na výstup
  41. //  digitalWrite(button1, HIGH); // zapnutí pullup rezistoru,
  42. //  digitalWrite(button2, HIGH); // zapnutí pullup rezistoru,
  43. //  digitalWrite(button3, HIGH); // zapnutí pullup rezistoru,
  44.   Serial.begin(115200);  // komunikace po sériové lince rychlostí 115200 baud
  45.  
  46. }
  47.  
  48. uint8_t button = 0;
  49. void loop()
  50. { // tady si vyctes stavy tlacitek a nastavis promennou
  51.   if (digitalRead(1) == true)
  52.     button = 1;
  53.   else if (digitalRead(2) == true)
  54.     button = 2;
  55.   else if (digitalRead(3) == true)
  56.     button = 3;
  57.  
  58.   switch (button)
  59.   {
  60.     case 1:
  61.       { // provede se ukol pro prvni tlacitko
  62.         digitalWrite(led1, HIGH);
  63.        
  64.         RTC.read(tm);
  65.         display.setTextSize(2);
  66.         display.setTextColor(WHITE);
  67.         //display.setCursor(17,0);
  68.         display.print(":");
  69.         display.print(tm.Minute);
  70.         display.print(":");
  71.         display.print(tm.Second);
  72.         display.setTextSize(1);
  73.         if (a == true)
  74.           display.print("am");
  75.         if (a == false)
  76.           display.print("pm");
  77.         display.setCursor(17, 17);
  78.         display.setTextSize(2);
  79.         display.print(tm.Month);
  80.         display.print('/');
  81.         display.print(tm.Day);
  82.         display.print('/');
  83.         display.print(tmYearToCalendar(tm.Year));
  84.         display.display();
  85.         delay(100);
  86.      //   display.clearDisplay();
  87.       }
  88.       break;
  89.   case 2:
  90.   { // provede se ukol pro druhe tlacitko
  91.     digitalWrite(led2, HIGH);
  92.  
  93.     bmp.begin();                                //Start the bmp
  94.     display.clearDisplay();
  95.     float T = bmp.readTemperature();
  96.     //Read temperature in C
  97.     float P = bmp.readPressure() / 100;
  98.     //Read Pressure in Pa and conversion to hPa
  99.     float A = bmp.readAltitude(1019.66);
  100.     display.setCursor(0, 0);
  101.     display.setTextColor(WHITE);
  102.     display.setTextSize(2);
  103.     display.print("Temp");
  104.  
  105.     display.setCursor(0, 18);
  106.     display.print(T, 1);
  107.     display.setCursor(50, 17);
  108.     display.setTextSize(2);
  109.     display.print("C");
  110.  
  111.     display.setTextSize(1);
  112.     display.setCursor(65, 0);
  113.     display.print("tlak");
  114.     display.setCursor(65, 10);
  115.     display.print(P);
  116.     display.setCursor(110, 10);
  117.     display.print("hPa");
  118.  
  119.     display.setCursor(65, 25);
  120.     display.print("Mnm");
  121.     display.setCursor(90, 25);
  122.     display.print(A, 0);
  123.     display.setCursor(110, 25);
  124.     display.print("m");
  125.     delay(10);
  126.     break;
  127.   }
  128. case 3:
  129.   { // provede se ukol pro teti tlacitko
  130.     digitalWrite(led3, HIGH);
  131.     // zapnutí přenosu
  132.     Wire.beginTransmission(MPU_addr);
  133.     // zápis do registru ACCEL_XOUT_H
  134.     Wire.write(0x3B);
  135.     Wire.endTransmission(false);
  136.     // vyzvednutí dat z 14 registrů
  137.     Wire.requestFrom(MPU_addr, 14, true);
  138.     AcX = Wire.read() << 8 | Wire.read();
  139.     AcY = Wire.read() << 8 | Wire.read();
  140.     AcZ = Wire.read() << 8 | Wire.read();
  141.     Tmp = Wire.read() << 8 | Wire.read();
  142.     GyX = Wire.read() << 8 | Wire.read();
  143.     GyY = Wire.read() << 8 | Wire.read();
  144.     GyZ = Wire.read() << 8 | Wire.read();
  145.     // výpis surových dat z proměnných na sériovou linku
  146.     Serial.print("AcX = "); Serial.print(AcX);
  147.     Serial.print(" | AcY = "); Serial.print(AcY);
  148.     Serial.print(" | AcZ = "); Serial.print(AcZ);
  149.     // přepočtení teploty dle datasheetu
  150.     Serial.print(" | Temp = "); Serial.print(Tmp / 340.00 + 36.53);
  151.     Serial.print(" | GyX = "); Serial.print(GyX);
  152.     Serial.print(" | GyY = "); Serial.print(GyY);
  153.     Serial.print(" | GyZ = "); Serial.println(GyZ);
  154.     delay(10);
  155.  
  156.     break;
  157.   }
  158. default:
  159.   {
  160.     // provede ukol pro ostatni stavy (napr. StisknuteTlacitko == 0)
  161.         display.clearDisplay();
  162.    
  163.     RTC.read(tm);
  164.     x = tm.Second % 2; //Dividing the seconds value by 2 and store the rest value, it's either 0 or 1
  165.     display.setTextSize(3);
  166.     display.setTextColor(WHITE);
  167.     display.setCursor(0, 10);
  168.     ampm(tm.Hour);
  169.  
  170.     if (x == 0)             //if x=0 we don't display the ":"
  171.       display.print(" ");
  172.     if (x == 1)             //if x=1 we display it, so for every 1s we display or not the ":"
  173.       display.print(":");
  174.  
  175.     display.print(tm.Minute);
  176.     display.setTextSize(1);
  177.     display.setCursor(50, 0);
  178.  
  179.     if (a == true)
  180.       display.print(" am");
  181.     if (a == false)
  182.       display.print(" pm");
  183.  
  184.     display.setCursor(90, 5);
  185.     display.setTextSize(2);
  186.     display.print(tm.Month);
  187.     display.print('/');
  188.     display.print(tm.Day);
  189.     display.setCursor(95, 23);
  190.     display.setTextSize(1);
  191.     display.print(tmYearToCalendar(tm.Year));
  192.     display.display();
  193.     display.clearDisplay();
  194.   }
  195. }
  196. }
  197.  
  198.  
  199. void ampm(int Hour) {  //ampm function takes the hour value 0-23
  200.   if (Hour == 0) {       //if it's 0 it becomes 12
  201.     Hour = 12;
  202.     a = true;             //true if it's am, false otherwise
  203.     display.print(Hour);
  204.   }
  205.   else if (Hour > 0 && Hour < 12) {
  206.     a = true;             //normal value
  207.     display.print(Hour);
  208.   }
  209.   else if (Hour == 12) {
  210.     a = false;             //changing to pm
  211.     display.print(Hour);
  212.   }
  213.   else if (Hour > 12) {
  214.     Hour = Hour - 12;     //substract 12 to bring it back to 1,2,3,...
  215.     a = false;
  216.     display.print(Hour);
  217.   }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment