Project

Implementation of RTOS to control LED Brightness

Aim: Create 4 Tasks of any Priority, out of which the task having the highest priority shall be running.

Problem Statement: With help of an LDRA sensor control the automatic switching of an LED (ON and OFF) with a Buzzer to indicate low illumination, Also control the Brightness of an LED with the Help of a potentiometer.

Requirements:

1. Arduino Mega 2560

3. LED

4. Buzzer

5. LDR Sensor

6. Potentiometer

7. Connecting wires

8. USB cable

Code:

#include
#include

// Define a struct
struct pinRead
{
int pin; // analog TASK identifier
int value; // sensor output value
};

//Declaration of structure objects
struct pinRead currentPinRead1;
struct pinRead currentPinRead2;
struct pinRead currentPinRead3;

QueueHandle_t structQueue;

void setup()
{
Serial.begin(9600);//Start sending data on the Serial Monitor
pinMode(A0,INPUT);//Declaring the Analog Pin A0 as INPUT
pinMode(A1,INPUT);//Declaring the Analog Pin A1 as INPUT
pinMode(3,OUTPUT);//Declaring the Digital Pin 3 as OUTPUT
pinMode(2,OUTPUT);//Declaring the Digital Pin 2 as OUTPUT

// create structure Queue
structQueue = xQueueCreate(7, sizeof(struct pinRead));

if (structQueue != NULL) {

xTaskCreate(TaskLightReadPin0, //Function pointer
"AnalogReadPin0", // Task name
128, // Stack size
NULL, //parameter
3, // Priority
NULL);//TaskHandler

xTaskCreate(TaskPotentiometerReadPin1, // Task function
"AnalogReadPin1", // Task name
128, // Stack size
NULL, //parameter
1, // Priority
NULL);//Task handler

xTaskCreate(TaskBuzzerwritePin2, // Task function
"DigitalWritePin2", // Task name
128, // Stack size
NULL,//parameter
4, // Priority
NULL);//Task Handler

xTaskCreate(TaskLEDwritePin3, // Task function
"DigitalWritePin3", // Task name
128, // Stack size
NULL, //parameter
2, // Priority
NULL); //Task Handler
}

vTaskStartScheduler();//The Scheduler will perform the function of an infinite loop

}

void loop()
{

}

// Light Sensor function definition
void TaskLightReadPin0(void *pvParameters)
{
(void) pvParameters;//type casting pointer to void type

for (;;)
{
currentPinRead1.pin = 0; // assign value '0' to pin element of struct
currentPinRead1.value = analogRead(A0); // Read adc value from A0 channel and store it in value element of struct
currentPinRead2.value = analogRead(A0); // Read adc value from A0 channel and store it in value element of struct
xQueueSend(structQueue, &currentPinRead1.value, portMAX_DELAY); //write struct message to queue
Serial.println("Inside the Task1");//print Task1 on serial monitor
delay(1000);
taskYIELD(); //terminate the task and inform scheduler about it
}
}

//Potentiometer function definition
void TaskPotentiometerReadPin1(void *pvParameters)
{
(void) pvParameters;//typecasting pointer to void type

for (;;)
{

currentPinRead3.pin = 1; // assign value '1' to pin element of struct
currentPinRead3.value = analogRead(A1); // Read adc value from A1 channel and store it in value element of struct
xQueueSend(structQueue, &currentPinRead3.value, portMAX_DELAY); //write struct message to queue
Serial.println("Inside the Task2"); //print Task2 on serial monitor
delay(1000);
taskYIELD(); //terminate the task and inform scheduler about it
}
}


//Buzzer value function definition
void TaskBuzzerwritePin2(void *pvParameters)
{
(void) pvParameters;//typecasting pointer to void type

for (;;)
{
// Read value from value element of struct and write it on digital pin 2
if (xQueueReceive(structQueue, &currentPinRead1.value, portMAX_DELAY) == pdPASS)
{
if((currentPinRead1.value)<=150)
{
digitalWrite(2,HIGH);
Serial.println("Inside the Task3-Buzzer ON ");
delay(1000);
}
else
{
digitalWrite(2,LOW);
Serial.println("Inside the Task3-Buzzer OFF ");
delay(1000);
}
taskYIELD(); //terminate the task and inform scheduler about it
}
}
}

//LED funtion Definition
void TaskLEDwritePin3(void *pvParameters)
{
(void) pvParameters;//typecasting pointer to void type
for (;;)
{
// Read value from value element of struct and write it on digital pin 3
if (xQueueReceive(structQueue, &currentPinRead2.value, portMAX_DELAY) == pdPASS)
{
if(currentPinRead2.value<=150)//Check for illumination value
{
digitalWrite(3, HIGH);
Serial.println("LED ON");
delay(1000);
analogWrite(3,currentPinRead3.value);
delay(1000);
}
else
{
digitalWrite(3, LOW);
Serial.println("LED OFF");
taskYIELD(); //terminate the task and inform scheduler about it
}
}
}
}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.