BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Shorten code

#1

Hi guys, have a question for you, have 16 or more virtual led, and i can write:

“WidgetLCD lcd(V30);
WidgetLCD lcd(V31);
…” for 16 times.

how can I shorten the code?
TY

0 Likes

#2

I’m not sure you can. You’re actually declaring WidgetLCD variables. And it wouldn’t be,

WidgetLCD lcd(V30);
WidgetLCD lcd(V31);
...
WidgetLCD lcd(V45);

but, rather,

WidgetLCD lcd0(V30);
WidgetLCD lcd1(V31);
...
WidgetLCD lcd15(V45);

You can try this,

WidgetLCD lcd0(V30), lcd1(V31), /* ..., */ lcd15(V45);

Personally, I find the first example more readable. I’m big on readability.

1 Like

#3

yes sorry i wrote wrong, is correct lcd0, lcd1…
Ok work it!
Thank you

0 Likes

#4

If you’re adventurous, you can try this …

WidgetLCD* lcd[16];

void setup(void) {
   for (int i = 0; i < 16; i++)
      lcd[i] = new WidgetLCD(i + 30);
}

I assume you’re using the LCD widget “advanced” mode. You’d apply the LCD widget clear and print commands like this,

   lcd[0]->clear();
   lcd[0]->print(0, 0, "Hello");

Note: This uses V30 - V45 as in the examples above.

Joe

1 Like

#5

LEDs or LCDs? (16 lcd? :scream:)

0 Likes

#6

He referenced “LCD” more times than he referenced “LED”, so I took him to mean LCD. :wink:

0 Likes

#7

With so many LCDs will be running out of virtual power quickly :laughing:

0 Likes

#8

My example works with LED’s, too. Just change the C’s and c’s to E’s and e’s, respectively, …

WidgetLED* led[16];

void setup(void) {
   for (int i = 0; i < 16; i++)
      led[i] = new WidgetLED(i + 30);
}

And,

   led[0]->on();
   led[0]->off();
   led[0]->setValue(127);
1 Like

#9

If you have that many Virtual LED’s it would probably be simpler to control them directly with Blynk.virtualWrite(vPin, value) commands. No defining required and the value would act like a PWM intensity with a range of 0(OFF) to 255(ON).

And the for() loop method would work just as well with this single command.

I have controlled a few Virtual LED’s this way before :stuck_out_tongue_winking_eye:

0 Likes

#10

Yes, we’ll have to see what @Andrewa80 was after. From his OP, I assumed he was after LCD, rather than LED. Even with LCD, there’s “simple” and “advanced”. The fact that he was declaring sixteen WidgetLCD instances, I assumed he was after advanced.

0 Likes

#11

It is hard to know… on one hand the OP referenced both, the defining works with both, but 16 LCD widgets?? Without accounting for vertical scrolling and assuming full visibility, one can only fit 4 on a tab. I just can’t think of a use case for 4 tabs of LCDs when even a single Terminal would fit more text.

Artistic desires perhaps?

OP has been seen in the forum 5 hours ago but didn’t comment, so let the guessing continue :stuck_out_tongue:

0 Likes

#12

Ahahah, omg sorry yesterday I was too tired… I have 16 led…

1 Like

#13

:rofl::rofl:
Andrea, do you know that you don’t need to declare the widgetLEDs ? :wink:

0 Likes