Go Down

Topic: Fuse control with Muxshield from mayhew Labs /Arduino Uno (Read 589 times) previous topic - next topic

lvanoppen

Hi

i need to read 24inputs (digital) to control fuses. i only need 1 or 0 / good or damaged.

Hardware:
Arduino UNO
Muxshield Mayhew Labs

Program from mayhewlabs
Code: [Select]
//Mux_Shield_DigitalIn_Example
//http://mayhewlabs.com/arduino-mux-shield

/*
This example shows how to read and store all pins as digital inputs into arrays and print the results over serial.
Switches should be connected from ground to the pin input.
Internal pull-up resistors are used on the inputs, so when a switch is depressed, the reading
changes from 1 to 0, but to correct this logic, the digitalRead function is corrected with '!'
to actually store a 1 when a switch is depressed.

To simplify this code further, one might use nested for loops or function calls.
*/

//Give convenient names to the control pins
#define CONTROL0 5   
#define CONTROL1 4
#define CONTROL2 3
#define CONTROL3 2

//Create arrays for data from the the MUXs
//See the Arduino Array Reference: http://www.arduino.cc/en/Reference/Array
int mux0array[16];
int mux1array[16];
int mux2array[16];

void setup()
{
  //Set MUX control pins to output
  pinMode(CONTROL0, OUTPUT);
  pinMode(CONTROL1, OUTPUT);
  pinMode(CONTROL2, OUTPUT);
  pinMode(CONTROL3, OUTPUT);
 
  //Open the serial port at 28800 bps
  Serial.begin(4800);
 
  //Set analog pins to digital input 
  pinMode(14, INPUT);         
  pinMode(15, INPUT);
  pinMode(16, INPUT);
 
  //Turn on pullup resistors
  digitalWrite(14, HIGH);       
  digitalWrite(15, HIGH);
  digitalWrite(16, HIGH);
}
 

void loop()
{
  //This for loop is used to scroll through and store the 16 inputs on the FIRST multiplexer
  for (int i=0; i<16; i++)
  {
    //The following 4 commands set the correct logic for the control pins to select the desired input
    //See the Arduino Bitwise AND Reference: http://www.arduino.cc/en/Reference/BitwiseAnd
    //See the Aruino Bitshift Reference: http://www.arduino.cc/en/Reference/Bitshift
    digitalWrite(CONTROL0, (i&15)>>3);
    digitalWrite(CONTROL1, (i&7)>>2); 
    digitalWrite(CONTROL2, (i&3)>>1); 
    digitalWrite(CONTROL3, (i&1));     
   
    //Read and store the input
    //Since internal pullup is on, the pin goes low on changing, so the value needs to be flipped from 0 to 1 (!)
    mux0array[i] = !digitalRead(14);
  }
 
  //This for loop is used to scroll through the SECOND multiplexer
  for (int i=0; i<16; i++)
  {
    digitalWrite(CONTROL0, (i&15)>>3);
    digitalWrite(CONTROL1, (i&7)>>2); 
    digitalWrite(CONTROL2, (i&3)>>1); 
    digitalWrite(CONTROL3, (i&1));     
    mux1array[i] = !digitalRead(15);
  }
 
  //This for loop is used to scroll through the THIRD multiplexer
  for (int i=0; i<16; i++)
  {
    digitalWrite(CONTROL0, (i&15)>>3);
    digitalWrite(CONTROL1, (i&7)>>2); 
    digitalWrite(CONTROL2, (i&3)>>1); 
    digitalWrite(CONTROL3, (i&1));     
    mux2array[i] = !digitalRead(16);
  }   
 
  //The following lines are for printing out results of array0
  Serial.print("mux0array: ");
  for (int i=0; i<16; i++)
  {
    Serial.print(mux0array[i]);
    Serial.print("-");
  }
  Serial.println();  //line feed
 
  //The following lines are for printing out results of array1
  Serial.print("mux1array: ");
  for (int i=0; i<8; i++)
  {
    Serial.print(mux1array[i]);
    Serial.print("-");
  }
  Serial.println();
}


i would like to readout on a display but first in the serial monitor.
i can see in the serial monitor the incomming signal with 1 or 0 but i would like to know how i can set in the serial monitor some text like Fuse 1 Damaged if the input 1 is broken.
i only use 24 of the 48inputs.

do i need to change the library or what?


johnwasser

Here is a function that will allow you to read any of the 48 MUX pins as digital inputs.  Just use digitalReadMuc(pin) like you would digitalRead(pin).  Use pin numbers from 0 to 47 for each of the 48 mux inputs.
Code: [Select]

//Give convenient names to the control pins
const int MUXCONTROL3 = 5;   
const int MUXCONTROL2 = 4;
const int MUXCONTROL1 = 3;
const int MUXCONTROL0 = 2;

void setup() {
    pinMode(MUXCONTROL0, OUTPUT);
    pinMode(MUXCONTROL1, OUTPUT);
    pinMode(MUXCONTROL2, OUTPUT);
    pinMode(MUXCONTROL3, OUTPUT);
}


int digitalReadMux(int pin) {  // pin is 0 to 47

     int mux = (pin / 16) % 3;  // Which MUX input

     switch (mux) {
     case 0: pinMode(A0, INPUT_PULLUP); break;
     case 1: pinMode(A1, INPUT_PULLUP); break;
     case 2: pinMode(A2, INPUT_PULLUP); break;
     }

    //  Set the mux address pins
    digitalWrite(MUXCONTROL3, pin & 8);
    digitalWrite(MUXCONTROL2, pin & 4); 
    digitalWrite(MUXCONTROL1, pin & 2); 
    digitalWrite(MUXCONTROL0, pin & 1);

     switch (mux) {
     case 0: return digitalRead(A0);
     case 1: return digitalRead(A1);
     case 2: return digitalRead(A2);
     }
}
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

lvanoppen

Thanks for the reply

but i think i don't really get it.
I tried a small void loop just for one Input of the muxshield, but it would not work. Or i'am just doing it wrong.

Or do i need to add something to your void setup? Or int?

Code: [Select]
void loop(){

if (int digitalReadMux(int pin0)== LOW);  //Pin0 from muxshield
Serial.println("Fuse 1 damaged"); //Print in serial monitor
  delay(1000); //wacht 1 seconde
 
}

johnwasser

Perhaps you meant to write:

Code: [Select]
void loop() {
    if (digitalReadMux(0)== LOW) {  //Pin0 from muxshield
        Serial.println("Fuse 1 damaged"); //Print in serial monitor
        delay(1000); //wacht 1 seconde
    }
}
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

lvanoppen

Thanks a lot.

I don't no why bud, i trought I had tried it like this.
Thanks for helping. Now it works

Go Up