/*
"its actually just 4 integer data but separated with a comma. Here's the python code thats sending the data
datanya = "{},{},{},{}".format(neko1,neko2,neko3,neko4)
ser.write((datanya + "\n").encode())
so the serial data sent is "0,0,0,0" with a "\n" at the end
the values of each integer changes between 0 and 1"
- foreyan
*/
#include <Adafruit_NeoPixel.h>
#define NYEO 4
#define NUMPIXELS 8
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, NYEO, NEO_GRB + NEO_KHZ800);
int r = 0;
int g = 255;
int b = 139;
int fr = 0;
int fg = 0;
int fb = 0;
uint32_t colors[NUMPIXELS];
int value1, value2, value3, value4;
int noko1, noko2, noko3, noko4;
void setup() {
Serial.begin(9600);
/* the next three lines are just instructions for the Wokwi */
Serial.println(F("Input expected Strings in Monitor"));
Serial.println(F("Four integers between 0 and 1 to simulate Python script"));
Serial.println(F("Example: 0,1,0,1"));
strip.begin();
strip.show();
}
void loop() {
if (Serial.available() > 0) {
// Stage 1 Denial
String input = Serial.readStringUntil('\n');
parseInput(input);
// Stage 2 Anger
if (value1 > 0) {
noko1 = 39;
}
if (value2 > 0) {
noko2 = 39;
}
if (value3 > 0) {
noko3 = 39;
}
if (value4 > 0) {
noko4 = 39;
}
}
{
// Stage 3 Bargaining
if (noko1 > 0) {
noko1--;
}
if (noko2 > 0) {
noko2--;
}
if (noko3 > 0) {
noko3--;
}
if (noko4 > 0) {
noko4--;
}
// Stage 4 Depression
int neko1 = noko1 * 6;
int neko2 = noko2 * 6;
int neko3 = noko3 * 6;
int neko4 = noko4 * 6;
int BrightNya[NUMPIXELS] = {neko1, neko1, neko2, neko2, neko3, neko3, neko4, neko4};
for (int i = 0; i < NUMPIXELS; i++) {
fr = (r * BrightNya[i]) / 255;
fg = (g * BrightNya[i]) / 255;
fb = (b * BrightNya[i]) / 255;
colors[i] = strip.Color(fr, fg, fb);
}
// Stage 5 Acceptance
for (int i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, colors[i]);
}
delay(10);
strip.show();
}
}
void parseInput(String input) {
int startIndex = 0;
int commaIndex;
int valueIndex = 0;
while ((commaIndex = input.indexOf(',', startIndex)) != -1 && valueIndex < 3) {
String valueString = input.substring(startIndex, commaIndex);
int value = valueString.toInt();
switch (valueIndex) {
case 0: value1 = value; break;
case 1: value2 = value; break;
case 2: value3 = value; break;
}
startIndex = commaIndex + 1;
valueIndex++;
}
value4 = input.substring(startIndex).toInt();
}