#include <ArduinoFreeRTOS.h> #include <semphr.h> #include <LedControl.h> #define DINPIN…
- User
#include <Arduino_FreeRTOS.h> #include <semphr.h> #include <LedControl.h>
#define DIN_PIN 11 #define CS_PIN 10 #define CLK_PIN 9
#define LED1_PIN 13 #define LED2_PIN 12
#define POT_PIN A0
LedControl lc = LedControl(DIN_PIN, CLK_PIN, CS_PIN, 4);
SemaphoreHandle_t led1Mutex; SemaphoreHandle_t led2Mutex; SemaphoreHandle_t lcdMutex; TaskHandle_t task1, task2, taskLcd;
void toggleLed1(void*) { while(1) { xSemaphoreTake(led1Mutex, portMAX_DELAY); digitalWrite(LED1_PIN, !digitalRead(LED1_PIN)); vTaskDelay(500 / portTICK_PERIOD_MS); xSemaphoreGive(led1Mutex); } }
void toggleLed2(void*) { while(1) { xSemaphoreTake(led2Mutex, portMAX_DELAY); digitalWrite(LED2_PIN, !digitalRead(LED2_PIN)); vTaskDelay(1300 / portTICK_PERIOD_MS); xSemaphoreGive(led2Mutex); } }
void controlLcd(void*) {
while(1) {
xSemaphoreTake(lcdMutex, portMAX_DELAY);
for(int r=0; r<8; r++) { for(int i=0; i<4; i++) { for(int c=0; c<8; c++) { lc.setLed(i, r, c, true); int sensorValue = analogRead(POT_PIN); int delayMs = map(sensorValue, 0, 1023, 50, 500); vTaskDelay(delayMs / portTICK_PERIOD_MS); lc.setLed(i, r, c, false); } } }
xSemaphoreGive(lcdMutex); }
}
void setup() {
led1Mutex = xSemaphoreCreateMutex(); led2Mutex = xSemaphoreCreateMutex(); lcdMutex = xSemaphoreCreateMutex();
pinMode(LED1_PIN, OUTPUT); pinMode(LED2_PIN, OUTPUT); pinMode(POT_PIN, INPUT);
// inicjalizacja MAX7219
xTaskCreate(toggleLed1, "", 100, NULL, 1, &task1); xTaskCreate(toggleLed2, "", 100, NULL, 1, &task2); xTaskCreate(controlLcd, "", 200, NULL, 2, &taskLcd);
vTaskStartScheduler(); }
void loop() {}
dodaj komentarze do kodu opisujące co robi, tylko nie za dużo i po polsku bez polskich znaków
Assistant
#include <Arduino_FreeRTOS.h> #include <semphr.h> // includes for FreeRTOS and semaphores #include <LedControl.h> // includes for controlling the LED strip #define DIN_PIN 11 // pin for the data input of the MAX7219 controller #define CS_PIN 10 // pin for the chip select of the MAX7219 controller #define CLK_PIN 9 // pin for the clock input of the MAX7219 controller #define LED1_PIN 13 // pin for the first LED on the strip #define LED2_PIN 12 // pin for the second LED on the strip #define POT_PIN A0 // pin for the potentiometer, which will control the brightness of the LEDs
LedControl lc = LedControl(DIN_PIN, CLK_PIN, CS_PIN, 4); // creates an instance of the LedControl class, which controls the LED strip SemaphoreHandle_t led1Mutex; // creates a semaphore for mutual exclusion when accessing the first LED SemaphoreHandle_t led2Mutex; // creates a semaphore for mutual exclusion when accessing the second LED SemaphoreHandle_t lcdMutex; // creates a semaphore for mutual exclusion when accessing the LCD display TaskHandle_t task1, task2, taskLcd; // creates handles for the tasks that control the LEDs and the LCD display
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.