0% found this document useful (0 votes)
59 views4 pages

Tong Hop

The document contains examples of using semaphores, mutexes, notifications, counting semaphores and queues in Arduino. It shows how to create and use these synchronization objects to control access to shared resources like LEDs from multiple tasks and interrupts.

Uploaded by

ThẤt RÍ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views4 pages

Tong Hop

The document contains examples of using semaphores, mutexes, notifications, counting semaphores and queues in Arduino. It shows how to create and use these synchronization objects to control access to shared resources like LEDs from multiple tasks and interrupts.

Uploaded by

ThẤt RÍ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

///////////////////////////Semaphore//////////////////////////////////////

#include<Arduino.h>
#define led 2
#define butt 23
void IRAM_ATTR processInterrupt();
void xTask(void* pvPramas);
SemaphoreHandle_t xSemaphore;
int ledStatus=0;
void setup()
{
pinMode(led,OUTPUT);
pinMode(butt,INPUT);
xTaskCreate(xTask,"xTask",1024,NULL,1,NULL);
xSemaphore= xSemaphoreCreateBinary();
}
void loop(){}
void xTask()
{
while(1)
{
if(xSemaphoreTake(xSemaphore,10)==pdTRUE)
{
ledStatus=1-ledStatus;
digitalWrite(led,ledStatus);
}}}
void IRAM_ATTR processInterrupt()
{
BaseType_t x = pdFALSE;
xSemaphoreGiveFromISR(xSemaphore,&x);}
////////////////////////////////MUTEX////////////////////////////////
//--MUTEX--//
#include <Arduino.h>
#define LED GPIO_NUM_22
#define Button GPIO_NUM_5
SemaphoreHandle_t xMutex;
void vtaskOne(void *pvParams);
void IRAM_ATTR processInterrupt();
int ledStatus=0;
void setup()
{
Serial.begin(9600);
pinMode(LED,OUTPUT);
pinMode(Button,INPUT);
attachInterrupt(digitalPinToInterrupt(Button), processInterrupt, RISING);
xMutex = xSemaphoreCreateMutex();
xTaskCreate(vtaskOne, "One", 2048, NULL, 1, NULL);

void loop()
{}
void vtaskOne(void *pvParams)
{
while(1)
{
if(xMutex != NULL)
{
if(xSemaphoreTake(xMutex, portMAX_DELAY))
{
ledStatus=1-ledStatus;
digitalWrite(LED,ledStatus);
}}}}
void IRAM_ATTR processInterrupt()
{
BaseType_t x = pdFALSE;
xSemaphoreGiveFromISR(xMutex,&x );
}
///////////////////////////////////////////NOTI//////////////////////
#include <Arduino.h>
#define button1 GPIO_NUM_4
#define led1 GPIO_NUM_2
#define led2 GPIO_NUM_23

int ledStatus=0;
void vTaskProcessLed(void* pvParams);
void IRAM_ATTR processInterruptone();
TaskHandle_t noti;

void setup()
{
pinMode(button1, INPUT_PULLDOWN);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led1, ledStatus);
attachInterrupt(digitalPinToInterrupt(button1), processInterruptone, RISING);// dăng kí intrtt
xTaskCreate(vTaskProcessLed,"Task processLed",1024, NULL, 1, &noti);
}
void loop()
{}
void IRAM_ATTR processInterruptone () // vaof hamf dat biet de xu lys ngat
{
vTaskNotifyGiveFromISR(noti,NULL);
}
void vTaskProcessLed(void* pvParams)
{
ulong notifyValue = 0;
while(1)
{
notifyValue = ulTaskNotifyTake(pdTRUE,portMAX_DELAY);
if(notifyValue==1)
{
ledStatus = 1-ledStatus;
digitalWrite(led1, ledStatus );
}}}
//////////////////////////////////////////////////COUNTING 10/////////////
#include <Arduino.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define BUTTON1 22
#define buttton 5
#define led 21
#define led2 19
struct infor_adc
{
uint8_t channelID;
uint8_t Value;

};
volatile bool isButtonpressed=false;
int ledstatus=0;
void vtaskprocessled(void*pvparam);
void IRAM_ATTR processInterrupt2();
SemaphoreHandle_t xSemaphore;
TickType_t delay_x = pdMS_TO_TICKS (0);
void setup()
{
Serial.begin(9600);
pinMode(BUTTON1,INPUT_PULLDOWN);
pinMode(buttton,INPUT_PULLDOWN);
pinMode(led,OUTPUT);
pinMode(led2,OUTPUT);
digitalWrite(led,ledstatus);
attachInterrupt(digitalPinToInterrupt(buttton),processInterrupt2,RISING);
xTaskCreate(vtaskprocessled,"task process led",2048,NULL,1,NULL);
xSemaphore=xSemaphoreCreateCounting(10,0);
}

void loop()
{}
void vtaskprocessled(void* pvparam)
{
BaseType_t b;
b=false;
int i;
while(1)
{
for( i=0;i<10;i++){
xSemaphoreTake( xSemaphore,portMAX_DELAY );
}
ledstatus=1-ledstatus;
digitalWrite(led,ledstatus);}}
void IRAM_ATTR processInterrupt2()
{
BaseType_t a;
a=false;
xSemaphoreGiveFromISR (xSemaphore,&a);
}
//////////////////////////////////////////QUEUE 2 button/////////////////
#include <Arduino.h>
#define LED1 GPIO_NUM_22
#define LED2 GPIO_NUM_23
#define ButtonOne GPIO_NUM_2
#define ButtonTwo GPIO_NUM_5
void vBlinkled(void *pvParams);
void IRAM_ATTR processInterruptOne();
void IRAM_ATTR processInterruptTwo();
int ledstatus = 0;
QueueHandle_t xQueue;
void setup()
{
pinMode(LED2, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(ButtonOne, INPUT_PULLDOWN);
pinMode(ButtonTwo, INPUT_PULLDOWN);
digitalWrite(LED2, ledstatus);
attachInterrupt(digitalPinToInterrupt(ButtonOne), processInterruptOne, RISING);
attachInterrupt(digitalPinToInterrupt(ButtonTwo), processInterruptTwo, RISING);
xTaskCreate(vBlinkled, "Task 1", 1024, NULL, 1, NULL);
xQueue = xQueueCreate(10, sizeof(uint32_t));
}
void loop()
{}
void vBlinkled(void *pvParams)
{
int ulVar;
while(1)
{
xQueueReceive(xQueue, &ulVar, portMAX_DELAY);
if (ulVar == 0)
{
//ledstatus = 1 - ledstatus;
ledstatus = ulVar;
digitalWrite(LED1, ledstatus);
}
if (ulVar == 1)
{
//ledstatus = 1 - ledstatus;
ledstatus = ulVar;
digitalWrite(LED2, ledstatus);
}
}
}
void IRAM_ATTR processInterruptOne()
{
BaseType_t temp;
temp = pdTRUE;
int ulVar = 0;
xQueueSendFromISR(xQueue, &ulVar, &temp);
}
void IRAM_ATTR processInterruptTwo()
{
BaseType_t temp;
temp = pdTRUE;
int ulVar = 1;
xQueueSendFromISR(xQueue, &ulVar, &temp);
}
/////////////////////////////////////QUEUE///////////////////////////
#include <Arduino.h>
#define LED1 GPIO_NUM_22
#define LED2 GPIO_NUM_23
#define Button GPIO_NUM_5
void vBlinkled(void *pvParams);
void IRAM_ATTR processInterrupt();
int ledstatus = 0;
QueueHandle_t xQueue;
void setup()
{
pinMode(LED2, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(Button, INPUT_PULLDOWN);
digitalWrite(LED2, ledstatus);
attachInterrupt(digitalPinToInterrupt(Button), processInterrupt, RISING);
xTaskCreate(vBlinkled, "Task 1", 1024, NULL, 1, NULL);
xQueue = xQueueCreate(10, sizeof(uint32_t));
}
void loop()
{}
void vBlinkled(void *pvParams)
{
int ulVar;
while(1)
{
if (xQueueReceive(xQueue, &ulVar, portMAX_DELAY))
{
ledstatus = 1 - ledstatus;
digitalWrite(LED2, ledstatus);
delay(1000);
digitalWrite(LED2, LOW);
}}}
void IRAM_ATTR processInterrupt()
{
BaseType_t temp;
temp = pdTRUE;
int ulVar = 1;
xQueueSendFromISR(xQueue, &ulVar, &temp);
}

You might also like