I need some advice on reading an 8-input multiplexer connected to 8 reed switches. The current program polls the inputs and produces an 8-character string result, i.e. 10010110, which is difficult to work with. I would like the output to be a single 8-bit byte so that the bitRead function can be used to easily determine the status of each switch.
Would appreciate any comments or suggestions on whether or not this is the right approach and how to go about it.
Thanks,
Below is the code but it produces only a string output. I'm not sure how to work with it but then again I can't figure out how to generate a byte or integer.
int Zin = 12; // input from multiplexer
int Z = 0;
String poll;
int r0 = 0;
int r1 = 0;
int r2 = 0;
int count = 0;
void setup(){
Serial.begin(9600);
pinMode(Zin, INPUT); // data from multiplexer
pinMode(2, OUTPUT); // s0
pinMode(3, OUTPUT); // s1
pinMode(4, OUTPUT); // s2
}
void loop () {
for (count=0; count<=7; count++) {
// select the bit
r0 = bitRead(count,0);
r1 = bitRead(count,1);
r2 = bitRead(count,2);
digitalWrite(2, r0);
digitalWrite(3, r1);
digitalWrite(4, r2);
// read the input pin:
int Z = digitalRead(Zin);
poll = poll + Z;
}
print1();
poll = 0;
}
void print1 () {
Serial.println(poll);
delay(500);
}
The string-concatenation operation in the existing code will quickly exhaust the available memory and crash your sketch - never use the String class if you can possibly avoid it, it will bite you hard.