MUX74HC4067

HI I am using the above board to expand the inputs on the arduino, the example works fine and I get a pressed/not pressed, message.

However in my own project the message isn’t firing. I am checking the value of data and it shows 4 in the example code it shows 255.

I am at a lost what is wrong, could anyone point in the right direction please.

#define NUM_ZONES (63)

#include <SPI.h>        
#include <Ethernet.h>
#include <LiquidCrystal.h>
#include <SD.h>
#include <SPI.h>
#include <IniFile.h>
#include "MUX74HC4067.h"

LiquidCrystal *pLCD = NULL;
MUX74HC4067 *pmux1 = NULL;

// The select pin used for the SD card
#define SD_SELECT 4
#define ETHERNET_SELECT 10

EthernetServer server(80);

//Lcd pins these need to read off ini file
String rs;
String en;
String d4;
String d5;
String d6;
String d7;

//Mux's variables
String Mux1en;
String Mux1s0;
String Mux1s1;
String Mux1s2;
String Mux1s3;

//arrays to check if button has changed
int buttonState[NUM_ZONES] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int previousState[NUM_ZONES]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

//Command to pass to homeseer
char* myStrings[64];

//SubRoutine For Error Messages Reading SD card
 void printErrorMessage(uint8_t e, bool eol = true)
{
  switch (e) {
  case IniFile::errorNoError:
    Serial.print("no error");
    break;
  case IniFile::errorFileNotFound:
    Serial.print("file not found");
    break;
  case IniFile::errorFileNotOpen:
    Serial.print("file not open");
    break;
  case IniFile::errorBufferTooSmall:
    Serial.print("buffer too small");
    break;
  case IniFile::errorSeekError:
    Serial.print("seek error");
    break;
  case IniFile::errorSectionNotFound:
    Serial.print("section not found");
    break;
  case IniFile::errorKeyNotFound:
    Serial.print("key not found");
    break;
  case IniFile::errorEndOfFile:
    Serial.print("end of file");
    break;
  case IniFile::errorUnknownError:
    Serial.print("unknown error");
    break;
  default:
    Serial.print("unknown error value");
    break;
  }
  if (eol)
    Serial.println();
}

void setup() 
{
  //Config and read SD card
  // Configure all of the SPI select pins as outputs and make SPI
  // devices inactive, otherwise the earlier init routines may fail
  // for devices which have not yet been configured.
  pinMode(SD_SELECT, OUTPUT);
  digitalWrite(SD_SELECT, HIGH); // disable SD card
  pinMode(ETHERNET_SELECT, OUTPUT);
  digitalWrite(ETHERNET_SELECT, HIGH); // disable Ethernet
  
  const size_t bufferLen = 80;
  char buffer[bufferLen];
  const char *filename = "/config.ini";
  Serial.begin(9600);
  SPI.begin();
  if (!SD.begin(SD_SELECT))
    while (1)
      Serial.println("SD.begin() failed");
  
  IniFile ini(filename);
  if (!ini.open()) {
    Serial.print("Ini file ");
    Serial.print(filename);
    Serial.println(" does not exist");
    // Cannot do anything else
    while (1)
      ;
  }
  Serial.println("Ini file exists");

  // Check the file is valid. This can be used to warn if any lines
  // are longer than the buffer.
  if (!ini.validate(buffer, bufferLen)) {
    Serial.print("ini file ");
    Serial.print(ini.getFilename());
    Serial.print(" not valid: ");
    printErrorMessage(ini.getError());
    // Cannot do anything else
    while (1)
      ;
  }
  
 // Fetch a value from a key which is present
 // get data from sd card and write to array
Serial.println("getting Homesser commands");
   for (int j=0; j<=63; j=j+1) {
         
        char mpass[ ] = "command"  ;     
        int num = j;
        char cstr[16];
        itoa(num, cstr, 10);
        strcat(mpass,cstr);
        
        if (ini.getValue("HSCOMMANDS",mpass, buffer, bufferLen)) {
        myStrings[j] = buffer;
        }
  }

  Serial.println("getting LCD");
 //get the lcd screen pins
 if (ini.getValue("LCD", "rs", buffer, bufferLen)) {
   rs = buffer;
 }

 if (ini.getValue("LCD", "en", buffer, bufferLen)) {
  en = buffer;
   }

if (ini.getValue("LCD", "d4", buffer, bufferLen)) {
   d4 = buffer;
  }

if (ini.getValue("LCD", "d5", buffer, bufferLen)) {
   d5 = buffer;
   }

if (ini.getValue("LCD", "d6", buffer, bufferLen)) {
   d6 = buffer;
 }

if (ini.getValue("LCD", "d7", buffer, bufferLen)) {
   d7 = buffer;
 }
 
pLCD = new LiquidCrystal(rs.toInt(), en.toInt(), d4.toInt(), d5.toInt(), d6.toInt(), d7.toInt());


//Mux set up
if (ini.getValue("MUX1", "en", buffer, bufferLen)) {
   Mux1en = buffer;
 }
if (ini.getValue("MUX1", "s0", buffer, bufferLen)) {
   Mux1s0 = buffer;
 }
if (ini.getValue("MUX1", "s1", buffer, bufferLen)) {
   Mux1s1 = buffer;
 }
if (ini.getValue("MUX1", "s2", buffer, bufferLen)) {
   Mux1s2 = buffer;
 }
if (ini.getValue("MUX1", "s3", buffer, bufferLen)) {
   Mux1s3 = buffer;
 }
MUX74HC4067 pmux1(Mux1en.toInt(), Mux1s0.toInt(), Mux1s1.toInt(), Mux1s2.toInt(), Mux1s3.toInt());
// Configures how the SIG pin will be interfaced e.g. The SIG pin connects to PIN 3 on the Arduino and PIN 3 is a digital input
pmux1.signalPin(3, INPUT, DIGITAL);

// set up the LCD's number of columns and rows:  Print a message to the LCD.
pLCD->begin(20, 3);
pLCD->print("loaded ini");
Serial.println("loaded ini");
}

void loop()
{
  
byte data;
String hsCommand;
String mypass;
  for (byte i = 0; i < 16; ++i)
  {
    // Reads from channel i and returns HIGH or LOW
    //when ALL 4 MUX IN THEN loop 0 63 and split by 16 each mux
    
    data = pmux1->read(i);

   Serial.print("Push button at channel ");
   Serial.print(i);
   Serial.print(" is ");
   Serial.println(data);
   
   
//    if ((data == HIGH) && (previousState[i] == LOW ))  {   
//              //call homeseerpost turning on
//              Serial.println("On");

              
//              mypass = String(myStrings[i]) + "&label=off";

//              Serial.println(mypass);
              
//              sendHomeSeerCommand(mypass );
              
//              } 
//    else if ((data == LOW) && (previousState[i] == HIGH )) {
//            // call homeseerpost turning off
//            Serial.println("Off");
//            mypass = String(myStrings[i]) + "&label=on";
//            sendHomeSeerCommand(mypass);
           
//            }

//    previousState[i] = data;

    
      if ( data == HIGH ) Serial.println("not pressed");
    else if ( data == LOW ) Serial.println("pressed");

  }
  Serial.println();
  delay(1500);
  
  }

  void sendHomeSeerCommand(String HScommand)
  {
    IPAddress server(192,168,1,60);
    String PostData = "/JSON?request=controldevicebylabel&ref=" + HScommand ;
    EthernetClient client ;
    if (client.connect(server, 8080)) {
      client.println("POST /Api/AddParking/3 HTTP/1.1");
      client.println("Host: 10.0.0.138");
      client.println("User-Agent: Arduino/1.0");
      client.println("Connection: close");
      client.print("Content-Length: ");
      client.println(PostData.length());
      client.println();
      client.println(PostData);
    }
}

I think you've got some setup errors... It appears you are mixing Strings and char arrays and pointers to char.

you declare this:

char* myStrings[64];

and then within setup, you do

       myStrings[j] = buffer;

which just assigns the address of buffer to all elements of myStrings. It doesn't actually copy the data since in is not a 'String'

You also do this:

       char mpass[ ] = "command"  ;     
        int num = j;
        char cstr[16];
        itoa(num, cstr, 10);
        strcat(mpass,cstr);

which is overrunning mpass. If you want to have room to append things, you have to create a larger array or use the 'String' class.

I personally don't use the String class (just use char arrays) but it is easier for newer people to understand and usually does what they expect with assignment and concatination, etc.

My guess is that you are not setting up the mux correctly from your ini file