/*
Consider you are in and industry and you are working on a flow line where you have 3 Tasks.
Task 1, Task 2, Task 3.
Task 1 takes 5s to complete.
Task 2 takes 15s to complete.
Task 3 takes 10s to complete.
You have to include a counter so that whenever the task is completed, counter is incremented.
Each task should be completed 10 times.
Consider that the product from these three tasks have to be combined together, so it is mandatory that all the tasks are completed at the same time, so we dont have any idle time.
Also, after 10 products, a batch is completed. So, indicate the batch completion with an LED. You have to complete 3 batches, so indicate this with 3 separate LEDs.
You have to include a real time clock, along with this, the start time and end time of each batch should also be noted. Also, the start and end time of each task should also be noted and displayed.
A stopwatch should also be included in the project to view the time of tasks.
*/
//choose core of the esp32
#include <RTClib.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
RTC_DS1307 rtc; //creating object of rtc
DateTime start_time_batch1;
DateTime end_time_batch1;
DateTime start_time_batch2;
DateTime end_time_batch2;
DateTime start_time_batch3;
DateTime end_time_batch3;
DateTime start_time_task1;
DateTime end_time_task1;
DateTime start_time_task2;
DateTime end_time_task2;
DateTime start_time_task3;
DateTime end_time_task3;
#define SDA_PIN_1 23
#define SCL_PIN_1 19
#define OLED_ADDR_1 0x3C
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_ADDR_1, SDA_PIN_1, SCL_PIN_1);
#if CONFIG_FREERTOS_UNICORE
static const BaseType_t app_cpu=0;
#else
static const BaseType_t app_cpu=1;
#endif
#define no_of_batches 3
#define Products_in_a_batch 10
//define time for each task
#define task1_t 100
#define task2_t 300
#define task3_t 200
// initialize pins to indicate task completion
static const int led_pin1=4;
static const int led_pin2=0;
static const int led_pin3=2;
//initialize counters to count each task completion
int counter1=0;
int counter2=0;
int counter3=0;
int prod=0;
int batch_counter=0;
// initialize task handles
static TaskHandle_t task_1= NULL ;
static TaskHandle_t task_2= NULL ;
static TaskHandle_t task_3= NULL ;
static TaskHandle_t task_4= NULL ;
//funciton for task 1
void startTask1(void *parameter)
{
while(1){
counter1++;
if(counter1==1){
start_time_task1 = rtc.now();
}
vTaskDelay( task1_t/ portTICK_PERIOD_MS);//time on task1
if (counter1==1){
end_time_task1 = rtc.now();
start_time_task2 = rtc.now();}
vTaskDelay( task3_t/portTICK_PERIOD_MS);//time on task2
end_time_task2 = rtc.now();
if (counter1 % Products_in_a_batch==0) // when batch of 10 completes led pin will set to high
{
counter1=0;
}
}
}
void startTask2(void *parameter){
while(1){
counter2++;
vTaskDelay( task2_t/ portTICK_PERIOD_MS);
if (counter2 % Products_in_a_batch==0){
counter2=0;
}
}
}
void startTask3(void *parameter){
int prevcount1=0;
int prevcount2=0;
while(1){
if(prod==0 && counter1==0)
{start_time_batch1 = rtc.now();}
if (counter1==counter2 && counter1!=prevcount1 && counter2!=prevcount2) {
prevcount1=counter1;
prevcount2=counter2;
Serial.print("C1:");
Serial.println(counter1);
Serial.print("C2:");
Serial.println(counter2);
prod++;
Serial.print("Product: ");
Serial.println(prod);
if (prod==(Products_in_a_batch*1))
{
end_time_batch1 = rtc.now();
start_time_batch2 = rtc.now();
}
if (prod==(Products_in_a_batch*2))
{
end_time_batch2 = rtc.now();
start_time_batch3 = rtc.now();
}
if (prod==(Products_in_a_batch*3))
{
end_time_batch3 = rtc.now();
}
if(prod%Products_in_a_batch==0)
{
batch_counter++;
}
if (batch_counter==1 && prod==10 )
{ Serial.println("Batch One completed:");
Serial.print("batch start time 1: ");
Serial.print(start_time_batch1.hour(), DEC);
Serial.print(':');
Serial.print(start_time_batch1.minute(), DEC);
Serial.print(':');
Serial.print(start_time_batch1.second(), DEC);
Serial.println();
Serial.print("Batch End time 1: ");
Serial.print(end_time_batch1.hour(), DEC);
Serial.print(':');
Serial.print(end_time_batch1.minute(), DEC);
Serial.print(':');
Serial.print(end_time_batch1.second(), DEC);
Serial.println();
digitalWrite(led_pin1 , HIGH);
}
if (batch_counter==2 && prod==20)
{
Serial.println("Batch 2 completed:");
Serial.print("batch 2 start time : ");
Serial.print(start_time_batch2.hour(), DEC);
Serial.print(':');
Serial.print(start_time_batch2.minute(), DEC);
Serial.print(':');
Serial.print(start_time_batch2.second(), DEC);
Serial.println();
Serial.print("Batch 2 End time : ");
Serial.print(end_time_batch2.hour(), DEC);
Serial.print(':');
Serial.print(end_time_batch2.minute(), DEC);
Serial.print(':');
Serial.print(end_time_batch2.second(), DEC);
Serial.println();
digitalWrite(led_pin2 , HIGH);
}
if (batch_counter==3 && prod==30)
{
Serial.println("Batch 3 completed:");
Serial.print("batch 3 start time : ");
Serial.print(start_time_batch3.hour(), DEC);
Serial.print(':');
Serial.print(start_time_batch3.minute(), DEC);
Serial.print(':');
Serial.print(start_time_batch3.second(), DEC);
Serial.println();
Serial.print("Batch 3 End time : ");
Serial.print(end_time_batch3.hour(), DEC);
Serial.print(':');
Serial.print(end_time_batch3.minute(), DEC);
Serial.print(':');
Serial.print(end_time_batch3.second(), DEC);
Serial.println();
digitalWrite(led_pin3 , HIGH);
batch_counter=0;
vTaskDelay( 300/ portTICK_PERIOD_MS);
digitalWrite(led_pin1 , LOW);
digitalWrite(led_pin2 , LOW);
digitalWrite(led_pin3 , LOW);
vTaskSuspend(task_1);
vTaskSuspend(task_2);
vTaskSuspend(task_3);
}
}
vTaskDelay( 300/ portTICK_PERIOD_MS);
}
}
void startTask4(void *parameter)
{
while(1){
Serial.print("Task 1 start time : ");
Serial.print(start_time_task1.hour(), DEC);
Serial.print(':');
Serial.print(start_time_task1.minute(), DEC);
Serial.print(':');
Serial.print(start_time_task1.second(), DEC);
Serial.println();
vTaskDelay( task1_t/ portTICK_PERIOD_MS);
Serial.print("Task 1 End time : ");
Serial.print(end_time_task1.hour(), DEC);
Serial.print(':');
Serial.print(end_time_task1.minute(), DEC);
Serial.print(':');
Serial.print(end_time_task1.second(), DEC);
Serial.println();
}
}
void setup() {
Serial.begin(9600);
if (!oled.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR_1, SDA_PIN_1, SCL_PIN_1)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
delay(1000);
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(0, 2); // set position to display (x,y)
oled.println("Hello"); // set text
oled.display();
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
}
pinMode(led_pin1, OUTPUT);
pinMode(led_pin2, OUTPUT);
pinMode(led_pin3, OUTPUT);
xTaskCreatePinnedToCore( startTask1,
"task 1",
2000,
NULL,
2,
&task_1,
app_cpu);
xTaskCreatePinnedToCore( startTask2,
"task 2",
2000,
NULL,
2,
&task_2,
app_cpu);
xTaskCreatePinnedToCore( startTask3,
"task 3",
2000,
NULL,
1,
&task_3,
app_cpu);
xTaskCreatePinnedToCore(startTask4 ,
"oled_display",
2000,
NULL,
0,
&task_4,
app_cpu);
}
void loop() {
}
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK
led1:A
led1:C
led3:A
led3:C
led2:A
led2:C
rtc1:GND
rtc1:5V
rtc1:SDA
rtc1:SCL
rtc1:SQW
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
oled1:GND
oled1:VCC
oled1:SCL
oled1:SDA