Help with dead simple 74HCT4051/mux example sketch for buttons

Status
Not open for further replies.

propa

Well-known member
Hi,

Found this wonderful tutorial https://youtu.be/OgaeEiHemU4 and adapted some code.

Code:
int pin_Out_S0 = 1;
int pin_Out_S1 = 2;
int pin_Out_S2 = 3;
int pin_In_Mux1 = 4;
int pin_In_Mux2 = 5;
int Mux1_State[8] = {0};
int Mux2_State[8] = {0};
int led = 6;
void setup() {
  pinMode(pin_Out_S0, OUTPUT);
  pinMode(pin_Out_S1, OUTPUT);
  pinMode(pin_Out_S2, OUTPUT);
  pinMode(led, OUTPUT);
  pinMode(pin_In_Mux1, INPUT);
  //pinMode(pin_In_Mux2, INPUT);
  Serial.begin(9600);
  delay(20);
}

void loop() {
  updateMux();
  for(int i = 0; i < 8; i ++) {
    if(i == 7) {
      Serial.println(Mux1_State[i]);
      
    } else {
      Serial.print(Mux1_State[i]);
      Serial.print(",");
    }
  }
  
}

void updateMux () {
  for (int i = 0; i < 8; i++){
    digitalWrite(pin_Out_S0, HIGH && (i & B00000001));
    digitalWrite(pin_Out_S1, HIGH && (i & B00000010));
    digitalWrite(pin_Out_S2, HIGH && (i & B00000100));
    Mux1_State[i] = digitalRead(pin_In_Mux1);
    //Mux2_State[i] = digitalRead(pin_In_Mux2);
    delayMicroseconds(50);
  }
}

that should work right?

I've added the 50micro secs delay, but haven't got anything registering on serial monitor yet. I've only got two buttons hooked to 7hct4051 that shouldn't make a difference right? Powering the 74HCT4051 with 5v as well.

Here's the schematic: Screen Shot 2021-03-19 at 20.53.20.png
 
ok changed to "INPUT_PULLUP" and have now got a response on the serial monitor, but seems 1 button takes up two places in the array for some reason, that's an overshoot/undershoot problem, is the loop going too fast and falsely registering two places?

eg the serial line when single button is pressed is 1,0,0,1,1,1,1,1

should I be bouncing as well as multiplexing or something, what's the best route from here?

edit, just changed cpu speed from 600mhz to 24mhz and works fine. So if I wanted to use 600mhz, how best to proceed?
 
The delay is the wrong place.


Code:
void updateMux () {
  for (int i = 0; i < 8; i++){
    digitalWrite(pin_Out_S0, HIGH && (i & B00000001));
    digitalWrite(pin_Out_S1, HIGH && (i & B00000010));
    digitalWrite(pin_Out_S2, HIGH && (i & B00000100));

                            [B]// delay is needed here, before you read the pin
[/B]
    Mux1_State[i] = digitalRead(pin_In_Mux1);
    //Mux2_State[i] = digitalRead(pin_In_Mux2);

    delayMicroseconds(50);  [B]// not needed after you have read the pin[/B]
  }
}
 
Presumably you mean 600MHz, not 0.6Hz?

If you want to be pedantic, I would have ignored the statement altogether from lack of proper formatting.

In most human discourse, context is king, so as you rightfully pointed out, and assumed correctly the piece of information I was trying to convey, based on the fact Teensy's don't have a 600mhz option.
 
Status
Not open for further replies.
Back
Top