void loop() {
// Handle commands received over Bluetooth Serial
if (SerialBT.available()) {
uint8_t receivedValue = SerialBT.read();
// Update PWM duty cycle with any received value (original behavior)
ledcWrite(0, receivedValue);
Serial.println("PWM duty cycle bijgewerkt: " + String(receivedValue));
// Process command bytes
if (receivedValue == 0x00) {
isPwmSweepActive = !isPwmSweepActive; // Toggle PWM sweep mode
Serial.println("Sweep-modus PWM: " + String(isPwmSweepActive ? "ON" : "OFF"));
CAN0.setRXFilter(0, 0x3EA, 0x7FF, false); // Reset CAN filter (original behavior)
} else if (receivedValue == 0x01) {
// Start EEPROM read sequence
Serial.println("start eeprom read");
isPeriodicWakeupActive = false; // Disable periodic wakeup during EEPROM read
isEepromReadActive = true;
CAN0.setRXFilter(0, 0x3E8, 0x7FF, false); // Set CAN filter for EEPROM responses
uint8_t startMsg[] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };
SerialBT.write(startMsg, sizeof(startMsg)); // Notify Bluetooth client
} else if (receivedValue == 0x02) {
// Start device programming sequence
isPeriodicWakeupActive = false; // Disable periodic wakeup
isDeviceProgrammingActive = true;
// Read parameters for programming from Bluetooth
programmingClusterID = SerialBT.read();
programmingEepromIndex = SerialBT.read();
programmingTargetValue = SerialBT.read();
Serial.println("programming angle with: C=" + String(programmingClusterID) + ", I=" + String(programmingEepromIndex) + ", V=" + String(programmingTargetValue));
} else if (receivedValue == 0x03) {
// Toggle periodic wakeup and CAN sweep modes
isPeriodicWakeupActive = !isPeriodicWakeupActive;
isCanSweepActive = !isCanSweepActive;
Serial.println("Periodic Wakeup: " + String(isPeriodicWakeupActive ? "ON" : "OFF") + ", CAN Sweep: " + String(isCanSweepActive ? "ON" : "OFF"));
}
}
// Execute active modes
if (isPwmSweepActive) {
runPwmSweep();
}
if (isEepromReadActive) {
requestEepromData();
}
if (isDeviceProgrammingActive) {
executeDeviceProgrammingSequence();
}
if (isCanSweepActive) {
performCanSweep();
}
sendPeriodicCanWakeup(); // Send periodic CAN message if active
// Check for and process incoming CAN messages
CAN_FRAME receivedCanMessage;
if (CAN0.read(receivedCanMessage)) {
printCanFrame(&receivedCanMessage);
}
}
maak van de void loop een flowchart in plantuml. De de flowchart overzichtelijk