#include <Arduino.h>
#include "DHTesp.h"
#define Sprintln(a) (Serial.println(a))
#define Sprint(a) (Serial.print(a))
#define ADC_READ_A0 analogRead(34) //
#define SWITCH_S1 (25) //
#define BUTTON_START digitalRead(SWITCH_S1)== 1 //
#define DHT_PIN (15) //D15 gpio004 esp32
#define LED_1 (2) //Pin D2
#define LED_2 (4) //Pin D4
unsigned int count_0 = 0; //increment cnt ++
unsigned int count_1 = 0;
unsigned int count_2 = 0;
unsigned int count_3 = 0;
unsigned int count_4 = 0;
unsigned int count_5 = 0;
unsigned int count_6 = 0;
unsigned int count_7 = 0;
unsigned char Blink_led_1 = 0; //LED_1 Toggle
unsigned char Blink_led_2 = 0; //LED_2 Toggle
unsigned int even = 0; //
unsigned long lastime = 0; //
volatile unsigned int ADval[4]; //
// ---- limits for sampling period -----------
const float T0MAX = 1; // controller (sec)
const float T0MIN = 0.001;
const int T0MSMAX = 1000; // controller (ms)
const int T0MSMIN = 1;
const int T1MSMAX = 1000; // serial bulk communication (ms)
const int T1MSMIN = 1;
const int T2MSMAX = 1000; // OLED
const int T2MSMIN = 1;
const int T3MSMAX = 1000; // RGB LED
const int T3MSMIN = 1;
const int T4MSMAX = 1000; // command
const int T4MSMIN = 1;
const int T5MSMAX = 10000; // NETPIE freeboard
const int T5MSMIN = 1;
const int T6MSMAX = 20000; // NETPIE feed
const int T6MSMIN = 1;
const int T7MSMAX = 5000; // NETPIE feed
const int T7MSMIN = 1;
float T = 0.08; // sampling period
float tdata = 0; // time data sent to output
int T0_ms, T1_ms, T2_ms, T3_ms, T4_ms, T5_ms, T6_ms, T7_ms;
int T0ticks, T1ticks, T2ticks, T3ticks, T4ticks, T5ticks, T6ticks, T7ticks;
int Tloop_ms = 500; // loop period
// -----------FreeRTOS handles and flags-----------------
TaskHandle_t xTask0h = NULL, xTask1h = NULL, xTask2h = NULL, xTask3h = NULL, xTask4h = NULL;
TaskHandle_t xTask5h = NULL, xTask6h = NULL, xTask7h = NULL;
QueueHandle_t xQueue_y = NULL, xQueue_u = NULL;
bool xQueueOK;
portMUX_TYPE myMutex = portMUX_INITIALIZER_UNLOCKED;
DHTesp dhtSensor;
/**************************************************************************
// create tasks and queues
//* Create the task, storing the handle. */
//xReturned = xTaskCreate(
//vTaskCode, /* Function that implements the task. */
//"NAME", /* Text name for the task. */
//STACK_SIZE, /* Stack size in words, not bytes. */
//( void * ) 1, /* Parameter passed into the task. */
//tskIDLE_PRIORITY, /* Priority at which the task is created. */
//&xHandle ); /* Used to pass out the created task's handle. */
/***************************************************************************/
void freertos_init(void)
{
xQueueOK = 0; // start wit false
xQueue_y = xQueueCreate(1000, sizeof(float));
xQueue_u = xQueueCreate(1000, sizeof(float));
if ((xQueue_y == NULL) | (xQueue_u == NULL))
Serial.println("Error creating the queue");
else xQueueOK = 1;
T0_ms = 1000 * T;
T0ticks = pdMS_TO_TICKS(T0_ms); // period of Task0 in number of ticks
// initialize delays to tasks
T1_ms = 1000;
T2_ms = 1000;
T3_ms = 1000;
T4_ms = 1000;
T5_ms = 1000;
T6_ms = 1000;
T7_ms = 1000;
T1ticks = pdMS_TO_TICKS(T1_ms);
T2ticks = pdMS_TO_TICKS(T2_ms);
T3ticks = pdMS_TO_TICKS(T3_ms);
T4ticks = pdMS_TO_TICKS(T4_ms);
T5ticks = pdMS_TO_TICKS(T5_ms);
T6ticks = pdMS_TO_TICKS(T6_ms);
T7ticks = pdMS_TO_TICKS(T7_ms);
xTaskCreatePinnedToCore(Task0, "Task 0", 1000, NULL, 1, &xTask0h, 0);
xTaskCreatePinnedToCore(Task1, "Task 1", 1000, NULL, 1, &xTask1h, 1);
xTaskCreatePinnedToCore(Task4, "Task 2", 1000, NULL, 2, &xTask4h, 1);
// the following tasks may be suspended if flags are set to 0
xTaskCreatePinnedToCore(Task2, "Task 3", 1000, NULL, 1, &xTask2h, 0);
xTaskCreatePinnedToCore(Task3, "Task 4", 1000, NULL, 2, &xTask3h, 1);
xTaskCreatePinnedToCore(Task5, "Task 5", 1000, NULL, 1, &xTask5h, 1);
xTaskCreatePinnedToCore(Task6, "Task 6", 1000, NULL, 2, &xTask6h, 1);
xTaskCreatePinnedToCore(Task7, "Task 7", 1000, NULL, 1, &xTask7h, 1);
}
void Task0(void *parameter)
{
TickType_t xLastWakeTime;
xLastWakeTime = xTaskGetTickCount();
for (;;)
{
// Sprint("Task 0 count : ");
//Sprintln(count_0 ++);
vTaskDelayUntil(&xLastWakeTime, T0ticks); //set period
}
vTaskDelete(NULL);
}
void Task1(void *parameter)
{
for (;;)
{
if(even)
{
Blink_led_1 ^= 1;
digitalWrite(LED_1, Blink_led_1);
}
else
{
Blink_led_1 = 0;
digitalWrite(LED_1,LOW);
}
vTaskDelay(T1ticks);
}
vTaskDelete(NULL);
}
void Task2(void *parameter)
{
for (;;)
{
if(even)
{
Blink_led_2 ^= 1;
digitalWrite(LED_2, Blink_led_2);
}
else
{
Blink_led_2 = 0;
digitalWrite(LED_2,LOW);
}
vTaskDelay(T2ticks);
}
vTaskDelete(NULL);
}
void Task3(void *parameter)
{
for (;;)
{
//Sprint("Task 3 count : ");
//Sprintln(count_3++);
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Sprintln("Temp"+ String(data.temperature,2)+"°c");
Sprintln("Humidity:"+ String(data.humidity,1)+"%");
Sprintln("---");
vTaskDelay(T3ticks);
}
vTaskDelete(NULL);
}
void Task4(void *parameter)
{
for (;;)
{
int AdjustVR= map(ADC_READ_A0,0,4095,0,100);
ADval[3]=ADval[2];
ADval[2]=ADval[1];
ADval[1]=ADval[0];
ADval[0]=AdjustVR;
int adcval = (ADval[3]+ADval[2]+ADval[1]+ADval[0])>>2;
Sprint("Task 4 AdjustVR : ");
Sprint(adcval);
Sprintln("%");
vTaskDelay(T4ticks);
}
vTaskDelete(NULL);
}
void Task5(void *parameter)
{
for (;;)
{
if(BUTTON_START)
{
long now = millis();
if((now-lastime) > 5)
{
lastime = 0;
if(even==0)
{
even = 1;
}
else if (even == 1)
{
even = 0;
}
// even ^ =1;
}
}
vTaskDelay(T5ticks);
}
vTaskDelete(NULL);
}
void Task6(void *parameter)
{
for (;;)
{
Sprint("Task 6 count : ");
Sprintln(count_6++);
vTaskDelay(T6ticks);
}
vTaskDelete(NULL);
}
void Task7(void *parameter)
{
for (;;)
{
Sprint("Task 7 count : ");
Sprintln(count_7++);
vTaskDelay(T7ticks);
}
vTaskDelete(NULL);
}
void setup()
{
Serial.begin(9600);
pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(SWITCH_S1, INPUT);
pinMode(ADC_READ_A0, INPUT);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
freertos_init();
}
void loop()
{
}