Have you detected whether the crash happens with else if (in == 'd')
or else if (in == 'p')
?
In the Pd patch it's probably better to swap the $1 and $2 values in the messages. Have the channel set in a cold inlet and the value in a hot one and then write this message print $2c$1v
I see in the screenshot of the patch that the PWM message is not connected to [comport]. Is this for some particular reason? Maybe the 10ms interval for sending a PWM value to [comport] is a bit too fast. Did you try something slower, like 50ms?
Another thing would be to try and merge messages for digital and PWM. Since PWM happens automatically, you could append the digital pin message and send something like print $2c$1p$4c$3d
where $4 will be the digital pin channel and $3 the value.
-
Having lots of switches into Pd
-
@alexandros said:
Have you detected whether the crash happens with
else if (in == 'd')
orelse if (in == 'p')
?It happens only in
else if (in == 'p')
. When I bypass that I can update snapshot~ by 1 ms (I assume that 'c' will still be sent by 1 ms intervals).In the Pd patch it's probably better to swap the $1 and $2 values in the messages. Have the channel set in a cold inlet and the value in a hot one and then write this message
print $2c$1v
Ok I did that.
I see in the screenshot of the patch that the PWM message is not connected to [comport]. Is this for some particular reason?
It is to make sure Pd does not crash/freeze until I want to try the code.
Maybe the 10ms interval for sending a PWM value to [comport] is a bit too fast. Did you try something slower, like 50ms?
That made it work. If I use six pwm pins the limit is 150 ms before crash.
Question 1: Could there something bad that happens around
else if (in == 'p')
? In another code snippet you used this format:while(Serial.available()){ byte inByte = Serial.read(); if((inByte >= '0') && (inByte <= '9')) temporary = 10 * temporary + inByte - '0'; else{ if(inByte == 'p'){ pwmLEDvalue = temporary; temporary = 0; } else if(inByte == 'd'){ dspLEDstate = temporary; temporary = 0; } } analogWrite(pwmLED, pwmLEDvalue); digitalWrite(dspLED, dspLEDstate); }
compared to this in the current sketch:
if (Serial.available()) { static int temp; byte in = Serial.read(); if (isDigit(in)) temp = temp * 10 + in - '0'; else if (in == 'c') { channel = temp; temp = 0; } else if (in == 'd') { digitalWrite(outPins[channel], temp); temp = 0; } else if (in == 'p') { analogWrite(pwmPins[channel], temp); temp = 0; } }
Question 2: Is this crash of Pd and the computer (macOS Mojave) expected behavior in a situation like this? If it is not, maybe it should be reported as a bug. It happens consistently.
Thoughts: Could there be a problem in [comport]? This crash behavior is identical with the problems I had when using Pdunio/Firmata.
-
First of all, glad it finally worked!
Answer 1: The current code looks better than the one you posted above it (which is apparently something I've used, hopefully in my tutorial and not the book...), because analogWrite() and digitalWrite() are only called when needed. In the first code snippet both functions are called whenever there's data in the serial line, which is overhead.
Answer 2: I don't really have an answer to this. You can send an email to Pd's mailing list where Martin Peach, the developer of [comport] is active. It does sound strange that your computer crashes, as a longer delay just gives time to the Arduino to do the PWM stuff, but that has to do with the Arduino. Maybe there's something strange going on when the Arduino doesn't have enough time to do what it has to do, but I really don't know. -
@alexandros said:
Answer 1: The current code looks better than the one you posted above it (which is apparently something I've used, hopefully in my tutorial and not the book...),
It is from the tutorial and I believe it was fine in a stepwise educational context. (Why I posted it was because the variables inside the else if() are treated differently. Wild guess that it could be a problem.)
Thank you so much for that you took time to help me on this. However, this problem does not exist in the old tutorial code(!).
Look here
I send two updates per millisecond and still no crash. The Pd console reports errors. Pd still works as normal. This is the codes (Arduino and Pd) from your tutorial.
When using the new codes (both Ardunio sketch and Pd patch) Pd crash/freeze and there is no report in the pd console.