Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

digital input...issue after reading 8th input #2

Open
snoophuman opened this issue Sep 25, 2020 · 7 comments
Open

digital input...issue after reading 8th input #2

snoophuman opened this issue Sep 25, 2020 · 7 comments

Comments

@snoophuman
Copy link

snoophuman commented Sep 25, 2020

Hey there! greatly appreciate your work. It seems to work flawlessly when reading analog input from 16 potentiometers annnd writing to 16 LEDS. Aw yeee!

I was trying out using it for reading digital input and it seems to work pretty well for buttons I have wired up to 0-7 on the multiplexer but after that for some reason if I have buttons wired up for pins 8-16 things get weird.

Heres a copy of the code I've cobbled together.
Note: I have wired up things as such:

en-> pin 24
s0-> pin 12
s1-> pin 11
s2-> pin 10
s3-> pin 19
sig -> pin 8
Each button i have wired up has a leg attached to a resistor to gnd.

/**
 * This example demonstrates how to write digital signals
 * It assumes there are LEDs+resistors with the positive lead of the LEDs
 * connected to the 16 channels of the 74HC4067 mux/demux, respectively
 * 
 * For more about the interface of the library go to
 * https://github.com/pAIgn10/MUX74HC4067
 */

#include "MUX74HC4067.h"

// Creates a MUX74HC4067 instance
// 1st argument is the Arduino PIN to which the EN pin connects
// 2nd-5th arguments are the Arduino PINs to which the S0-S3 pins connect
MUX74HC4067 mux(24, 12, 11, 10, 9);

void setup()
{
	// Configures how the SIG pin will be interfaced
	// e.g. The SIG pin connects to PIN 3 on the Arduino,
	//      and PIN 3 is a Digital Output
	mux.signalPin(8, INPUT, DIGITAL);
}

// Writes to the 16 channels a HIGH value, one after the other
void loop()
{

  int data;
	for (int i = 0; i < 16; ++i)
	{
		// Connects to channel i and writes HIGH
		data = mux.read(i);
    if ( data == HIGH ) Serial.println(i);
		delay(25);
	}
	
	mux.disable();  // Disconnects the SIG pin from any channel
	delay(25);
}`

if i press my button 0 it will print to serial both 0 and 8 for some reason...

@snoophuman
Copy link
Author

snoophuman commented Sep 25, 2020

ive wrote some additional code which includes output to LEDS so you can visualize the problem a bit better and recorded a video...seems to be inputs 0,1 and 2 that are having this issue.

heres the new code

/**
 * This example demonstrates how to write digital signals
 * It assumes there are LEDs+resistors with the positive lead of the LEDs
 * connected to the 16 channels of the 74HC4067 mux/demux, respectively
 * 
 * For more about the interface of the library go to
 * https://github.com/pAIgn10/MUX74HC4067
 */

#include "MUX74HC4067.h"

// Creates a MUX74HC4067 instance
// 1st argument is the Arduino PIN to which the EN pin connects
// 2nd-5th arguments are the Arduino PINs to which the S0-S3 pins connect
MUX74HC4067 mux(24, 12, 11, 10, 9);
MUX74HC4067 muxLEDS(6, 5, 4, 3, 2);
void setup()
{
	// Configures how the SIG pin will be interfaced
	// e.g. The SIG pin connects to PIN 3 on the Arduino,
	//      and PIN 3 is a Digital Output
	mux.signalPin(8, INPUT, DIGITAL);
 muxLEDS.signalPin(1, OUTPUT, DIGITAL);
}

// Writes to the 16 channels a HIGH value, one after the other
void loop()
{

  int data;
	for (int i = 0; i < 16; ++i)
	{
		// Connects to channel i and writes HIGH
		data = mux.read(i);
    if ( data == HIGH ) {Serial.println(i);
    muxLEDS.write(i,HIGH);}
		delay(25);
	}
	
	mux.disable();  // Disconnects the SIG pin from any channel
	delay(25);
}`

and heres the video
https://www.youtube.com/watch?v=NPcc54w_GcM&feature=youtu.be

@nlamprian
Copy link
Owner

Hi @snoophuman, thank you for the feedback (I really like the last experiment you put together).

I cannot test the code myself at the moment, so I gonna have to ask for your help.

I made a little change that I think might be causing the problem. Please give the unstable/en branch a try.

@snoophuman
Copy link
Author

I gave it a shot, no luck.
with the new code, if buttons 0-7 are pressed it flip flops between printing out a few different numbers. If i hold them down itll print the numbers repetitively.

When buttons 8-15 are pressed, nothing at all is printed.
here's a table of what happens when each button is pressed

button 0: 1, 8, 12, 1, 8, 12, 1, 8, 12, 1, 8, 12, 1, 8, 12,
button 1: 1, 9,13, 1, 9,13, 1, 9,13, 1, 9,13, 1, 9,13, 1,
button 2: 2, 10, 14, 2, 10, 14, 2, 10, 14, 2, 10, 14,
button 3. 11, 3. 11, 3. 11, 3. 11, 3. 11, 3. 11, 3. 11,
button 4: 4,12, 4,12, 4,12, 4,12, 4,12, 4,12, 4,12,
button 5: 5, 13, 5, 13, 5, 13, 5, 13, 5, 13, 5, 13,
button 6: 6, 14, 6, 14, 6, 14, 6, 14, 6, 14, 6, 14, 6, 14,
button 7: 7, 15, 7, 15, 7, 15, 7, 15, 7, 15, 7, 15, 7, 15,
button 8: nothing
button 9: nothing
button 10: nothing
button 11: nothing
button 12: nothing
button 13: nothing
button 14: nothing
button 15: nothing

here is an additional video

https://www.youtube.com/watch?v=3HR-CvC_5Os&feature=youtu.be

i dont know much about binary and shift registers but heres a copy of the datasheet...i noticed theres a table in there that has a lot of 1s and 0s...maybe that has something to do with it
https://www.sparkfun.com/datasheets/IC/cd74hc4067.pdf

heres a link to the product page as well just in case im using the wrong hardware or somethin
https://www.sparkfun.com/products/9056

@nlamprian
Copy link
Owner

I cannot associate what's happening in your circuit with something in the code. I'd like to say I tested everything when I created the library, but I cannot remember at this point. I'm gonna have to test this myself to see if there is anything wrong with the library. But this is not going to happen soon.

In the meantime, if you are willing to continue testing, you can try a few more things (with the original code):

  • use different pins on the teensy
  • replace the pcb with a circuit you build on the breadboard
  • use an arduino in place of the teensy

@snoophuman
Copy link
Author

good idea, ill try those options out! and let ya know how it goes

@snoophuman
Copy link
Author

snoophuman commented Sep 29, 2020

PROBLEM SOLVED!

It turns because I was using a Teensy (4.1 in this case) it caused an unexpected problem...
Teensys are much faster than Arduino. Apparently this caused problems with reading inputs.
Luckily PaulStoffregen from the Teensy forum had the solultion - adding a 5 millisecond delay to the setChannel() function in MUX74HC4067.cpp

HERE is a link to his post if you would like to read in more detail

simply changing this bit of code in MUX74HC4067.cpp

 void MUX74HC4067::setChannel(int8_t pin, uint8_t set)
{
	digitalWrite(enable_pin, HIGH);
	current_channel = pin;
	for (uint8_t i = 0; i < num_of_control_pins; ++i)
	{
		digitalWrite(control_pin[i], pin & 0x01);
		pin >>= 1;
	}
	enable_status = ENABLED;
	if ( set == ENABLED ) digitalWrite(enable_pin, LOW);
	
	
} 

to this

 void MUX74HC4067::setChannel(int8_t pin, uint8_t set)
{
	digitalWrite(enable_pin, HIGH);
	current_channel = pin;
	for (uint8_t i = 0; i < num_of_control_pins; ++i)
	{
		digitalWrite(control_pin[i], pin & 0x01);
		pin >>= 1;
	}
	enable_status = ENABLED;
	if ( set == ENABLED ) digitalWrite(enable_pin, LOW);
	
	delayMicroseconds(5);
} 

fixes everything.

I also simplified my code a little bit. Here is the final version of my code.
It reads input from 16 buttons on a breakout board I made (each wired with 1k pulldown resistors)
and lights up a corresponding LED on another little breakout board I made.

/**
 * This example demonstrates how to read digital signals
 * It assumes there are push buttons with pullup resistors
 * connected to the 16 channels of the 74HC4067 mux/demux
 * 
 * For more about the interface of the library go to
 * https://github.com/pAIgn10/MUX74HC4067
 */

#include "MUX74HC4067.h"

// Creates a MUX74HC4067 instance
// 1st argument is the Arduino PIN to which the EN pin connects
// 2nd-5th arguments are the Arduino PINs to which the S0-S3 pins connect
MUX74HC4067 muxBUTTONS(24, 12, 11, 10, 9);
MUX74HC4067 muxLEDS(7, 6, 5, 4, 3);

void setup()
{
	Serial.begin(9600);  // Initializes serial port
    // Waits for serial port to connect. Needed for Leonardo only
    while ( !Serial ) ;
	
	// Configures how the SIG pin will be interfaced
	// e.g. The SIG pin connects to PIN 3 on the Arduino,
	//      and PIN 3 is a digital input
	muxBUTTONS.signalPin(8, INPUT, DIGITAL);
  muxLEDS.signalPin(2, OUTPUT, DIGITAL);
}

// Reads the 16 channels and reports on the serial monitor
// if the corresponding push button is pressed
void loop()
{
	byte data;

	for (byte i = 0; i < 16; ++i) //cycles through inputs 0-15 on the multiplexer board
	{

		data = muxBUTTONS.read(i);		// Reads from channel i and returns HIGH or LOW

		//if the button is pressed - print out the number of the channel that has been pressed
		if ( data == HIGH ) {  
		  Serial.print("Push button at channel ");
                  Serial.print(i);
                  Serial.println(" is pressed");
                 muxLEDS.write(i,HIGH); //turn on the LED 
		} else if ( data == LOW ) {
                muxLEDS.write(i,LOW); //turns off the LED when the button is released
    }
   
	}

}

Lastly - here is a video of things in action: YOUTUBE

Huge thank you to you @nlamprian and another huge thanks to @PaulStoffregen ! You both rock!

@nlamprian
Copy link
Owner

nlamprian commented Sep 29, 2020

Awesome news 👍

Please give my branch (along with your fix) another shot if you will. It's something that should be fixed regardless.

And feel free to create a pull request with your fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants