Center Surround Sound (NodeMCU)
When settling speakers in to place, you are idealizing that you will always sit dead center .. yet in our more lethargic reality, we lounge off-center. As consequence, you cease being the center of the speaker array .. until now ..


Ingredients
NodeMCU ESP8266 ESP-12E V2 (2)
Digital Potentiometer (2)
5V Power Supply (2)
4 Dip Switch (2)
4 Slot Terminals (2)



Notations
1. Intended to create an app with MIT App Inventor .. how-ever .. per the previous HaD FP (Voice Kitchen Faucet) .. it was decided to employ an alternative favored open source (-ish) voice command software http://voiceattack.com purely as demonstration.
2. Adding additional speakers merely requires copy-paste and if-statement modifications .. tho ideally a more advanced 'find' function upon installation would be coded.
3. Variances of % may depend on wattage (See #4)
4. Higher voltage systems would subsequently require HV digital potentiometers (AD7376)
5. Why is this .. not already a thing?!

Codelings
// Center Surround Sound
// Speaker NodeMCU Code
// Vije Miller

#include <ESP8266WiFi.h>
#include <DigiPotX9Cxxx.h>

// Set Static IP Per Speaker
// Ex. 20 = Center Left Speaker
IPAddress ip(192, 168, 1, 20);

IPAddress gateway(192, 168, 1, 1);
IPAddress dns(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
const char* ssid = "ssid";
const char* pass = "password";

WiFiServer server(80);

// LED
int led = D0;

DigiPot pot(4, 5, 6);
volatile int i = 0;
// Start at 75%
int wiper = 75;
int peak = 75;

// Speaker Dip Switch
int dipone = 1;
int diptwo = 2;

void setup() {
Serial.begin(115200);

pinMode(dipone, INPUT);
pinMode(diptwo, INPUT);
if (digitalRead(dipone) == HIGH) {
int dip = 1;
} else if (digitalRead(diptwo) == HIGH) {
int dip = 2;
}

pinMode(led, OUTPUT);
digitalWrite(led, LOW);

// Connect
Serial.println(ssid);
WiFi.config(ip, gateway, subnet, dns);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected");
server.begin();
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}

void loop() {
// Active LED
digitalWrite(led, LOW);
// Check
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait
while (!client.available()) {
delay(1);
}
// Read
String query = client.readStringUntil('\r');
client.flush();
if (query.indexOf("/center") != -1) {
center();
}
if (query.indexOf("/left_center") != -1) {
left_center();
}
if (query.indexOf("/right_center") != -1) {
right_center();
}
if (query.indexOf("/left_off_set") != -1) {
left_off_set();
}
if (query.indexOf("/right_off_set") != -1) {
right_off_set();
}
delay(1);
}

void center() {
digitalWrite(led, HIGH);
// Zero All Speakers
if (wiper < peak) {
for (i = wiper; i < peak; i++) {
pot.increase(1);
delay(5);
}
} else if (wiper > peak) {
for (i = wiper; i > peak; i--) {
pot.decrease(1);
delay(5);
}
}
}

void left_center() {
digitalWrite(led, HIGH);
// 1 = Decrease -15
// 2 = Increase +15
if (dip == 1) {
int lc = 60
if (wiper < lc) {
for (i = wiper; i < lc; i++) {
pot.increase(1);
delay(5);
}
} else if (wiper > lc) {
for (i = wiper; i > lc; i--) {
pot.decrease(1);
delay(5);
}
}
} else if (dip == 2) {
int lc = 90
if (wiper < lc) {
for (i = wiper; i < lc; i++) {
pot.increase(1);
delay(5);
}
} else if (wiper > lc) {
for (i = wiper; i > lc; i--) {
pot.decrease(1);
delay(5);
}
}
}
}

void right_center() {
digitalWrite(led, HIGH);
// 1 = +15
// 2 = -15
if (dip == 1) {
int lc = 90
if (wiper < lc) {
for (i = wiper; i < lc; i++) {
pot.increase(1);
delay(5);
}
} else if (wiper > lc) {
for (i = wiper; i > lc; i--) {
pot.decrease(1);
delay(5);
}
}
} else if (dip == 2) {
int lc = 60
if (wiper < lc) {
for (i = wiper; i < lc; i++) {
pot.increase(1);
delay(5);
}
} else if (wiper > lc) {
for (i = wiper; i > lc; i--) {
pot.decrease(1);
delay(5);
}
}
}
}

void left_off_set() {
digitalWrite(led, HIGH);
// 1 = -25
// 2 = +25
if (dip == 1) {
int lc = 50
if (wiper < lc) {
for (i = wiper; i < lc; i++) {
pot.increase(1);
delay(5);
}
} else if (wiper > lc) {
for (i = wiper; i > lc; i--) {
pot.decrease(1);
delay(5);
}
}
} else if (dip == 2) {
int lc = 100
if (wiper < lc) {
for (i = wiper; i < lc; i++) {
pot.increase(1);
delay(5);
}
} else if (wiper > lc) {
for (i = wiper; i > lc; i--) {
pot.decrease(1);
delay(5);
}
}
}
}

void right_off_set() {
digitalWrite(led, HIGH);
// 1 = +25
// 2 = -25
if (dip == 1) {
int lc = 100
if (wiper < lc) {
for (i = wiper; i < lc; i++) {
pot.increase(1);
delay(5);
}
} else if (wiper > lc) {
for (i = wiper; i > lc; i--) {
pot.decrease(1);
delay(5);
}
}
} else if (dip == 2) {
int lc = 50
if (wiper < lc) {
for (i = wiper; i < lc; i++) {
pot.increase(1);
delay(5);
}
} else if (wiper > lc) {
for (i = wiper; i > lc; i--) {
pot.decrease(1);
delay(5);
}
}
}
}

Archive
https://hackaday.io/project/160842-center-surround-sound
https://hackaday.com/2018/09/19/voice-controlled-stereo-balance-with-esp8266/

18:19:41 177-018