/*
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.

1 ->implement task to complete

add tasks that take time
5 sec
15 sec
10 sec

2->when one task completed a counter increments
each task count upto 10, i.e each task must be iterated/ completed 10 times

3->when task 1 task 2 and task 3 reaches 10 return a batch
when each task increments 10-> return a batch completed after every task complete by LED1,2,3
when all led blink or every task increment 10/completed 10 times, blink another led4

4->include a real time clock to note start and end time of each batch and each task

5->include a stopwatch to view time of every task(time to complete)

*/

//choose core of the esp32
#include <RTClib.h>

RTC_DS1307 rtc; //creating object of rtc
  DateTime start_time_batch1;
  DateTime end_time_batch1;
  DateTime start_time_task2;
  DateTime end_time_task2;
  DateTime start_time_task3;
  DateTime end_time_task3;

#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 product=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 ;

//funciton for task 1
void startTask1(void *parameter)
{
  
  while(1){
    counter1++;


    start_time_batch1 = rtc.now();
    Serial.print("ST batch 1: ");
    Serial.println( start_time_batch1.timestamp());
  vTaskDelay( task1_t/ portTICK_PERIOD_MS);//time on task1

  vTaskDelay( task3_t/portTICK_PERIOD_MS);//time on task2

  if (counter1 % Products_in_a_batch==0) // when batch of 10 completes led pin will set to high
  {
    end_time_batch1 = rtc.now();
        Serial.print("ET batch 1: ");
        Serial.println(end_time_batch1.timestamp());

  }
  }
}

void startTask2(void *parameter){
  
 while(1){
  counter2++;
  start_time_task2 = rtc.now();
  
 vTaskDelay( task2_t/ portTICK_PERIOD_MS);
 if (counter2 % Products_in_a_batch==0){
  end_time_task2 = rtc.now();



  }
}
}


void setup() {
  Serial.begin(9600);

  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);

}

void loop() {
  Serial.print("loop started, i am in loop");
 if (counter1==Products_in_a_batch&& 
    counter2 ==Products_in_a_batch) {
counter1=0;
counter2=0;
batch_counter++;
Serial.print("batch:");
Serial.print(batch_counter);
Serial.println("Completed");
delay(300);
if (batch_counter==1)
{
        

digitalWrite(led_pin1 , HIGH);
delay(300);
digitalWrite(led_pin1 , LOW);
}
if (batch_counter==2)
{
digitalWrite(led_pin2 , HIGH);
delay(300);
digitalWrite(led_pin2 , LOW);
}
if (batch_counter==3)
{
digitalWrite(led_pin3 , HIGH);
delay(300);
digitalWrite(led_pin3 , LOW);
batch_counter=0;

}
  }
 delay(300);
}
  
    


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
GND5VSDASCLSQWRTCDS1307+
rtc1:GND
rtc1:5V
rtc1:SDA
rtc1:SCL
rtc1:SQW
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r