Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <SPI.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_BME280.h>
- #define BME_SCK 52
- #define BME_MISO 50
- #define BME_MOSI 51
- #define BME_CS 40
- #define SEALEVELPRESSURE_HPA (1013.25)
- Adafruit_BME280 bme(BME_CS);
- unsigned long delayTime;
- #include "Adafruit_GFX.h"
- #include "Adafruit_ILI9341.h"
- #include "SPI.h"
- #define TFT_DC 9
- #define TFT_CS 10
- #define RED2RED 0
- #define GREEN2GREEN 1
- #define BLUE2BLUE 2
- #define BLUE2RED 3
- #define GREEN2RED 4
- #define RED2GREEN 5
- #define ILI9341_GREY 0x2104
- Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
- uint32_t runTime = -99999;
- int Temp = 0;
- int Humid = 0;
- #include "RTClib.h"
- RTC_PCF8523 rtc;
- char daysOfTheWeek[7][12] = {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"};
- void setup(void) {
- tft.begin();
- tft.setRotation(1);
- tft.fillScreen(ILI9341_BLACK);
- tft.setCursor(2, 90);
- tft.setTextColor(ILI9341_WHITE);
- tft.setTextSize(2);
- tft.print("Temperature") ;
- tft.setCursor(200, 90);
- tft.setTextColor(ILI9341_WHITE);
- tft.setTextSize(2);
- tft.print("Humidite") ;
- while (!Serial);
- Serial.println(F("BME280 test"));
- unsigned status;
- status = bme.begin();
- Serial.begin(57600);
- #ifndef ESP8266
- while (!Serial); // wait for serial port to connect. Needed for native USB
- #endif
- if (! rtc.begin()) {
- Serial.println("Couldn't find RTC");
- Serial.flush();
- abort();
- }
- if (! rtc.initialized() || rtc.lostPower()) {
- Serial.println("RTC is NOT initialized, let's set the time!");
- // When time needs to be set on a new device, or after a power loss, the
- // following line sets the RTC to the date & time this sketch was compiled
- //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- // This line sets the RTC with an explicit date & time, for example to set
- // January 21, 2014 at 3am you would call:
- // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
- //
- // Note: allow 2 seconds after inserting battery or applying external power
- // without battery before calling adjust(). This gives the PCF8523's
- // crystal oscillator time to stabilize. If you call adjust() very quickly
- // after the RTC is powered, lostPower() may still return true.
- }
- // When time needs to be re-set on a previously configured device, the
- // following line sets the RTC to the date & time this sketch was compiled
- rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- // This line sets the RTC with an explicit date & time, for example to set
- // January 21, 2014 at 3am you would call:
- // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
- }
- void loop() {
- DateTime now = rtc.now();
- //HEURES,MINUTES,SECONDES
- tft.fillRect (0, 0, 320, 40, ILI9341_BLACK);
- tft.setCursor(70, 5);
- tft.setTextColor(ILI9341_WHITE);
- tft.setTextSize(4, 5);
- if (now.hour() < 10) {
- tft.print('0');
- }
- tft.print(now.hour(), DEC);
- tft.print(':');
- if (now.minute() < 10) {
- tft.print('0');
- }
- tft.print(now.minute(), DEC);
- tft.print(':');
- if (now.second() < 10) {
- tft.print('0');
- }
- tft.print(now.second(), DEC);
- //JOURS
- tft.fillRect (0, 60, 120, 20, ILI9341_BLACK);
- tft.setCursor(5, 55);
- tft.setTextColor(ILI9341_WHITE);
- tft.setTextSize(2);
- tft.print(daysOfTheWeek[now.dayOfTheWeek()]);
- //DATE
- tft.fillRect (190, 60, 320, 20, ILI9341_BLACK);
- tft.setCursor(200, 55);
- tft.setTextColor(ILI9341_WHITE);
- tft.setTextSize(2);
- if (now.day() < 10) {
- tft.print('0');
- }
- tft.print(now.day(), DEC);
- tft.print('/');
- if (now.month() < 10) {
- tft.print('0');
- }
- tft.print(now.month(), DEC);
- tft.print('/');
- tft.print(now.year(), DEC);
- tft.println();
- delay(1000);
- if (millis() - runTime >= 1000L) { // Execute every 2s
- runTime = millis();
- int xpos = 15, ypos = 115, gap = 24, radius = 64;
- ringMeter(Temp, -0, 85, xpos, ypos, radius, "C", GREEN2RED);
- xpos = 180, ypos = 115, gap = 24, radius = 64;
- ringMeter(Humid, -0, 100, xpos, ypos, radius, "%", BLUE2RED);
- }
- }
- int ringMeter(int value, int vmin, int vmax, int x, int y, int r, char *units, byte scheme)
- {
- x += r; y += r;
- int w = r / 4;
- int angle = 150;
- int text_colour = 0;
- int v = map(value, vmin, vmax, -angle, angle);
- byte seg = 5;
- byte inc = 8;
- for (int i = -angle; i < angle; i += inc) {
- int colour = 0;
- switch (scheme) {
- case 0: colour = ILI9341_RED; break;
- case 1: colour = ILI9341_GREEN; break;
- case 2: colour = ILI9341_BLUE; break;
- case 3: colour = rainbow(map(i, -angle, angle, 0, 127)); break;
- case 4: colour = rainbow(map(i, -angle, angle, 63, 127)); break;
- case 5: colour = rainbow(map(i, -angle, angle, 127, 63)); break;
- default: colour = ILI9341_BLUE; break;
- }
- float sx = cos((i - 90) * 0.0174532925);
- float sy = sin((i - 90) * 0.0174532925);
- uint16_t x0 = sx * (r - w) + x;
- uint16_t y0 = sy * (r - w) + y;
- uint16_t x1 = sx * r + x;
- uint16_t y1 = sy * r + y;
- // Calculate pair of coordinates for segment end
- float sx2 = cos((i + seg - 90) * 0.0174532925);
- float sy2 = sin((i + seg - 90) * 0.0174532925);
- int x2 = sx2 * (r - w) + x;
- int y2 = sy2 * (r - w) + y;
- int x3 = sx2 * r + x;
- int y3 = sy2 * r + y;
- if (i < v) { // Fill in coloured segments with 2 triangles
- tft.fillTriangle(x0, y0, x1, y1, x2, y2, colour);
- tft.fillTriangle(x1, y1, x2, y2, x3, y3, colour);
- text_colour = colour; // Save the last colour drawn
- }
- else // Fill in blank segments
- {
- tft.fillTriangle(x0, y0, x1, y1, x2, y2, ILI9341_GREY);
- tft.fillTriangle(x1, y1, x2, y2, x3, y3, ILI9341_GREY);
- }
- }
- // Convert value to a string
- char buf[10];
- byte len = 4; if (value > 999) len = 5;
- dtostrf(value, len, 0, buf);
- tft.fillRect (40, 160, 80, 40, ILI9341_BLACK);
- tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
- tft.setCursor(50, 170);
- tft.setTextColor(ILI9341_WHITE);
- tft.setTextSize(2);
- tft.print(bme.readTemperature());
- tft.fillRect (200, 160, 80, 40, ILI9341_BLACK);
- tft.setCursor(210, 170);
- tft.setTextColor(ILI9341_WHITE);
- tft.setTextSize(2);
- tft.print(bme.readHumidity());
- return x + r;
- }
- // #########################################################################
- // Return a 16 bit rainbow colour
- // #########################################################################
- unsigned int rainbow(byte value)
- {
- // Value is expected to be in range 0-127
- // The value is converted to a spectrum colour from 0 = blue through to 127 = red
- byte red = 0; // Red is the top 5 bits of a 16 bit colour value
- byte green = 0;// Green is the middle 6 bits
- byte blue = 0; // Blue is the bottom 5 bits
- byte quadrant = value / 32;
- if (quadrant == 0) {
- blue = 31;
- green = 2 * (value % 32);
- red = 0;
- }
- if (quadrant == 1) {
- blue = 31 - (value % 32);
- green = 63;
- red = 0;
- }
- if (quadrant == 2) {
- blue = 0;
- green = 63;
- red = value % 32;
- }
- if (quadrant == 3) {
- blue = 0;
- green = 63 - 2 * (value % 32);
- red = 31;
- }
- return (red << 11) + (green << 5) + blue;
- }
- // #########################################################################
- // Return a value in range -1 to +1 for a given phase angle in degrees
- // #########################################################################
- float sineWave(int phase) {
- return sin(phase * 0.0174532925);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement