I have been pulling my hair out for hours trying to get the below code to work. It compiles and uploads and works in Max, but the values are just noise.... Any ideas how to get the multiplexer to work properly?
// Code for multiplexing Analog inputs and getting all the others into MaxMSP
//based on Andrew Benson's sketch
// 16 channels
int r0 = 0;
int r1 = 0;
int r2 = 0;
int r3 = 0;
int row = 0;
int ID = 0;
char analogValue[96];//array of analog values
char current=0;//current position of analog value in array
int digVal;//digital pins bits are packed into a single variable
char imask = 128;//index bytes start with 1
char theEnd = 255;//byte to signal message end to Max patch
//set multiplexer control pins
#define mpinA 5
#define mpinB 6
#define mpinC 7
#define mpinD 8
//bit-packed truth tables per pin for multiplexer
int bin [] = {0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111};
//select analog channels to multiplex
char is_muxPIN[6] = {1,0,0,0,0,0};//currently just uses A0. change to multiplex other inputs
char muxcount;//variable to store the number of multiplexed inputs
void setup(void) {
Serial.begin(9600);
digVal=0;
muxcount=0;
for (int i = 0;i<6;i++){
muxcount+=is_muxPIN[i];
}
for (int i = 2;i<14;i++){
digitalWrite(i,HIGH);//enable pullups
}
if(muxcount>0){
pinMode(mpinA,OUTPUT);
pinMode(mpinB,OUTPUT);
pinMode(mpinC,OUTPUT);
pinMode(mpinD,OUTPUT);
}
while (establishContact()==0){delay(100);} //wait for 99 byte
}
//uses serial.write() to avoid needless symbol creation in MaxMSP
void loop() {
//read analog pins
for (int g = 0;g<6;g++){
packValue(g);
}
char total = current+1;
sendOFF(total);//send everything to Max
current=0;//reset analog value counter
delay(10);//wait 10 milliseconds
//see if someone tried to turn us off:
if(establishContact()==1) {
while(establishContact()==0) {delay(100);}//go into idle mode
}
}
void sendOFF(char total){
//Send analog values in the format 0x81 a1 a2.....0xFF
Serial.write(imask|1);
for (int i = 0;i<total;i++){
Serial.write(analogValue[i]);
}
Serial.write(theEnd);//ends analog stream
}
//read an analog pin and then pack into low/high bytes
void packValue(int index) {
if(is_muxPIN[index]==1) muxAnalog(index);
else regAnalog(index);
}
char establishContact(void){
if (Serial.available() > 0) {
char checkup = Serial.read();
if (checkup==99) return 1;
else return 0;
}
else return 0;
}
//function for handling multiplexed analog reading
void muxAnalog(char index){
if (Serial.available()) {
ID = Serial.read();
Serial.print(ID);
}
row = bin[ID];
r0 = row & 0x01;
r1 = (row>>1) & 0x01;
r2 = (row>>2) & 0x01;
r3 = (ID>7)? 1:0;
digitalWrite(5, r0);
digitalWrite(6, r1);
digitalWrite(7, r2);
digitalWrite(8, r3);
delayMicroseconds(50);//allow analog signal to settle
regAnalog(index);
}
//read an analog pin
void regAnalog(char index) {
int tempA = analogRead(index);
analogValue[current]=tempA & 127;
current++;
analogValue[current] = (tempA>>7);
current++;
}
The thing is I have tried to combine 2 examples of multiplexer code....one that used the old 405 multiplexer that only needs 3 digital control pins (this is the one that comes into Max in a usable format) and an example for the 406 multiplexer, that you have to set 4 pins in the code. Plus I am a total beginner with the language..
But if I just replace the messed up binary bit with ID, i get errors....
//bit-packed truth tables per pin for multiplexer
int bin [ID];
//select analog channels to multiplex
char is_muxPIN[6] = {1,0,0,0,0,0};//currently just uses A0. change to multiplex other inputs
char muxcount;//variable to store the number of multiplexed inputs
Similar problem. I've been trying yo make this thing work with no success. When I attach anything to the inputs of the multiplexer, nothing happens (the serial keeps printing zero as value for each pin). Where did I go wrong?
int count;
int pinval;
int s0=6; // control pins
int s1=7;
int s2=8;
int s3=9;
Things like pin numbers are generally constant (or arrays of constants), so it is best to make them such.
It helps to avoid problems like yours earlier.