/*
* Use of MAX72XX, DS1307 and DTH22 components to
* print some information on the display.
*
* for more examples:
* https://github.com/MajicDesigns/MD_Parola/tree/main/examples
* https://github.com/MajicDesigns/MD_MAX72XX/tree/main/examples
*/
// Header file includes
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <DHT.h>
#include <SPI.h>
#include <Wire.h>
#include "Font7Seg.h"
// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may
// need to be adapted
#define HARDWARE_TYPE MD_MAX72XX::PAROLA_HW
//#define HARDWARE_TYPE MD_MAX72XX::FC16_HW alterar na IDE do arduino
#define MAX_DEVICES 4 // Define the number of displays connected
#define CLK_PIN 23 // CLK or SCK
#define DATA_PIN 19 // DATA or MOSI
#define CS_PIN 22 // CS or SS
#define SPEED_TIME 75 // Speed of the transition
#define PAUSE_TIME 0
#define MAX_MESG 20
// These are for the temperature
#define DHTPIN 2
#define DHTTYPE DHT22
#define TIMEDHT 1000
char szTime[9]; // mm:ss\0
char szMesg[MAX_MESG + 1] = "";
float humidity, celsius, fahrenheit;
char buffer[20]; // Make sure the buffer is large enough
uint8_t degC[] = { 6, 3, 3, 56, 68, 68, 68 }; // Deg C
uint8_t degF[] = { 6, 3, 3, 124, 20, 20, 4 }; // Deg F
uint8_t clear = 0x00;
uint32_t timerDHT = TIMEDHT;
DHT dht(DHTPIN, DHTTYPE);
// Hardware SPI connection
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
// Code for get Temperature
void getTemperature()
{
// Wait for a time between measurements
if ((millis() - timerDHT) > TIMEDHT) {
// Update the timer
timerDHT = millis();
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
celsius = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
fahrenheit = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again)
if (isnan(humidity) || isnan(celsius) || isnan(fahrenheit)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
}
}
void setup(void)
{
Serial.begin(115200);
Serial.println(F("DHTxx test!"));
Wire.begin();
P.begin();
dht.begin();
floatToASCII(celsius, buffer, 2);
}
void loop(void)
{
Serial.print (celsius);
getTemperature();
P.displayAnimate();
delay(2000);
P.print(buffer);
}
void floatToASCII(float num, char *buffer, int precision)
{
// Handle the case where the number is negative
if (num < 0)
{
*buffer++ = '-';
num = -num;
}
// Extract the integer and fractional parts
int intPart = (int)num;
float fracPart = num - (float)intPart;
// Convert the integer part to ASCII
int length = snprintf(buffer, 10, "%d", intPart);
buffer += length;
// Add the decimal point
*buffer++ = '.';
// Convert the fractional part to ASCII with the specified precision
for (int i = 0; i < precision; i++)
{
fracPart *= 10;
int digit = (int)fracPart;
*buffer++ = '0' + digit;
fracPart -= digit;
}
// Null-terminate the string
*buffer = '\0';
}