Advertisement
nikolas77

horloge et station meteo

Jun 28th, 2020
230
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_Sensor.h>
  4. #include <Adafruit_BME280.h>
  5.  
  6. #define BME_SCK 52
  7. #define BME_MISO 50
  8. #define BME_MOSI 51
  9. #define BME_CS 40
  10.  
  11. #define SEALEVELPRESSURE_HPA (1013.25)
  12.  
  13. Adafruit_BME280 bme(BME_CS);
  14.  
  15. unsigned long delayTime;
  16.  
  17. #include "Adafruit_GFX.h"
  18. #include "Adafruit_ILI9341.h"
  19. #include "SPI.h"
  20.  
  21. #define TFT_DC 9
  22. #define TFT_CS 10
  23.  
  24. #define RED2RED 0
  25. #define GREEN2GREEN 1
  26. #define BLUE2BLUE 2
  27. #define BLUE2RED 3
  28. #define GREEN2RED 4
  29. #define RED2GREEN 5
  30.  
  31. #define ILI9341_GREY 0x2104
  32.  
  33. Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
  34.  
  35. uint32_t runTime = -99999;
  36.  
  37. int Temp = 0;
  38. int Humid = 0;
  39.  
  40. #include "RTClib.h"
  41.  
  42. RTC_PCF8523 rtc;
  43.  
  44. char daysOfTheWeek[7][12] = {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"};
  45.  
  46.  
  47. void setup(void) {
  48.   tft.begin();
  49.   tft.setRotation(1);
  50.   tft.fillScreen(ILI9341_BLACK);
  51.  
  52.   tft.setCursor(2, 90);
  53.   tft.setTextColor(ILI9341_WHITE);
  54.   tft.setTextSize(2);
  55.   tft.print("Temperature")  ;
  56.  
  57.   tft.setCursor(200, 90);
  58.   tft.setTextColor(ILI9341_WHITE);
  59.   tft.setTextSize(2);
  60.   tft.print("Humidite")  ;
  61.  
  62.   while (!Serial);
  63.   Serial.println(F("BME280 test"));
  64.  
  65.   unsigned status;
  66.   status = bme.begin();
  67.  
  68.     Serial.begin(57600);
  69.  
  70. #ifndef ESP8266
  71.   while (!Serial); // wait for serial port to connect. Needed for native USB
  72. #endif
  73.  
  74.   if (! rtc.begin()) {
  75.     Serial.println("Couldn't find RTC");
  76.     Serial.flush();
  77.     abort();
  78.   }
  79.  
  80.   if (! rtc.initialized() || rtc.lostPower()) {
  81.     Serial.println("RTC is NOT initialized, let's set the time!");
  82.     // When time needs to be set on a new device, or after a power loss, the
  83.     // following line sets the RTC to the date & time this sketch was compiled
  84.     //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  85.     // This line sets the RTC with an explicit date & time, for example to set
  86.     // January 21, 2014 at 3am you would call:
  87.     // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  88.     //
  89.     // Note: allow 2 seconds after inserting battery or applying external power
  90.     // without battery before calling adjust(). This gives the PCF8523's
  91.     // crystal oscillator time to stabilize. If you call adjust() very quickly
  92.     // after the RTC is powered, lostPower() may still return true.
  93.   }
  94.  
  95.   // When time needs to be re-set on a previously configured device, the
  96.   // following line sets the RTC to the date & time this sketch was compiled
  97.   rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  98.   // This line sets the RTC with an explicit date & time, for example to set
  99.   // January 21, 2014 at 3am you would call:
  100.   // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
  101. }
  102.  
  103. void loop() {
  104.  
  105.   DateTime now = rtc.now();
  106.  
  107.   //HEURES,MINUTES,SECONDES
  108.   tft.fillRect (0, 0, 320, 40, ILI9341_BLACK);
  109.   tft.setCursor(70, 5);
  110.   tft.setTextColor(ILI9341_WHITE);
  111.   tft.setTextSize(4, 5);
  112.   if (now.hour() < 10) {
  113.     tft.print('0');
  114.   }
  115.   tft.print(now.hour(), DEC);
  116.   tft.print(':');
  117.   if (now.minute() < 10) {
  118.     tft.print('0');
  119.   }
  120.   tft.print(now.minute(), DEC);
  121.   tft.print(':');
  122.   if (now.second() < 10) {
  123.     tft.print('0');
  124.   }
  125.   tft.print(now.second(), DEC);
  126.  
  127.   //JOURS
  128.   tft.fillRect (0, 60, 120, 20, ILI9341_BLACK);
  129.   tft.setCursor(5, 55);
  130.   tft.setTextColor(ILI9341_WHITE);
  131.   tft.setTextSize(2);
  132.   tft.print(daysOfTheWeek[now.dayOfTheWeek()]);
  133.  
  134.   //DATE
  135.   tft.fillRect (190, 60, 320, 20, ILI9341_BLACK);
  136.   tft.setCursor(200, 55);
  137.   tft.setTextColor(ILI9341_WHITE);
  138.   tft.setTextSize(2);
  139.   if (now.day() < 10) {
  140.     tft.print('0');
  141.   }
  142.   tft.print(now.day(), DEC);
  143.   tft.print('/');
  144.   if (now.month() < 10) {
  145.     tft.print('0');
  146.   }
  147.   tft.print(now.month(), DEC);
  148.   tft.print('/');
  149.   tft.print(now.year(), DEC);
  150.   tft.println();
  151.   delay(1000);
  152.  
  153.   if (millis() - runTime >= 1000L) { // Execute every 2s
  154.     runTime = millis();
  155.  
  156.     int xpos = 15, ypos = 115, gap = 24, radius = 64;
  157.  
  158.     ringMeter(Temp, -0, 85, xpos, ypos, radius, "C", GREEN2RED);
  159.  
  160.     xpos = 180, ypos = 115, gap = 24, radius = 64;
  161.  
  162.     ringMeter(Humid, -0, 100, xpos, ypos, radius, "%", BLUE2RED);
  163.  
  164.   }
  165. }
  166. int ringMeter(int value, int vmin, int vmax, int x, int y, int r, char *units, byte scheme)
  167. {
  168.   x += r; y += r;
  169.  
  170.   int w = r / 4;
  171.  
  172.   int angle = 150;
  173.  
  174.   int text_colour = 0;
  175.  
  176.   int v = map(value, vmin, vmax, -angle, angle);
  177.  
  178.   byte seg = 5;
  179.   byte inc = 8;
  180.  
  181.   for (int i = -angle; i < angle; i += inc) {
  182.  
  183.     int colour = 0;
  184.     switch (scheme) {
  185.       case 0: colour = ILI9341_RED; break;
  186.       case 1: colour = ILI9341_GREEN; break;
  187.       case 2: colour = ILI9341_BLUE; break;
  188.       case 3: colour = rainbow(map(i, -angle, angle, 0, 127)); break;
  189.       case 4: colour = rainbow(map(i, -angle, angle, 63, 127)); break;
  190.       case 5: colour = rainbow(map(i, -angle, angle, 127, 63)); break;
  191.       default: colour = ILI9341_BLUE; break;
  192.     }
  193.     float sx = cos((i - 90) * 0.0174532925);
  194.     float sy = sin((i - 90) * 0.0174532925);
  195.     uint16_t x0 = sx * (r - w) + x;
  196.     uint16_t y0 = sy * (r - w) + y;
  197.     uint16_t x1 = sx * r + x;
  198.     uint16_t y1 = sy * r + y;
  199.  
  200.     // Calculate pair of coordinates for segment end
  201.     float sx2 = cos((i + seg - 90) * 0.0174532925);
  202.     float sy2 = sin((i + seg - 90) * 0.0174532925);
  203.     int x2 = sx2 * (r - w) + x;
  204.     int y2 = sy2 * (r - w) + y;
  205.     int x3 = sx2 * r + x;
  206.     int y3 = sy2 * r + y;
  207.  
  208.     if (i < v) { // Fill in coloured segments with 2 triangles
  209.       tft.fillTriangle(x0, y0, x1, y1, x2, y2, colour);
  210.       tft.fillTriangle(x1, y1, x2, y2, x3, y3, colour);
  211.       text_colour = colour; // Save the last colour drawn
  212.     }
  213.     else // Fill in blank segments
  214.     {
  215.       tft.fillTriangle(x0, y0, x1, y1, x2, y2, ILI9341_GREY);
  216.       tft.fillTriangle(x1, y1, x2, y2, x3, y3, ILI9341_GREY);
  217.     }
  218.   }
  219.  
  220.   // Convert value to a string
  221.   char buf[10];
  222.   byte len = 4; if (value > 999) len = 5;
  223.   dtostrf(value, len, 0, buf);
  224.  
  225.   tft.fillRect (40, 160, 80, 40, ILI9341_BLACK);
  226.   tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
  227.   tft.setCursor(50, 170);
  228.   tft.setTextColor(ILI9341_WHITE);
  229.   tft.setTextSize(2);
  230.   tft.print(bme.readTemperature());
  231.  
  232.   tft.fillRect (200, 160, 80, 40, ILI9341_BLACK);
  233.   tft.setCursor(210, 170);
  234.   tft.setTextColor(ILI9341_WHITE);
  235.   tft.setTextSize(2);
  236.   tft.print(bme.readHumidity());
  237.  
  238.   return x + r;
  239. }
  240.  
  241. // #########################################################################
  242. // Return a 16 bit rainbow colour
  243. // #########################################################################
  244. unsigned int rainbow(byte value)
  245. {
  246.   // Value is expected to be in range 0-127
  247.   // The value is converted to a spectrum colour from 0 = blue through to 127 = red
  248.  
  249.   byte red = 0; // Red is the top 5 bits of a 16 bit colour value
  250.   byte green = 0;// Green is the middle 6 bits
  251.   byte blue = 0; // Blue is the bottom 5 bits
  252.  
  253.   byte quadrant = value / 32;
  254.  
  255.   if (quadrant == 0) {
  256.     blue = 31;
  257.     green = 2 * (value % 32);
  258.     red = 0;
  259.   }
  260.   if (quadrant == 1) {
  261.     blue = 31 - (value % 32);
  262.     green = 63;
  263.     red = 0;
  264.   }
  265.   if (quadrant == 2) {
  266.     blue = 0;
  267.     green = 63;
  268.     red = value % 32;
  269.   }
  270.   if (quadrant == 3) {
  271.     blue = 0;
  272.     green = 63 - 2 * (value % 32);
  273.     red = 31;
  274.   }
  275.   return (red << 11) + (green << 5) + blue;
  276. }
  277.  
  278. // #########################################################################
  279. // Return a value in range -1 to +1 for a given phase angle in degrees
  280. // #########################################################################
  281. float sineWave(int phase) {
  282.   return sin(phase * 0.0174532925);
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement