01-01-2024, 14:49
Witam,
napisałem coś takiego
napisałem coś takiego
Kod:
#include <Timers.h>
#include <TimerOne.h>
#include <Wire.h>
#include <DS3231.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
int temperatura;
const byte oneWireBus = 11; // Tutaj jest określony port magistrali 1-wire.
OneWire oneWire(oneWireBus); // Tutaj definiuje się port magistrali dla biblioteki.
DallasTemperature sensors(&oneWire); // Teraz można już przekazać bibliotece obsługi termometru namiary na bibliotekę obsługi magistrali.
///////////////////////////////////////////////
//////////////////Do wyświetlacza/////////////////////////////
LiquidCrystal_I2C lcd(0x27, 20, 4);
////////////////////////////////////////////////////////
//////////////////Do Zegara/////////////////////////////
DS3231 clock;
RTCDateTime dt;
///////////////////////////////////////////////
Timers <3> akcja; // na poczatek 8 niezależnych wątków (procesów, zadań, procedur, akcji itp.)
int x = 3, y = 2; // takie zmienne pomocnicze, dla testów;
void setup()
{
//////////////////Do czyjnika/////////////////////////////
sensors.requestTemperatures();
sensors.setWaitForConversion(false);
sensors.begin();
///////////////////////////////////////////////
//////////////////Do wyświetlacza/////////////////////////////
lcd.begin();
lcd.backlight();
///////////////////////////////////////////////
//////////////////Do zegara/////////////////////////////
clock.begin();
clock.setDateTime(__DATE__, __TIME__);
// zadanie np. 0 co 1 sek
akcja.attach(0, 1000, zadanie1);
akcja.attach(1, 1000, zadanie2);
akcja.attach(2, 750, zadanie3);
}
void loop()
{
akcja.process(); // inicjalizacja lub aktualizacja wszystkich procedur(wątków, zdarzeń itp.)
if (x == 0) {
akcja.updateInterval(0, 1000);
}
if (x == 1) {
akcja.updateInterval(1, 1000);
}
if (x == 2) {
akcja.updateInterval(2, 750);
}
}
// --- poniżej przykładowe definicje zadań: ------
void zadanie1()
{
//////////////////////////////////Zegarek//////////////////////////////////////
dt = clock.getDateTime();
lcd.setCursor(0, 0);
if (dt.hour < 10 )
{
lcd.print(F(" "));
}
lcd.print(dt.hour);
lcd.print(F(":"));
if (dt.minute < 10 )
{
lcd.print(F("0"));
}
lcd.print(dt.minute);
lcd.print(F(":"));
if (dt.second < 10 )
{
lcd.print(F("0"));
}
lcd.print(dt.second);
////////////////////////////////////////////////////////////////////////////////
////////////////////////////Data do zegarka///////////////////////////////
lcd.setCursor(0, 1);
lcd.print(dt.day);
lcd.print(F("/"));
lcd.print(dt.month);
lcd.print(F("/"));
lcd.print(dt.year);
}
void zadanie2()
{
sensors.setWaitForConversion(true);
sensors.requestTemperatures();
temperatura = 10 * (sensors.getTempCByIndex(0) + 0.05);
if (temperatura < 0) // Gdy wartość jest ujemna...
{
temperatura--; // odejmij jeden ze względu na zaokrąglenia liczb ujemnych.
lcd.print(F("-")); // rysuj znak minusa.
}
else
{
lcd.print(F(" ")); // W przeciwnym razie rysuj spację.
}
temperatura = abs(temperatura); // Wyciągnij wartość bezwzględną.
if (temperatura < 100) // Jeśli temperatura będzie większa od 9 bądź mniejsza od -9 stopni...
{
lcd.print(F(" ")); // rysuj dodatkową spację.
}
lcd.setCursor(0, 2);
lcd.print(F("Temp.:"));
lcd.setCursor(0, 3);
lcd.print(float(temperatura) / 10, 1);
lcd.print((char)223);
lcd.print(F("C"));
sensors.setWaitForConversion(false);
}
void zadanie3()
{
}
// ------------------------------------------------