Adafruit Amplifier MAX9744 Web Volume Control

Hi,

I have a MAX9744 amplifier that I’m trying to use digital control from a EtherTen from freetronics (Arduino Uno + Ethernet Shield).

Everything seems to work fine. The amplifier works (plays sound), the EtherTen works but there is a problem in my code when it tries to set the volume. When I call the setvolume function it executes but it doesn’t seem to return. Any help would be greatly appreciated.

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>

// 0x4B is the default i2c address
#define MAX9744_I2CADDR 0x4B
 
// We'll track the volume level in this variable.
int8_t thevol = 10;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //mac address
byte ip[] = { 10, 10, 10, 34 };                      // Lan IP
byte gateway[] = { 10, 10, 10, 254 };                // gateway IP
byte subnet[] = { 255, 255, 255, 0 };
EthernetServer server(80);
String buffer;

void setup() {
delay( 100 );   // allow some time (50+ ms) after powerup and sketch start, for the Wiznet W5100 Reset IC to release and come out of reset.

  Serial.begin(9600);

  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
  
  Serial.println("MAX9744 amp test...");
  Wire.begin();
  
  if (! setvolume(thevol)) {
    Serial.println("Failed to set volume, MAX9744 not found!");
    while (1);
  }
  Serial.println("I never get here");
}

// ** SET VOLUME FUNCTION **
// Setting the volume is very simple! Just write the 6-bit
// volume to the i2c bus. That's it!
boolean setvolume(int8_t v) {
  // cant be higher than 63 or lower than 0
  if (v > 63) v = 63;
  if (v < 0) v = 0;
  
  Serial.print("Setting volume to ");
  Serial.println(v);
  Wire.beginTransmission(MAX9744_I2CADDR);
  Wire.write(v);
  if (Wire.endTransmission() == 0) 
    return true;
  else
    return false;
}

void loop() {
  EthernetClient ethernet_shield = server.available();
  if (ethernet_shield) {
    while (ethernet_shield.connected()) {   
      if (ethernet_shield.available()) {
        char c = ethernet_shield.read();
        if (buffer.length() < 100) {
          buffer += c;
           }
           if (c == '\n') {
           // Serial.println(buffer); 
           ethernet_shield.println("HTTP/1.1 200 OK"); 
           ethernet_shield.println("Content-Type: text/html");
           ethernet_shield.println();     
           ethernet_shield.println("");
           ethernet_shield.println("");
           ethernet_shield.println("Amplifier Controlled by webpage");
           ethernet_shield.println("");
           ethernet_shield.println("");
           ethernet_shield.println("<p style='color: black;'>&nbsp;&nbsp;Volume&nbsp;&nbsp;");
           ethernet_shield.println("<a href='?voldown'><button style='background-color: azure; border-radius: 12px;'> DOWN +5</button></a>&nbsp;&nbsp;");
           ethernet_shield.println("<a href='?volup'><button style='background-color: azure; border-radius: 12px;'> UP +5</button></a>&nbsp;&nbsp;");
           ethernet_shield.println("<a href='?mute'><button style='background-color: lightpink; border-radius: 12px;'> MUTE</button></a></p>");
           ethernet_shield.println("");
     
           delay(1);
           ethernet_shield.stop();
           
           if (buffer.indexOf("?voldown") >0){
               thevol = thevol - 5;
           }
           if (buffer.indexOf("?volup") >0){
               thevol = thevol + 5;
           }
           if (buffer.indexOf("?mute") >0){
               thevol = 0;
           }

           if (! setvolume(thevol)) {
            Serial.println("Failed to set volume from web command");
           }
           
           //clearing string for next read
           buffer="";  
         }
       }
    }
  }
}

Serial monitor output…

server is at 10.10.10.34
MAX9744 amp test...
Setting volume to 10