Use analog pins for s0-3 with Lightweight-CD74HC4067-Arduino

edit :: this also happens with the non-light version of the library

edit2 :: ok,, I've moved a serial println above and below the my_mux call (or mux call in the light version) ,, and the for loop will do everything above the my_mux call correctly,, but then the my_mux call somehow exists outside of the loop, and runs once after the loop is completed. ??

final edit:: I figured it out. the other lines don't need a {} around the loop, but for some reason using the mux library,, I need to put the loop in those brackets or the mux call falls outside of it, along with everything below the mux call.
I'm trying to make a game controller using this multiplexer to read 6 joysticks.

If I cancel out the mux code,, everything loops the right number of times,, but with the mux code,, it doesn't even loop through the analog section of the code. If I use the mux call in the loop,, it does the loop once and says i is 12, when i should be LESS than 12 for anything to happen.

I wonder if the problem is that this library doesn't allow me to use analog pins to address the mux?? I don't see how to change that.

I know the code is a mess but I'm no good at code,, and yes I know the lib is in twice but it doesn't have any effect. its' just the state right now....

#include <light_CD74HC4067.h>

#include "ibus.h"
#include "light_CD74HC4067.h"


// //////////////////
// Edit here to customize

// How often to send data?
#define UPDATE_INTERVAL 10 // milliseconds


// 1. Analog channels. Data can be read with the Arduino's 10-bit ADC.
// This gives values from 0 to 1023.
// Specify below the analog pin numbers (as for analogRead) you would like to use.
// Every analog input is sent as a single channel.
#define NUM_ANALOG_INPUTS 12
byte analogPins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; // element count MUST be == NUM_ANALOG_INPUTS


// 2. Digital channels. Data can be read from Arduino's digital pins.
// They could be either LOW or HIGH.
// Specify below the digital pin numbers (as for digitalRead) you would like to use.
// Every pin is sent as a single channel. LOW is encoded as 0, HIGH - as 1023
#define NUM_DIGITAL_INPUTS 12
byte digitalPins[] = {2,3,4,5,6, 7,8,9,10,11,12,13}; // element count MUST be == NUM_DIGITAL_INPUTS


// 3. Digital bit-mapped channels. Sending a single binary state as a 16-bit
// channel is pretty wasteful. Instead, we can encode one digital input
// in each of the 16 channel bits.
// Specify below the digital pins (as for digitalRead) you would like to send as
// bitmapped channel data. Data will be automatically organized in channels.
// The first 16 pins will go in one channel (the first pin goes into the LSB of the channel).
// The next 16 pins go in another channel and so on
// LOW pins are encoded as 0 bit, HIGH - as 1.
#define NUM_DIGITAL_BITMAPPED_INPUTS 0
byte digitalBitmappedPins[] = {}; // element count MUST be == NUM_DIGITAL_BITMAPPED_INPUTS


// Define the appropriate analog reference source. See
// https://www.arduino.cc/reference/en/language/functions/analog-io/analogreference/
#define ANALOG_REFERENCE DEFAULT

// Define the baud rate
#define BAUD_RATE 115200

// /////////////////





#define NUM_CHANNELS ( (NUM_ANALOG_INPUTS) + (NUM_DIGITAL_INPUTS) + (15 + (NUM_DIGITAL_BITMAPPED_INPUTS))/16 )

CD74HC4067 mux(A0,A1,A2,A3); // mux(s0,s1,s2,s3), where s3 is the highest select line (MSB)
const int signal_pin = A4; // Pin A4 - Connected to Sig pin of CD74HC4067
IBus ibus(NUM_CHANNELS);

void setup()
{
  analogReference(ANALOG_REFERENCE); // use the defined ADC reference voltage source
  Serial.begin(BAUD_RATE);           // setup serial
}

void loop()
{
  int i, bm_ch = 0;
  unsigned long time = millis();

  //ibus.begin();

  // read analog pins - one per channel
  for(i=0; i < NUM_ANALOG_INPUTS; i++)
   // mux.channel(i);
    Serial.println("-" + String(i));
    //int val = analogRead(signal_pin);
    //Serial.println("Channel "+String(i)+": "+String(val));
    //ibus.write(analogRead(signal_pin));
   // Serial.println(String(analogPins[i]) + " - " + String(analogRead(signal_pin)));
   // Serial.println(analogPins[i]);
  // read digital pins - one per channel
  for(i=0; i < NUM_DIGITAL_INPUTS; i++)
    //ibus.write(digitalRead(digitalPins[i]) == HIGH ? 1023 : 0);
    Serial.println("--" + String(i));
  // read digital bit-mapped pins - 16 pins go in one channel
  for(i=0; i < NUM_DIGITAL_BITMAPPED_INPUTS; i++) {
  	int bit = i%16;
  	if(digitalRead(digitalBitmappedPins[i]) == HIGH)
  		bm_ch |= 1 << bit;

  	if(bit == 15 || i == NUM_DIGITAL_BITMAPPED_INPUTS-1) {
  		// data for one channel ready
  		//ibus.write(bm_ch);
      Serial.println("--");
  		bm_ch = 0;
  	}
  }

  //ibus.end();
  Serial.println("end");
  time = millis() - time; // time elapsed in reading the inputs
  if(time < UPDATE_INTERVAL)
    // sleep till it is time for the next update
    delay(UPDATE_INTERVAL  - time);
}

Can you add a schematic and also tell us what sort of Arduino you are using please.

What do you think this is doing?

1 Like

Post a annotated schematic with links to "Technical information" on all of the hardware devices. You should be setting your channel before trying to read the A/D. The part works well if properly connected.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.