#include <Arduino_FreeRTOS.h>
#include <semphr.h>
#include <string.h>
#define YellowLEDPin 8
#define BlueLEDPin 7
#define GreenLEDPin 4
#define Button1 2
#define Button2 3
struct Division{
char order[10];
char numberSecond[10];
};
SemaphoreHandle_t xSemaphoreLEDControl;
TaskHandle_t yellowLEDTaskHandle, blueLEDTaskHandle, greenLEDTaskHandle;
const char sequence[] = "YB1G1B1YG3YGB2E";
int sequenceIndex = 0;
int sequenceIndexForSequences = 0;
int numSequence = 0;
int sequencesResult = 0;
bool sequenceRunning = false;
char sequences[10];
void turnOffAllLEDs()
{
digitalWrite(YellowLEDPin, LOW);
digitalWrite(BlueLEDPin, LOW);
digitalWrite(GreenLEDPin, LOW);
}
bool checkIfIsNumber(int sequenceIndex)
{
return (sequence[sequenceIndex] >= '1' && sequence[sequenceIndex] <= '9');
}
Division checkSequences(int &sequenceIndex){
Division division[10];
bool posicaoNumero = false;
int divisionNumber = 0;
int orderIndex = 0;
int numberIndex = 0;
while(1){
if (checkIfIsNumber(sequenceIndex)) {
posicaoNumero = true; // Marca que um número foi encontrado na sequência
division[divisionNumber].numberSecond[numberIndex++] = sequence[sequenceIndex];
} else {
division[divisionNumber].order[orderIndex++] = sequence[sequenceIndex];
}
if (posicaoNumero) {
break; // Sai do loop após armazenar toda a sequência até o número
}
divisionNumber++;
delay(10); // Pequeno atraso para evitar loop muito rápido
}
return division[10];
}
int getStringValue(const char* str) {
int value = 0;
for (int i = 0; str[i] != '\0'; i++) {
value = value * 10 + (str[i] - 'A' + 1);
}
return value;
}
void generalTaskController()
{
TickType_t xLastWakeTime = xTaskGetTickCount();
Division sequencesResult = checkSequences(sequenceIndex);
int divisionCount = 0;
int length = sizeof(sequencesResult.order);
Serial.println("ALO");
while(1){
Serial.print("Sequences result - Order: ");
// Serial.print(sequencesResult.order);
// Serial.print("Sequences result - Second ");
// Serial.print(sequencesResult.numberSecond);
if(sequence[sequenceIndex] == 'E'){
Serial.println("Encontrou o caractere 'E'. Reiniciando...");
sequenceIndex = 0;
}
delay(10); // Pequeno atraso para evitar loop muito rápido
}
}
void taskYellowLED(void *pvParameters)
{
Serial.println("Yellow LED should turn on.");
digitalWrite(YellowLEDPin, HIGH);
vTaskDelay(pdMS_TO_TICKS(100));
}
void taskBlueLED(void *pvParameters)
{
Serial.println("Blue LED should turn on.");
digitalWrite(BlueLEDPin, HIGH);
vTaskDelay(pdMS_TO_TICKS(100));
}
void taskGreenLED(void *pvParameters)
{
Serial.println("Green LED should turn on.");
digitalWrite(GreenLEDPin, HIGH);
vTaskDelay(pdMS_TO_TICKS(100));
}
void setup()
{
Serial.begin(9600);
pinMode(YellowLEDPin, OUTPUT);
pinMode(BlueLEDPin, OUTPUT);
turnOffAllLEDs();
pinMode(Button1, INPUT_PULLUP);
pinMode(Button2, INPUT_PULLUP);
xSemaphoreLEDControl = xSemaphoreCreateBinary();
if (xSemaphoreLEDControl != NULL)
{
xSemaphoreGive(xSemaphoreLEDControl);
}
generalTaskController();
}
void loop() {}