I have a basic program where I am controlling an external device via a digital output on the Portenta Machine Control. The code turns on the digital output for 5 seconds, then off for 5 seconds, and repeat. During this cycle, it continuously calls Serial.println() to report some readings of its analog input ports (a placeholder string is used in the code below).
Due to safety reasons, I need the PLC to immediately turn off the digital output in the case that the USB cable is disconnected. However, it appears that when and only if Serial.println() is called, the Portenta Machine Control hangs and the digital output will remain in the state that was in when the USB cable was disconnected.
I have an ESP32 PLC which just resets by default if the USB is disconnected - is there some way to get the Portenta to do the same? Any help on how to prevent hanging when the USB cable is disconnected would be greatly appreciated. The code is below.
# include <Arduino_PortentaMachineControl.h>
unsigned long t0;
unsigned long t1;
unsigned long t2;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// Initialize IO
// I2C Wire
Wire.begin();
// Analog Read
MachineControl_AnalogIn.begin(SensorType::V_0_10);
// Analog Write
MachineControl_AnalogOut.begin();
MachineControl_AnalogOut.setPeriod(0, 1);
// Digital Read
MachineControl_DigitalInputs.begin();
// Digital Write
MachineControl_DigitalOutputs.begin(true);
MachineControl_DigitalOutputs.writeAll(0);
}
void loop() {
// put your main code here, to run repeatedly:
t0 = millis();
MachineControl_DigitalOutputs.write(1, HIGH);
t1 = millis();
while(millis()-t1 < 5000){
Serial.println("AAAABBBB");
delay(200);
}
MachineControl_DigitalOutputs.write(1, LOW);
t2 = millis();
while(millis()-t2 < 5000){
Serial.println("AAAABBBB");
delay(200);
}
}