Hello everyone,
im currently working on a Wallmounted LED 4 connect game (first project with c++ and microcontroller).
The Animations works just fine but i have a problem with the last status which gets overwritten.
(it will just show the last coin and nothing else)
My first idea was to store every step in a vector but that failed horribly due to storage issues (have no clue how to correct save/edit stuff during runtime).
For a better understanding i will attach how my build looks like.
Game logic itself is not needed this will be done by the node server.
Every help/idea to solve this issue is welcome. Thx in advance.
#include <Adafruit_NeoPixel.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#ifndef STASSID
#define STASSID "network"
#define STAPSK "password"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
#define LED_COUNT 126
#define LED_PIN D9
using namespace std;
ESP8266WebServer server(80);
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
uint32_t getPlayerColor(int playerId){
if(playerId == 1) {
return strip.Color(0, 0, 127);
}
return strip.Color(127, 0, 0);
}
void coin(uint32_t c, int line, int row) {
int start = (row-1)*18;
int end = start+(line*3)-2;
for (uint16_t i=start; i < end; i=i+1) {
strip.setPixelColor(i, c);
strip.setPixelColor(i+1, c);
strip.setPixelColor(i+2, c);
strip.show();
delay(200);
if (i!=end) {
strip.setPixelColor(i, 0);
strip.setPixelColor(i+1, 0);
strip.setPixelColor(i+2, 0);
}
}
}
void coinReverse(uint32_t c, int line, int row) {
int start = ((row-1)*18-1)+18;
int end = start-(line*3)+2;
for (uint16_t i=start; i > end; i=i-1) {
strip.setPixelColor(i, c);
strip.setPixelColor(i-1, c);
strip.setPixelColor(i-2, c);
strip.show();
delay(200);
if (i!=end) {
strip.setPixelColor(i, 0);
strip.setPixelColor(i-1, 0);
strip.setPixelColor(i-2, 0);
}
}
}
void connect() {
int l = atoi(server.arg("l").c_str());
int r = atoi(server.arg("r").c_str());
int p = atoi(server.arg("p").c_str());
if (l != 0 && r != 0 && p != 0) {
server.send(200, "text/plain", "OK");
if (r % 2 == 0) {
coin(getPlayerColor(p), l, r);
} else {
coinReverse(getPlayerColor(p), l, r);
}
} else {
server.send(400, "text/plain", "Bad Request");
}
}
void setup() {
Serial.begin(115200);
Serial.println("Booting");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else {
type = "filesystem";
}
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
pinMode(2, OUTPUT);
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
strip.begin();
strip.show();
server.on("/connect", connect);
server.begin();
}
void loop() {
digitalWrite(LED_BUILTIN, LOW);
ArduinoOTA.handle();
server.handleClient();
}