const int maxLeds = 12;
const int ledPins[maxLeds] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
enum Mode { NONE,
SERIELL,
PARALLEL };
Mode currentMode = NONE;
struct LedTask {
int ledIndex;
unsigned long duration; // in ms
unsigned long startTime;
bool active;
};
LedTask tasks[maxLeds];
int taskCount = 0;
bool busy = false;
String inputString = "";
bool stringComplete = false;
void setup() {
Serial.begin(9600);
for (int i = 0; i < 12; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW);
}
Serial.println("ready");
}
void loop() {
if (stringComplete && !busy) {
parseInput(inputString);
inputString = "";
stringComplete = false;
}
if (busy) {
if (currentMode == SERIELL) {
handleSerialMode();
} else if (currentMode == PARALLEL) {
handleParallelMode();
}
}
readSerial();
}
void readSerial() {
while (Serial.available()) {
char inChar = (char)Serial.read();
if (inChar == '\n' || inChar == '\r' || inChar == 'E') {
if (inputString.length() > 0) {
stringComplete = true;
}
} else {
inputString += inChar;
}
}
}
void parseInput(String str) {
// Beispiel: S;L1:1000;L5:3000;L2:1000 oder P;L1:5000;L5:3000;L2:1000
// S;L1:1000;L5:3000;L2:2500;L4:8000;L8:4000;L6:5000;L7:7000;L3:2000
// P;L1:1000;L5:3000;L2:2500;L4:8000;L8:4000;L6:5000;L7:7000;L3:2000
str.trim();
if (str.length() < 2) return;
char modeChar = str.charAt(0);
if (modeChar == 'S') currentMode = SERIELL;
else if (modeChar == 'P') currentMode = PARALLEL;
else return;
// Reset tasks
taskCount = 0;
busy = true;
// Teile String in Teile nach ';'
int start = 2; // nach "S;" oder "P;"
while (start < str.length()) {
int sep = str.indexOf(';', start);
String part;
if (sep == -1) {
part = str.substring(start);
start = str.length();
} else {
part = str.substring(start, sep);
start = sep + 1;
}
part.trim();
if (part.length() == 0) continue;
// Format Lx:yy
int colon = part.indexOf(':');
if (colon == -1) continue;
String ledStr = part.substring(0, colon);
String timeStr = part.substring(colon + 1);
ledStr.trim();
timeStr.trim();
if (ledStr.length() < 2) continue;
if (ledStr.charAt(0) != 'L') continue;
int ledNum = ledStr.substring(1).toInt();
if (ledNum < 1 || ledNum > 12) continue;
int durationMS = timeStr.toInt();
if (durationMS <= 0) continue;
tasks[taskCount].ledIndex = ledNum - 1;
tasks[taskCount].duration = (unsigned long)durationMS;// * 1000;
tasks[taskCount].active = false;
tasks[taskCount].startTime = 0;
taskCount++;
if (taskCount >= 12) break;
}
if (taskCount == 0) {
busy = false;
currentMode = NONE;
}
}
void handleSerialMode() {
static int currentTask = 0;
if (currentTask >= taskCount) {
// Alle Aufgaben fertig
Serial.println("fertig");
busy = false;
currentMode = NONE;
currentTask = 0;
return;
}
LedTask &task = tasks[currentTask];
if (!task.active) {
digitalWrite(ledPins[task.ledIndex], HIGH);
task.startTime = millis();
task.active = true;
if (currentTask == 0) Serial.println("working");
} else {
if (millis() - task.startTime >= task.duration) {
digitalWrite(ledPins[task.ledIndex], LOW);
currentTask++;
}
}
}
void handleParallelMode() {
static bool started = false;
static unsigned long startTimes = { 0 };
if (!started) {
for (int i = 0; i < taskCount; i++) {
digitalWrite(ledPins[tasks[i].ledIndex], HIGH);
tasks[i].startTime = millis();
tasks[i].active = true;
}
if (taskCount > 0) Serial.println("working");
started = true;
}
bool allDone = true;
unsigned long now = millis();
for (int i = 0; i < taskCount; i++) {
if (tasks[i].active) {
if (now - tasks[i].startTime >= tasks[i].duration) {
digitalWrite(ledPins[tasks[i].ledIndex], LOW);
tasks[i].active = false;
} else {
allDone = false;
}
}
}
if (allDone) {
Serial.println("fertig");
busy = false;
currentMode = NONE;
started = false;
}
}