C++ Blynk (Legacy) - Code Examples for Basic Tasks

#5 Matrix of Randomly Flashing & Coloured Virtual RGB LEDs

I posted this last year…but am tossing it in here to keep my group of sketches up to date.

Sorry to those not on Local Server… it is not cheap on the energy, however nothing in it is non-recyclable… 72 LED widgets @ 200 energy means you need 14,400 units of energy to load this.

This code is compiled on a Wemos D1 mini with OTA programming functionality built in… but it basically should work on most any ESP8266.

image

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include <ESP8266mDNS.h>  // For OTA
#include <WiFiUdp.h>  // For OTA
#include <ArduinoOTA.h>  // For OTA

#define BLYNK_NO_BUILTIN  // Disable built-in analog & digital pin operations
#define BLYNK_MAX_READBYTES 1024
#define BLYNK_MSG_LIMIT 0
#define BLYNK_HEARTBEAT 60

BlynkTimer timer;

char auth[] = "xxxxxxxxxx";
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxxx";
//char server[] = "xxx.xxx.xxx.xxx";  // IP for your Local Server
char server[] = "blynk-cloud.com";  // URL for Blynk Cloud Server
int port = 8080;
String HEXstring;
int randLED;
int randIntensity;
int rrr, ggg, bbb;
int RandomTimer;



void setup() {
  WiFi.begin(ssid, pass); 
  // Preset for use on a Local Server... adjust as required for Cloud Server
  Blynk.config(auth, server, port);
  Blynk.connect();
  for (int i = 0; i <= 71; i++) {
    Blynk.setProperty(i, "label", " ");  // Set LED Label to blank
    Blynk.setProperty(i, "color", "#000000");  // Set LED colour to black
    Blynk.virtualWrite(i, 255);  // Set LED to full intensity
  }
  // You may need to adjust this timer higher, particularly if using Cloud Server
  RandomTimer = timer.setInterval(15L, RGBMatrix); 
 
  ArduinoOTA.setHostname("ESP8266 vRGB Matrix");  // For OTA
  ArduinoOTA.begin();  // For OTA
}



void loop() {
  Blynk.run();
  timer.run();
  ArduinoOTA.handle();
}



void RGBMatrix() {
  randLED = random(72);
  //randIntensity = random(72, 256);
  rrr = random(256);
  ggg = random(256);
  bbb = random(256);
  String strRED = String(rrr, HEX);  // Convert RED DEC to HEX.
  if (rrr < 16) {
    strRED = String("0" + strRED);  // Buffer with 0 if required.
  }  // END if
  String strGRN = String(ggg, HEX);  // Convert GREEN DEC to HEX.
  if (ggg < 16)  {
    strGRN = String("0" + strGRN);  // Buffer with 0 if required.
  }  // END if
  String strBLU = String(bbb, HEX);  // Convert BLUE DEC to HEX.
  if (bbb < 16)  {
    strBLU = String("0" + strBLU);  // Buffer with 0 if required.
  }  // END if
  String HEXstring = String("#" + strRED + strGRN + strBLU);  // Combine HEX fragments.
  Blynk.setProperty(randLED, "color", HEXstring);  // Set LED Label to HEX colour
  //Blynk.virtualWrite(randLED, randIntensity);
}
5 Likes