Close

Speaker Code (Each)

A project log for Center Surround Sound

Make You The Center of Your World

Vije MillerVije Miller 09/05/2018 at 18:410 Comments
// 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);
      }
    }
  }
}

Discussions