Shorten code

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?

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

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

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]->print(0, 0, "Hello");

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


1 Like

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

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

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

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);


1 Like

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:

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.

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:

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

1 Like

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