7 years ago.

Array for analog multiplexer. Help.

Hello guys :)

I need a little help with the arrays, I have 8 pressure sensors connected to the analog multiplexer 4051 . The outputs of the MUX are three digital and 1 analog. I need to make an array represent the truth table of this digital pins. So to can at anytime to pick and read exactly this sensor which I want.

Any suggestions on how I can do it ?

Thank you.

1 Answer

7 years ago.

So you need a way to set 3 digital outputs to represent a number between 0 and 7? No need for an array, you can use a BusOut data type to set a group of pins to represent a number in binary.

e.g. to read your 8 sensors the code would be:

BusOut muxSelect(P1,P2,P3);
AnalogIn ain(P4);

main () {
  while (true) {
    for (int i = 0; i<7; i++) {
      muxSelect = i;
      wait_ms(1); // allow MUX settling time, we probably can't trust the output the instant we change the selection.
      printf("Input %d is reading %.2f\r\n",i, ain * 3.3);
    }
    wait(5);
  }
}

No I need to represend the truth table.For example when P1=0, P2=1, P3=0 can read the third sensor. Now I use this:

#include "mbed.h"
DigitalOut fsr_1(PC_15);
DigitalOut fsr_2(PC_14); // MUX PINS
DigitalOut fsr_3(PC_13);

void pressure_mode()
{
    
 while (1) 
   {
    
    fsr_1=0;
    fsr_2=0;
    fsr_3=0;
    wait(0.2);

  if (pressure < pressure_expect )
    {
       while (pressure < pressure_expect + 100)
          {
            valve_1 = 1;
            pump = 1;
            air_in = 1;
            air_out = 0;
            read_sensor();
            wait(0.2);
          }
    }
valve_1 = 0;

  if (pressure > pressure_expect )
    {
       while (pressure > pressure_expect )
         {
           valve_1 = 1;
           pump = 0;
           air_in = 0;
           air_out = 1;
           read_sensor();
           wait(0.2);
         }
    }
valve_1 = 0;
    
    fsr_1=1;
  //fsr_2=0;
  //fsr_3=0;
    wait(0.2);

  if (pressure < pressure_expect )
    {
       while (pressure < pressure_expect + 100)
          {
            valve_2 = 1;
            pump = 1;
            air_in = 1;
            air_out = 0;
            read_sensor();
            wait(0.2);
          }
    }
valve_2 = 0;

  if (pressure > pressure_expect )
    {
       while (pressure > pressure_expect )
         {
           valve_2 = 1;
           pump = 0;
           air_in = 0;
           air_out = 1;
           read_sensor();
           wait(0.2);
         }
    }
valve_2 = 0;

    fsr_1=0;
    fsr_2=1;
  //fsr_3=0;
    wait(0.2);

  if (pressure < pressure_expect )
    {
       while (pressure < pressure_expect + 100)
          {
            valve_3 = 1;
            pump = 1;
            air_in = 1;
            air_out = 0;
            read_sensor();
            wait(0.2);
          }
    }
valve_3 = 0;

  if (pressure > pressure_expect )
    {
       while (pressure > pressure_expect )
         {
           valve_3 = 1;
           pump = 0;
           air_in = 0;
           air_out = 1;
           read_sensor();
           wait(0.2);
         }
    }
valve_3 = 0;

Now I need to turn ON and OFF the fsr pins all the time. I need something which can take at this moment this combinations of pins and read this sensor. Thank you.

posted by Viktoria Vladimirova 30 Mar 2017

I'm confused, do you need:

sensor 0 - fsr1 = 0, fsr2 = 0, fsr3 = 0
sensor 1 - fsr1 = 1, fsr2 = 0, fsr3 = 0
sensor 2 - fsr1 = 0, fsr2 = 1, fsr3 = 0
sensor 3 - fsr1 = 1, fsr2 = 1, fsr3 = 0

If so that's exactly what my code gives you, just change the BusOut to being the correct pins in the order fsr3, fsr2, fsr1.

If not them please give me a truth table of what you want without all the other code (which really should be moved to a function rather than repeated so many times) cluttering it up.

posted by Andy A 30 Mar 2017

ahaha yes I know that this is confuse. I am new in cpp so for me that was the more easy way. Yes I need what you showed. Ok I will try with your code, but I dont understand how to turn on the right sensor, because your code is for counting? Thank you.

posted by Viktoria Vladimirova 03 Apr 2017

Look at the pattern your fsr pins are following, it is a binary count. Set it to 0 and you get the first input, 1 and you get the second etc. 3 outputs means you have a 3 bit binary number which represents a number between 0 and 7. BusOut gives you a simple way to set a group of pins to represent a binary number and so select the input you want.

posted by Andy A 03 Apr 2017

Yes I understand , I make it , Thank you very much.

posted by Viktoria Vladimirova 03 Apr 2017