#include "Arduino_FreeRTOS.h"
#include "task.h"
#include "semphr.h"
// include the library code:
#include <LiquidCrystal.h>
#include <SimpleDHT.h>
#include <Servo.h>
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
//const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#include <math.h>
#define SEND_GANTT 1
#define AOUT 9
#define LED_OUT 7
//jepetos
unsigned long Programa; //dias igual a 21 dias.
unsigned long cont;
unsigned long time;// = 21;// ou 504 horas.
int bpm;
unsigned int vira;
void seleciona();
long alterVeloc = !0;
int Programador = 0;
int programa;
int pos;
int umid;
int programGalinha = 21;// ou 504 horas.
int bpmGalinha = 333;// interval at which to blink (milliseconds) 333 para configurar 180 bpm (frequência cardiaca da galina).
int umdGalinha = 29;
int programCodorna = 17;// ou 408 horas.
int programGanso = 34;// ou 816 horas.
int programPavao = 28;// ou 672 horas.
int programPeru = 28;// ou 672 horas.
int programAngola = 28;// ou 672 horas.
int programPato = 28;// ou 672 horas.
int programMarreco = 30;// ou 720 horas.
int programFaisao = 28;// ou 672 horas.
int programPerdizBr = 21;// ou 504 horas.
int programPerdizCk = 24;// ou 576 horas.
int programEMA = 42;// ou 1008 horas.
int programMUTUM = 30;// ou 720 horas.
int programPombo = 17;// ou 408 horas.
int programMacucu = 18;// ou 432 horas.
int programAvestruz = 42;// ou 1008 horas.
int pinDHT22 = A4;
SimpleDHT22 dht22(pinDHT22);
Servo motor;
//Tasks
TaskHandle_t ledTaskH;
TaskHandle_t sineTaskH;
TaskHandle_t lcdTaskH;
//Mutex
SemaphoreHandle_t SerialMutex;
void sendGantt(const char *name, unsigned int stime, unsigned int etime) {
if (xSemaphoreTake(SerialMutex, portMAX_DELAY) == pdTRUE) { //Solicita Mutex
Serial.print("\t\t");
Serial.print(name);
Serial.print(": ");
Serial.print(stime);
Serial.print(", ");
Serial.println(etime);
xSemaphoreGive(SerialMutex); //Libera Mutex
}
}
void setup() {
//Inicializa Serial
Serial.begin(115200);
// set up the LCD's number of columns and rows:
lcd.clear();
lcd.begin(16, 2);
Serial.print("1s is ");
Serial.print(configTICK_RATE_HZ);
Serial.print(" ticks at ");
Serial.print(F_CPU);
Serial.print(" Hz\n\n");
// set the digital pin as output:
motor.attach(9, 0, 0);
motor.write(pos);
#if (defined(SEND_GANTT) && (SEND_GANTT==1))
Serial.println("gantt\n\tdateFormat x\n\ttitle A gant diagram");
#endif
SerialMutex = xSemaphoreCreateMutex();
//Cria tarefa ledTask
xTaskCreate(ledTask, //Funcao
"ledTask", //Nome
128, //Pilha
NULL, //Parametro
1, //Prioridade
&ledTaskH);
//Cria tarefa sineTask
xTaskCreate(SineTask,
"SineTask",
128,
NULL,
1,
&sineTaskH);
}
void loop() {
// Nada é feito aqui, Todas as funções são feitas em Tasks
}
/* LedTask
Pisca Led com frequência de 1Hz
*/
void ledTask(void *arg) {
unsigned int stime;
pinMode(LED_OUT, OUTPUT);
while (1) {
stime = millis();
digitalWrite(LED_OUT, HIGH); //Liga Led
vTaskDelay(pdMS_TO_TICKS(500)); //Espera 0.5s
digitalWrite(LED_OUT, LOW); //Desliga Led
vTaskDelay(pdMS_TO_TICKS(500)); //Espera 0.5s
#if (defined(SEND_GANTT) && (SEND_GANTT==1))
sendGantt("Led", stime, millis()); //Envia Informações pela Serial
#endif
}
//O codigo nunca deve chegar aqui
vTaskDelete(NULL); //Deleta a Task atual
}
/* SineTask
Descreve uma senoide de 2Hz na saida analogica
*/
void SineTask(void *arg) {
unsigned int stime = 0, etime = 0;
pinMode(AOUT, OUTPUT);
unsigned int outpv = 0;
unsigned int period = 0;
while (1) {
stime = millis();
for (period = 0; period < 16; ++period) {
etime = millis();
outpv = (unsigned int)((sin(2 * PI * 2 * (etime - stime) * 0.001) + 1) * 127.5); /*Senoide = seno(2*PI*Freq*t)*/
analogWrite(AOUT, outpv);
vTaskDelay(pdMS_TO_TICKS(33)); //Espera 33 milisegundos (32 ms)
}
#if (defined(SEND_GANTT) && (SEND_GANTT==1))
sendGantt("Sine", stime, millis());
#endif
}
//O codigo nunca deve chegar aqui
vTaskDelete(NULL); //Deleta a Task atual
}
void lcdTask(void *args) {
unsigned int stime = 0;
unsigned int falta = 1;
unsigned long minutos;
unsigned long horas;
unsigned long segundos;
unsigned long dia;// = 86400000;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
while (1) {
stime = millis();
//relógio é a divisão do quantador millis().
cont = millis();
segundos = cont / falta;
minutos = segundos / 60;
horas = minutos / 60;
dia = horas / 24;
/* if (vira = horas / 4) {
vira = time;
time = pos += 40;
motor.write(time);
//Servos de rotação contínua responderão à função writeMicrosecond de maneira análoga à função write.
//myservo.writeMicroseconds(1500); // set servo to mid-point
}
if (dia >= programa) {
lcd.clear();
lcd.noBlink();
lcd.setCursor(0, 1);
lcd.print("Ovos ECLODINDO");
delay(3000);
// Turn on the blinking cursor:
lcd.blink();
lcd.setCursor(0, 0);
lcd.print("Ovos ECLODINDO");
delay(3000);
return;
}*/
//set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(segundos);
lcd.setCursor(6, 1);
lcd.print(minutos);
lcd.setCursor(10, 1);
lcd.print(horas);
lcd.setCursor(14, 1);
lcd.print(dia);
/*lcd.noBlink();
lcd.setCursor(0, 1);
lcd.print("F:");
lcd.print(programa - dia);
lcd.print("d H:");
lcd.print(horas);
lcd.noBlink();
lcd.setCursor(11, 1);
lcd.print(" P:");
lcd.print(alterVeloc);*/
#if (defined(SEND_GANTT) && (SEND_GANTT==1))
sendGantt("lcd", stime, millis());
#endif
}
//O codigo nunca deve chegar aqui
vTaskDelete(NULL); //Deleta a Task atual
}