OK, so I think I improved my code (in theory... it just doesn’t work yet. is giving back semi-random values... might have a wrong port selected or something) and since I will let this rest until tomorrow, I thought I would check to see whether someone can tell me if I am on the right path or not.
I need the array of binary values, because of the wiring of my sensors. Using the array allows me to call the pins in the order I want to read them, rather than in sequential order. This saves me a step of re-ordering them later. (Do you think this is silly? as in, am I wasting processing power on the arduino, as I could just do this on my computer?)
I am using DDRB to set pins 4,5,6,7 of register B of the arduino mega high (corresponding to pins 10,11,12,13)
I then use PORTB to write the binary values stored in muxInput[] to the digital outputs.
Is this correct so far?
Quick question to see if I got this right PORTB = B01000010 will set pin 1 & 6 of Port B high. Correct?
I am using this data sheet for selecting ports. Does anyone know any more extensive documentation on this? (for the arduino mega?)
https://spreadsheets.google.com/ccc?key=roX9D5pGUrS4muSBJysz1QQ#gid=0Anyway, here is the code:
Quick question to see if I got this right PORTB = B01000010 will set pin 1 & 6 of Port B high. Correct?
I am using this data sheet for selecting ports. Does anyone know any more extensive documentation on this? (for the arduino mega?)
https://spreadsheets.google.com/ccc?key=roX9D5pGUrS4muSBJysz1QQ#gid=0Anyway, here is the code:
int muxInput[16]={ B00000000, B01000000, B10000000, B11000000, B00010000, B01010000, B10010000,
B11010000, B00100000, B01100000, B10100000, B11100000, B00110000, B01110000, B10110000, B11110000};
//Signal pin for each individual multiplexer
int mux1 = 3;
int mux2 = 2;
int mux3 = 1;
int mux4 = 0;
void setup(){
DDRB = B11110000;
//initate serial
Serial.begin(9600);
}
void loop(){
//read the multiplexers & display the values
/*
I only use 8 pins of multiplexer 4, thats why
I have two different conditions for i < 8 and i < 15
Depending on how many sensors you are reading, you will
probably want to change this.
*/
for(int i = 0; i < 16; i ++){
if (i < 8) {
// read all 4 multiplexers
setMux(i);
Serial.print(analogRead(mux1));
Serial.print(",");
Serial.print(analogRead(mux2));
Serial.print(",");
Serial.print(analogRead(mux3));
Serial.print(",");
Serial.print(analogRead(mux4));
Serial.print(",");
}
else if (i < 15) {
//read multiplexer 1,2 & 3 only
setMux(i);
Serial.print(analogRead(mux1));
Serial.print(",");
Serial.print(analogRead(mux2));
Serial.print(",");
Serial.print(analogRead(mux3));
Serial.print(",");
}
else {
//read multiplexer 1,2 & 3 and then start new line
setMux(i);
Serial.print(analogRead(mux1));
Serial.print(",");
Serial.print(analogRead(mux2));
Serial.print(",");
Serial.print(analogRead(mux3));
Serial.println(" ");
}
}
}
//function for setting control pins
void setMux(int input){
PORTB = muxInput[input];
}