• Arduino Nano 33 IoT with WiFi
• iOS current
• Blynk server
This code causes a freeze and Blynk server connection timeout/loss when I toggle the Segmented Switch in the Blynk App; all the other button and data widgets are functioning, so, I get into the WRITE function. I have searched for a solution but have not kicked up one, please point me towards appropriate information or provide suggestions related to a solution.
My void loop() is clean with only run and timer
Specifically, the V30 event is causing the failure and the testing of the V30 event with “ASx.enableBulb()” event does not occur but another LED indicator related to the timer stops.
Thanks for any guidance
BLYNK_WRITE_DEFAULT() {
//change button color
Blynk.setProperty(V1, "color", "#BF23B8");
Blynk.setProperty(V3, "color", "#3FBF23");
Blynk.setProperty(V4, "color", "#EAFF33");
Blynk.setProperty(V7, "color", "#EEEDCD");
int pin = request.pin; //which pin is handled
ledState = param.asInt();
digitalWrite((led_pins[pin-1]), ledState); //pin-1 reregisters vPin to array value
//special cases of all off (==0) does not handle some LEDs on/off
if (pin == V0) {
for (int ii=0;ii<lpSize;ii++) {
digitalWrite((led_pins[ii]), ledState);
Blynk.virtualWrite((ii+1), ledState);
}
}
//special case of gain setting on virtual pin V30
if (pin == V30) {
ASx.enableBulb(); //test flow
switch (param.asInt()) {
case 1: {
GAIN = 0;
ASx.setGain(GAIN);
break;
}
case 2: {
GAIN = 1;
ASx.setGain(GAIN);
break;
}
case 3: {
GAIN = 2;
ASx.setGain(GAIN);
break;
}
case 4: {
GAIN = 3;
ASx.setGain(GAIN);
break;
}
}
ASx.disableBulb(); //test flow
}
}
But the if statement will only evaluate to true if the BLYNK_WRITE_DEFAULT() callback was triggered by a widget attached to V0.
The problem occurs wen the widget attached to pin V30 is activated.
The way that the virtual pin numbers are evaluated is very messy in this code, and it had me scratching my head for a while…
“V30” obviously isn’t a valid integer value, so this comparison shouldn’t work, but it does because of the entries in the BlynkHandlers.h library file contains entries like this for every virtual pin…
#define V30 30
I suspect that the problem is this…
but without seeing more of the code to understand what the ASx object is or does it’s impossible to be sure.
Also, I’m not really a great fan of using the BLYNK_WRITE_DEFAULT() callback in situations where it’s then necessary to have if or switch/case statements to choose which code segments to execute, but it may be an appropriate thing to do if there are a great many virtual pins which require the same response. Once again, it’s not possible to tell from a snippet of code like this one.
For clarity:
V0-V7 are Button(s), that operate light sources, work fine (as long as V30 is not toggled)
V20-V25 are Value Display(s) from a sensor that work fine (as long as V30 is not toggled)
I discovered, but have not found documentation, that the vPins are declared in the header as integers - however, I have found some differentials in handling these in ‘for’ loops.
The basics of the code references, key being the SparkFun AS726x library:
I am coloring outside the lines of my profession in bioanalytics, so, I am happy to follow suggestions for ‘better’ code structure/methods other than BLYNK_WRITE_DEFAULT() callback or switch/case statements.
commands work fine outside of the Segmented Switch (V30).
I added the ASx.enableBulb() after I encountered the issue, to assess condition entry.
I have tried increasing the sample time for data (6 values) from 0.5sec to 1.0sec and there was same issue of timeout.
I was hoping for some simple miscode issue that I have been overlooking…
but it appears to just come down to the V30 call (and depending on the library to #define V30).
As said previously, all other functions seem to work fine until the V30 call. I assume there is always the potential that my lack of understanding and inclusion of supporting code can cover up my failings.
@SeanH please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```
The ‘normal’ method is to use oneBLYNK_WRITE(vPin) callback per virtual pin in use. This then runs code which is explicit to that particular virtual pin, so negates the need for all of the if/switch case code that is necessary with the BLYNK_WRITE_DEFAULT callback.
The potential drawback to the ‘normal’ approach is that if you have multiple widgets that do very similar things then there can be quite a lot of code duplication within the various BLYNK_WRITE(vPin) callbacks. This can usually be negated by calling a function from within the BLYNK_WRITE(vPin) callback, so the only time that I really use BLYNK_WRITE_DEFAULT is is very specific and specialised situations such as this one…
The normal approach would be to use serial print messages.
It’s impossible to say when you just post snippets of code instead of the full sketch.
Mark it solved - and it was a simple code issue.
Structuring the digitalWrite into a conditional was the solution:
if ((pin > V0) && (pin < V8)) {
digitalWrite((led_pins[pin-1]), ledState); //pin-1 reregisters vPin to array value
}
I was writing 1, 2, 3, or 4 to V30 – ne fonctionne pas!
This code works A-OK
//When any App button with a virtual pin is pushed
BLYNK_WRITE_DEFAULT() {
//change button color
Blynk.setProperty(V1, "color", "#BF23B8");
Blynk.setProperty(V3, "color", "#3FBF23");
Blynk.setProperty(V4, "color", "#EAFF33");
Blynk.setProperty(V7, "color", "#EEEDCD");
int pin = request.pin; //which pin is handled
ledState = param.asInt();
if ((pin > V0) && (pin < V8)) {
digitalWrite((led_pins[pin-1]), ledState); //pin-1 reregisters vPin to array value
}
//special cases of all off (==0) does not handle some LEDs on/off
if (pin == V0) {
for (int ii=0;ii<lpSize;ii++) {
digitalWrite((led_pins[ii]), ledState);
Blynk.virtualWrite((ii+1), ledState);
}
}
//special case of gain setting on virtual pin V30
if (pin == V30) {
ASx.enableBulb(); //test flow
switch (param.asInt()) {
case 1: {
Gain = 0;
ASx.setGain(Gain);
break;
}
case 2: {
Gain = 1;
ASx.setGain(Gain);
break;
}
case 3: {
Gain = 2;
ASx.setGain(Gain);
break;
}
case 4: {
Gain = 3;
ASx.setGain(Gain);
break;
}
}
ASx.disableBulb(); //test flow
}
}