Data logger and two 4051 Mux's

I am trying to find a way to separate GetAjaxData () into smaller functions that only control one input. I would like to also find a way to log the data. But I still need the inputs to be read every second, or user defined time.

Currently, the code reads two 4051 mux's through common address lines every second. I would like to have them all read separately so that if the user is only plugged into 3 of them the other's don't show random data in the logger. I am using an arduino Mega 2560 with ethernet board.

Any help is greatly appreciated!

please find code attached

current code.txt (19.9 KB)

You can't tell is there is a 'valid' signal input unless u read the post... Which means U'll read all 16.
U may want to use an array to store values and a loop to read then. (to replace 8 equal sequences)

knut_ny

I'm afraid I do not understand what you are proposing. Could you try to explain it again in a little more detail? I apologize I do not do much coding.

altered code..

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

byte mac[] = { 
  0x90, 0xA2, 0xDA, 0x0E, 0xCC, 0xC8 };
IPAddress ip(192,168,1, 177);

EthernetServer server(80);

int S0=11;
int S1=12;
int S2=13;

int reading[16]; //0..7 MUX1, 9..15 MUX2  THIS IS ADDED ************

String HTTP_req; //Stores the HTTP request

void setup() {
      // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.begin(9600);

 pinMode(S0,OUTPUT); //s0
  pinMode(S1,OUTPUT); //s1
  pinMode(S2,OUTPUT); //s2
}


void loop() 
{
//   retore this part
{

//send the state of the switch to the web
void GetAjaxData(EthernetClient cl)
{
   for(byte i=0;i<8;i++)  // REWRITTEN to use an array and a loop
   {
    digitalWrite(S0,i & 1);
    digitalWrite(S1,i & 2);
    digitalWrite(S2,i & 4);
    reading[i] = analogRead(0); //read the arduino input pin the 4051 is using    
    reading[i+8] = analogRead(1); //read the arduino input pin the 4051 is using    
   }       
  // if you call your inputs numbers from 0..15, also the next section goes fine into the same loop    
    cl.print(F("M1Y0 :"));
    cl.println(reading[0]); 
    cl.print(F("M1Y1 :"));
    cl.println(reading[1]); 
    cl.print(F("M1Y2 :"));
    cl.println(reading[2]); 
    cl.print(F("M1Y3 :"));
    cl.println(reading[3]); 
    cl.print(F("M1Y4 :"));
    cl.println(reading[4]); 
    cl.print(F("M1Y5 :"));
    cl.println(reading[5]); 
    cl.print(F("M1Y6 :"));
    cl.println(reading[6]);  
    cl.print(F("M1Y7 :"));
    cl.println(reading[7]); 
    cl.println("


");
    cl.print(F("M2Y0 :"));
    cl.println(reading[8]);
    cl.print(F("M2Y1 :"));
    cl.println(reading[9]); 
    cl.print(F("M2Y2 :"));
    cl.println(reading[10]); 
    cl.print(F("M2Y3 :"));
    cl.println(reading[11]); 
    cl.print(F("M2Y4 :"));
    cl.println(reading[12]); 
    cl.print(F("M2Y5 :"));
    cl.println(reading[13]); 
    cl.print(F("M2Y6 :"));
    cl.println(reading[14]); 
    cl.print(F("M2Y7 :"));
    cl.println(reading[15]); 
  // comments removed
}

Thanks knut_ny!! That works great.