Go Down

Topic: Function for multiplexing (Read 1 time) previous topic - next topic



I've been playing with multiplexers recently and was wondering if there's a way to create a function to read the inputs and print the result. I wrote a sketch that does not work for some reason:

void setup()
  //4051 digital control pins
  pinMode(7, OUTPUT); // s0
  pinMode(8, OUTPUT); // s1
  pinMode(9, OUTPUT); // s2
void loop(){
  int a=pinRead(0,0,0);
  int b=pinRead(0,0,1);
  int c=pinRead(0,1,0);
  int d=pinRead(0,1,1);
  int e=pinRead(1,0,0);
  int f=pinRead(1,0,1);
  int g=pinRead(1,1,0);
  int h=pinRead(1,1,1);

int pinRead(in bita,int bitb,int bitc){
  digitalWrite=(7,bita); // the three digital pins to control the multiplexer
  return p;

This one results an error message: assignment of function 'void digitalWrite(uint8_t,uint8_t)'

Any ideas? I will use 32 analog inputs, it would be awesome if I could read all of them with a function. I also wonder how could I make the counting for the select pins easier? So far I have hardcoded the values manually. How could I do it with bit shifting? This ( http://www.arduino.cc/playground/Learning/4051 ) particular example does something similar:

int r0 = 0;      //value of select pin at the 4051 (s0)

int r1 = 0;      //value of select pin at the 4051 (s1)

int r2 = 0;      //value of select pin at the 4051 (s2)

int count = 0;   //which y pin we are selecting

void setup(){

  pinMode(2, OUTPUT);    // s0

  pinMode(3, OUTPUT);    // s1

  pinMode(4, OUTPUT);    // s2


void loop () {

  for (count=0; count<=7; count++) {

    r0 = count & 0x01;

    r1 = (count>>1) & 0x01;

    r2 = (count>>2) & 0x01;

    digitalWrite(2, r0);

    digitalWrite(3, r1);

    digitalWrite(4, r2);

//Either read or write the multiplexed pin here


I need 16 inputs to read instead of 8 like in the example. Any ideas?


int pinRead(in bita,int bitb,int bitc){

there is a typo in there :)




Another thing is whether this statement digitalWrite=(8,bitb); is syntatically correct? Won't it be digitalWrite(8,bitb); ?

Yumnam Kirani Singh
Tronglaobi Awang Leikai


You have:-

  return p;

Which means that the print command is never reached because the function returns before getting to it. Swap those last two instructions over.

Go Up