#define BLYNK_TEMPLATE_ID "TMPL6pCaFDCix"
#define BLYNK_TEMPLATE_NAME "IOT RGB BLYNK"
#define BLYNK_AUTH_TOKEN "_8IIQyHBJnnk7KkgxlT-NQbAlLA8sk7I"
#define BLYNK_PRINT Serial
#include <Adafruit_NeoPixel.h>
#include <SPI.h>
#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
#include <WiFiClient.h>
#define PIN 14 // DIN PIN (GPIO14)
#define NUMPIXELS 16 // Number of your LED
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
const byte interruptPin = 0;
//Non-Blocking
unsigned long previousMillis = 0;
int theaterIndex = 0;
unsigned long prevAnimTime = 0;
int rainbowIndex = 0;
int rainbowCycleIndex = 0;
int theaterColorIndex = 0;
int state = 0;
int R = 255;
int G = 255;
int B = 255;
BLYNK_WRITE(V4)
{
// uint8_t newState = param.asInt();
// state = newState;
neoState();
}
BLYNK_WRITE(V3)
{
R = param[0].asInt();
G = param[1].asInt();
B = param[2].asInt();
Blynk.virtualWrite(V1, R);
Blynk.virtualWrite(V2, G);
Blynk.virtualWrite(V3, B);
}
BLYNK_WRITE(V0)
{
R = param.asInt();
}
BLYNK_WRITE(V1)
{
G = param.asInt();
}
BLYNK_WRITE(V2)
{
B = param.asInt();
}
void neoState()
{
state++;
if (state > 5) state = 0;
}
void setup()
{
Serial.begin(115200);
Serial.println("hello");
pinMode(interruptPin, INPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), neoState, CHANGE);
// attachInterrupt(digitalPinToInterrupt(interruptPin), neoState, FALLING);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
pixels.begin();
}
void loop()
{
Blynk.run();
lcdShow();
//Non-Blocking
switch (state)
{
case 0:
for (int i = 0; i < NUMPIXELS; i++)
{
pixels.setPixelColor(i, pixels.Color(R, G, B));
}
pixels.show();
break;
case 1:
theaterChase(pixels.Color(R, G, B), 50);
break;
case 2:
colorWipe(pixels.Color(255, 0, 0), 50); // Red
colorWipe(pixels.Color(0, 255, 0), 50); // Green
colorWipe(pixels.Color(0, 0, 255), 50); // Blue
break;
case 3:
nonBlockingRainbow(20);
break;
case 4:
nonBlockingRainbowCycle(20);
break;
case 5:
nonBlockingTheaterChaseRainbow(50);
break;
default:
break;
}
// switch (state)
// {
// case 0:
// for (int i = 0; i < NUMPIXELS; i++)
// {
// pixels.setPixelColor(i, pixels.Color(R, G, B));
// pixels.show();
// }
// break;
// case 1:
// theaterChase(pixels.Color(R, G, B), 50);
// break;
// case 2:
// colorWipe(pixels.Color(255, 0, 0), 50); // Red
// colorWipe(pixels.Color(0, 255, 0), 50); // Green
// colorWipe(pixels.Color(0, 0, 255), 50); // Blue
// break;
// case 3:
// rainbow(20);
// break;
// case 4:
// rainbowCycle(20);
// break;
// case 5:
// theaterChaseRainbow(50);
// break;
// }
}
void lcdShow()
{
switch (state)
{
case 0:
Blynk.virtualWrite(V5, "MANUAL");
Serial.println("MANUAL");
break;
case 1:
Blynk.virtualWrite(V5, "THEATER");
Serial.println("THEATER");
break;
case 2:
Blynk.virtualWrite(V5, "COLOR WIPE");
Serial.println("COLOR WIPE");
break;
case 3:
Blynk.virtualWrite(V5, "RAINBOW");
Serial.println("RAINBOW ");
break;
case 4:
Blynk.virtualWrite(V5, "RAINBOW CYCLE");
Serial.println("RAINBOW CYCLE");
break;
case 5:
Blynk.virtualWrite(V5, "THEATER RAINBOW");
Serial.println("THEATER RAINBOW");
break;
}
}
// {
// if (state == 0)
// {
// Blynk.virtualWrite(V5, " MANUAL ");
// Serial.println(" MANUAL ");
// }
// else if (state == 1)
// {
// Blynk.virtualWrite(V5, " THEATER ");
// Serial.println(" THEATER ");
// }
// else if (state == 2)
// {
// Blynk.virtualWrite(V5, " COLOR WIPE ");
// Serial.println(" COLOR WIPE ");
// }
// else if (state == 3)
// {
// Blynk.virtualWrite(V5, " RAINBOW ");
// Serial.println(" RAINBOW ");
// }
// else if (state == 4)
// {
// Blynk.virtualWrite(V5, " RAINBOW CIRCLE ");
// Serial.println(" RAINBOW CIRCLE ");
// }
// else if (state == 5)
// {
// Blynk.virtualWrite(V5, " THEATER RAINBOW ");
// Serial.println(" THEATER RAINBOW ");
// }
// }
void theaterChase(uint32_t c, uint8_t wait)
{
for (int j = 0; j < 10; j++)
{
for (int q = 0; q < 3; q++)
{
for (uint16_t i = 0; i < pixels.numPixels(); i += 3)
{
pixels.setPixelColor(i + q, c); //turn every third pixel on
}
pixels.show();
delay(wait);
for (uint16_t i = 0; i < pixels.numPixels(); i += 3)
{
pixels.setPixelColor(i + q, 0); //turn every third pixel off
}
}
}
}
void colorWipe(uint32_t c, uint8_t wait)
{
for (uint16_t i = 0; i < pixels.numPixels(); i++)
{
pixels.setPixelColor(i, c);
pixels.show();
delay(wait);
}
}
void rainbow(uint8_t wait)
{
for (uint16_t j = 0; j < 256; j++)
{
if (state != 3) break;
for (uint16_t i = 0; i < pixels.numPixels(); i++)
{
if (state != 3) break;
pixels.setPixelColor(i, Wheel((i + j) & 255));
}
pixels.show();
delay(wait);
}
}
void rainbowCycle(uint8_t wait)
{
for (uint16_t j = 0; j < 256 * 5; j++) // 5 cycles of all colors on wheel
{
if (state != 4) break;
for (uint16_t i = 0; i < pixels.numPixels(); i++)
{
if (state != 4) break;
pixels.setPixelColor(i, Wheel(((i * 256 / pixels.numPixels()) + j) & 255));
}
pixels.show();
delay(wait);
}
}
// Non-Blocking
// Non-blocking rainbow
void nonBlockingRainbow(uint8_t wait) {
unsigned long currentMillis = millis();
if (currentMillis - prevAnimTime >= wait) {
prevAnimTime = currentMillis;
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, Wheel((i + rainbowIndex) & 255));
}
pixels.show();
rainbowIndex++;
if (rainbowIndex >= 256) rainbowIndex = 0;
}
}
// Non-blocking rainbow cycle
void nonBlockingRainbowCycle(uint8_t wait) {
unsigned long currentMillis = millis();
if (currentMillis - prevAnimTime >= wait) {
prevAnimTime = currentMillis;
for (int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, Wheel(((i * 256 / NUMPIXELS) + rainbowCycleIndex) & 255));
}
pixels.show();
rainbowCycleIndex++;
if (rainbowCycleIndex >= 256 * 5) rainbowCycleIndex = 0;
}
}
// Non-blocking theaterChaseRainbow
void nonBlockingTheaterChaseRainbow(uint8_t wait) {
unsigned long currentMillis = millis();
if (currentMillis - prevAnimTime >= wait) {
prevAnimTime = currentMillis;
for (int i = 0; i < NUMPIXELS; i += 3) {
pixels.setPixelColor(i + theaterColorIndex % 3, Wheel((i + theaterIndex) % 255));
}
pixels.show();
for (int i = 0; i < NUMPIXELS; i += 3) {
pixels.setPixelColor(i + theaterColorIndex % 3, 0); // Clear
}
theaterColorIndex++;
if (theaterColorIndex >= 3) {
theaterColorIndex = 0;
theaterIndex++;
if (theaterIndex >= 256) theaterIndex = 0;
}
}
}
void nonBlockingTheaterRainbow() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 50) {
previousMillis = currentMillis;
// update 1 langkah animasi
// simulasikan satu langkah dari theaterChaseRainbow
pixels.setPixelColor(theaterIndex % NUMPIXELS, pixels.Color(random(255), random(255), random(255)));
pixels.show();
theaterIndex++;
if (theaterIndex >= NUMPIXELS) theaterIndex = 0;
}
}
// void theaterChaseRainbow(uint8_t wait)
// {
// for (uint16_t j = 0; j < 256; j++) // cycle all 256 colors in the wheel
// {
// if (state != 5) break;
// for (int q = 0; q < 3; q++)
// {
// for (uint16_t i = 0; i < pixels.numPixels(); i += 3)
// {
// pixels.setPixelColor(i + q, Wheel((i + j) % 255)); //turn every third pixel on
// }
// pixels.show();
// delay(wait);
// for (uint16_t i = 0; i < pixels.numPixels(); i += 3)
// {
// pixels.setPixelColor(i + q, 0); //turn every third pixel off
// }
// }
// }
// // Reset state to 0 after finishing theaterChaseRainbow
// state = 0;
// }
uint32_t Wheel(byte WheelPos)
{
WheelPos = 255 - WheelPos;
if (WheelPos < 85)
{
return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
else if (WheelPos < 170)
{
WheelPos -= 85;
return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
else
{
WheelPos -= 170;
return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}