Go Down

Topic: Mayhew Labs MUX SHIELD  (Read 790 times) previous topic - next topic

szosh

Hello there,
I've been looking for some help on my specific topic, but I don't believe I can find anything on the forum as of yet.  I am using a MUX shield II from Mayhew labs to create a reactive LED table.  I currently have 12 infrared sensors in close proximity to infrared emitters.  These sensors are being fed into the inputs of the mutiplexer section of the board, and we are using the shift registers to allow the cathode of an LED cluster to go LOW and drop the necessary voltage. 
Currently, I have run tests of my board with the MUX library examples, I know I am able to obtain accurate sensor readings, and write to all the available LED clusters. 
My problem with this project, I believe comes from running inefficient programming, and not fully understanding how to effectively write to the shift registers. 
My program consists of a setup loop where 25 readings of each sensor are obtained and then an average is computed for each sensor.
Then the loop executes, it uses 12 IF statements to compare the current analog read value to the average sensor value, and writes the LED closest to that sensor LOW(my logic is reversed because I am supplying 5V to all LED clusters, and using the shift registers to allow the clusters cathodes to be grounded and let the voltage drop). 
I think that there is a much more effective way to handle this, perhaps by calling individual functions for each instance of sensor readings.
Currently, I can get favorable results by only programming one row of shift registers at a time.  When I try communicating with rows 2 and 3 of the MUX Shield, extra data is written down the line from the activated sensor.
I will have a schematic to post asap


Code

#include <MuxShield.h>

MuxShield muxShield;

struct calibration_t {    //create structure
  int sensorArray[25];    //make an array for 25 sensor values
  int sensorTotal;
  int sensorAverage;
};
const int calibration_runs = 25; //25 runs
calibration_t calibs[calibration_runs];
int dely = 100;
int dely1 = 25;
int loopRead[16];
//int wig = 0;//wiggle room for sensor average
void setup() {
  muxShield.setMode(1,ANALOG_IN);

  muxShield.setMode(2,DIGITAL_OUT);

  muxShield.setMode(3,DIGITAL_OUT);
 
  Serial.begin(9600);
  for  (int i=0; i<=14;i++){  //write all LED's HIGH(Turn them off)
    muxShield.digitalWriteMS(2,i,HIGH); 
    muxShield.digitalWriteMS(3,i,HIGH);
  }
 
  for (int j=0; j <calibration_runs; j++) {//run 25 calibration loops
   
    for (int i=0; i<=11; i++){//take easch sensor value
      calibs[j].sensorArray = muxShield.analogReadMS(1,i);
     
      calibs[j].sensorTotal += calibs[j].sensorArray;
      // add each reading to itself
      delay(dely1);
    }
    calibs[j].sensorAverage = calibs[j].sensorTotal / 25;
   //divide totals by 25

}

 muxShield.digitalWriteMS(3,14,LOW);
 delay(500);
 muxShield.digitalWriteMS(3,14,HIGH);
 

}

void loop(){
//
//  for(int i=0;i<=5;i++){
//    muxShield.analogReadMS(1,i);
//   
//}
//not sure if I need this to obtain a good base reading


 
   if((muxShield.analogReadMS(1,0)) < (calibs[0].sensorAverage*1.15))
   {
     for (int i=0;i<=3;i++){
     muxShield.digitalWriteMS(2,i,B11111111);
     }
 }
     else
     {
         for(int i=0;i<=3;i++){
         muxShield.digitalWriteMS(2,i,B00000000);
//         delay(dely);
         }
 }
//Is this the best way to write to the Shift registers?
     
     
     if(muxShield.analogReadMS(1,1)<(calibs[1].sensorAverage*1.5))
     {
       for (int i=2;i<=5;i++){
       muxShield.digitalWriteMS(2,i,B11111111);
     }
 }
     else{
       for(int i=2;i<=5;i++){
         muxShield.digitalWriteMS(2,i,B00000000);
//         delay(dely);
       }
     }
     
     
     if(muxShield.analogReadMS(1,2) <(calibs[2].sensorAverage*1.15)){
     for (int i=4;i<=7;i++){
     muxShield.digitalWriteMS(2,i,B11111111);
     }
 }
     else{
       for(int i=4;i<=7;i++){
         muxShield.digitalWriteMS(2,i,B00000000);
         //delay(dely);
       }
     }

//////     
//////     
     if(muxShield.analogReadMS(1,3) < calibs[3].sensorAverage){
     for (int i=6;i<=9;i++){
     muxShield.digitalWriteMS(2,i,B11111111);
     }
 }
     else
     {
       for(int i=6;i<=9;i++){
         muxShield.digitalWriteMS(2,i,B00000000);
         //delay(dely);
         
       }
       
    }
       
     if(muxShield.analogReadMS(1,4)<(calibs[4].sensorAverage*1.05)){
   
     for (int i=10;i<=13;i++){
     muxShield.digitalWriteMS(2,i,B11111111);
        }
     }
     else{
       for (int i=10;i<=13;i++){
      muxShield.digitalWriteMS(2,i,B00000000);
      //delay(dely);
       }
     }

//     
     if(muxShield.analogReadMS(1,5)<(calibs[5].sensorAverage*0.95))
     {
     
       for (int i=12;i<=14;i++){
       muxShield.digitalWriteMS(2,i,B11111111);
        }     
     
     }
     
       else if ((muxShield.analogReadMS(1,5))>(calibs[5].sensorAverage*0.95))
       {
         for (int i=12;i<=14;i++){
     muxShield.digitalWriteMS(2,i,B00000000);
        }   
       }

         for(int i=6;i<=12;i++)
         {
            muxShield.analogReadMS(1,i);
         }
             
    if(muxShield.analogReadMS(1,6)<(calibs[6].sensorAverage)*0.5){
     for (int i=0;i<=2;i++){
       muxShield.digitalWriteMS(3,i,B11111111);
     }     
     }
     else
     {
      for (int i=0;i<=2;i++){
       muxShield.digitalWriteMS(3,i,B00000000);
      // delay(dely);
     
   }     
     }
     
   //  Serial.println(muxShield.analogReadMS(1,6));
     //delay(500);
     
     if(muxShield.analogReadMS(1,7)<calibs[7].sensorAverage){
      for(int i=1;i<=4;i++){
        muxShield.digitalWriteMS(3,i,B11111111);
     }
     }
     else{
       for(int i=1;i<=4;i++){
        muxShield.digitalWriteMS(3,i,B00000000);
       // delay(dely);
     }
     }
     
     if(muxShield.analogReadMS(1,8)<(calibs[8].sensorAverage)){
       for (int i=5;i<=8;i++){
         muxShield.digitalWriteMS(3,i,B11111111);
       }
     }
       else{
         for (int i=5;i<=8;i++)
         muxShield.digitalWriteMS(3,i,B00000000);
         //delay(dely);
       }

       if(muxShield.analogReadMS(1,9)<calibs[9].sensorAverage){
       for (int i=7;i<=10;i++){
         muxShield.digitalWriteMS(3,i,B11111111);
       }
     }
       else{
         for (int i=7;i<=10;i++)
         muxShield.digitalWriteMS(3,i,B00000000);
         //delay(dely);
       }

      if(muxShield.analogReadMS(1,10)<calibs[10].sensorAverage){
       for (int i=9;i<=12;i++){
         muxShield.digitalWriteMS(3,i,B11111111);
       }
     }
       else{
         for (int i=9;i<=12;i++)
         muxShield.digitalWriteMS(3,i,B00000000);
         //delay(dely);
       }
     
        if(muxShield.analogReadMS(1,11)<calibs[11].sensorAverage){
       for (int i=11;i<=14;i++){
         muxShield.digitalWriteMS(3,i,B11111111);
       }
     }
       else{
         for (int i=11;i<=14;i++)
         muxShield.digitalWriteMS(3,i,B00000000);
         //delay(dely);
       }
}









szosh

Here is a close representation of my schematic.  The transistors are phototransistors closed when more infrared comes in.

chearne

I am having the exact same issue. I can write to one I/O row just fine, but as soon as I include code to write to multiple I/O channels, things get weird. I want to use all 48 pins for outputs. Did you happen to figure out a solution?

fiatlux

I'm also having this same issue. Anyone out there with a solution?

Go Up