Teensy LED brightness

Status
Not open for further replies.

mtiger

Well-known member
Hi guys,

So i'm running this code through a 4067 mux with 8 buttons for testing. The issue is, when testing each button from 1 - 7, the led comes on but at very low brightness. The eighth button displays the LED at full brightness. I've tested the connections on another sketch without using a mux array and address individually and it works, so the physical connections seem correct which leads me to believe its the code. Any ideas?

Code:
int ledPin = 13;

int muxAddressPins [4] = {2, 3, 4, 5};
int muxPin = 6; 

int muxChannel[8][4] =  {
  {0, 0, 0, 0}, //channel 0
  {1, 0, 0, 0}, //channel 1
  {0, 1, 0, 0}, //channel 2
  {1, 1, 0, 0}, //channel 3
  {0, 0, 1, 0}, //channel 4
  {1, 0, 1, 0}, //channel 5
  {0, 1, 1, 0}, //channel 6
  {1, 1, 1, 0}, //channel 7
};

void setup()  {
  Serial.begin(9600);
  for (int i = 0; i < 4; i++) {
    pinMode(muxAddressPins[i], OUTPUT);
  }
  pinMode(muxPin, INPUT_PULLUP);

  pinMode(ledPin, OUTPUT);
}

void loop()  {
  for (int j = 0; j < 8; j++) {
    for (int i = 0; i < 4; i++) {
      digitalWrite(muxAddressPins[i], muxChannel[j][i]);
      ledActive();
    }
  }
  delay(10);
}

void ledActive()  {
  if (digitalRead(muxPin) == LOW) {
    digitalWrite(ledPin, HIGH);
  } else  {
    digitalWrite(ledPin, LOW);
  }
}

Appreciate the help
 
Yes, think in terms of 'duty cycle'. As the code loops through the mux states, for all but one, it turns the LED off. So 7/8 of the time, during the for loops, the LED is off. Then if the LED happens to be on at the end of the loops, the delay(10) gives you a chance to see the LED at full brightness.

Make sense?
 
Thanks for your response Pictographer. I don't think i understand correctly.

I thought by providing two loops to go through the mux (address Pins & high / low) it would do it for all. So i'm not sure how I am communicating with them all and missing one, having the only one i've missed allow it to light to full brightness. I've tried to move around the delay(10); and all it does it make make the LED blink rapidly (at full brightness mind you).
 
Rather than changing the state of the LED inside the loops, try using the loops to figure out what the next state should be and only change the LED after the loops. This way you're not turning the LED off and on as you poll each button. If I understand correctly, you'd like the LED to turn on if any of the buttons are pressed, right?
 
Currently just testing that the communication through the arrays to the mux is correct and using the LED as a debug tool. Making sure the code is correct and that i'm writing and reading from the mux correctly. Perhaps there is a better way to do it.

For each of the 8 buttons pressed and released, the LED should turn on and off.
 
I adjusted the loop() to see if i understand your answer correctly. Now only button 8 lights the LED, and the other 7 don't.

Code:
void loop()  {
  int ledState = digitalRead(muxPin0);

  for (int j = 0; j < 8; j++) {
    for (int i = 0; i < 4; i++) {
      digitalWrite(muxAddressPins[i], muxChannel[j][i]);
    }
  }

  if (ledState == LOW) {
    digitalWrite(ledPin, HIGH);
  } else  {
    digitalWrite(ledPin, LOW);
  }
  delay(10);
}
 
If I understand the MUX correctly, the way to use it is to set the select lines (muxChannel) to one of the 8 states, then read the mux input. That will tell you if the button corresponding to the current mux state is pressed. What your code does is run through the mux states, without reading the button state for 7 of them. So, indeed, seven buttons are ignored.

With respect, I think you might be doing yourself a disservice by asking for help too quickly. If you think about what you're trying to do and what your code says to do, you'll figure it out for yourself and grow. But that's for you to decide.

Here's some untested code that I think will blink the LED when each button is pressed. If you hold multiple buttons, you'll get multiple blinks per loop, but you'd need an additional delay to see that.


Untested code:
Code:
void loop() {
  digitalWrite(ledPin, LOW);
  // Scan through the buttons looking for a button that is pressed.
  for (int j = 0; j < 8; j++) {
    // Set the mux state.
    for (int i = 0; i < 4; i++) {
      digitalWrite(muxAddressPins[i], muxChannel[j][i]);
    }
    // Read button j. Blink if it is pressed.
    if (digitalRead(muxPin0)) {
      digitalWrite(ledPin, HIGH);
      delay(100);
      digitalWrite(ledPin, LOW);
    }
  }
}
 
Thanks Pictographer. I had to alter your code slightly by adding:
if (digitalRead(muxPin0) == LOW) which managed to get it work

I don't believe i've asked for help too quickly, as i've been trying to figure out this solution all day and its nice to have another pair of eyes on it to see where I am going wrong.
 
Status
Not open for further replies.
Back
Top