Pushbutton Bounce on 74HC165 ...?

Hi all,

I am doing some simple tests to see, if I could implement an 74HC595 as well as an 74HC165 into my project for extending digital Inputs and digital Outputs.

Since I do not have an 74HC165 chip here, which I can use on my Breadboard atm, I am using an online Simulation tool do create the code and do some tests.

As of now, this simulator is working quite well - but now, I run into an issue with my code - where I am not sure, what's causing it...

Here's my current sketch:

// Front Torpedo-Launcher LED 74HC 595 Q0
// Front Torpedo-Launcher BTN 74HC 165 D0
// Back Torpedo-Launcher LED 74HC 595 Q1
// Back Torpedo-Launcher BTN 74HC 165 D1

// 74HC 595 DS = 13     (Data)
// 74HC 595 STCP = 12   (Latch)
// 74HC 595 SHCP = 8    (Clock)

// 74HC 165 Q7 = 7      (Data)
// 74HC 165 PL =        (Latch)
// 74HC 165 CP =        (Clock)

const int _srOutData = 13;
const int _srOutClock = 8;
const int _srOutLatch = 12;

const int _srInData = 7;
const int _srInClock = 2;
const int _srInLatch = 4;

const int _srOutNumBits = 8;
const int _srInNumBits = 8;

int _WriteData[_srOutNumBits];
int _ReadData[_srInNumBits];

bool _FrontTorpedoLauncherBTNLastState;
bool _FrontTorpedoLauncherBTNCurrentState;

void setup() {
  // put your setup code here, to run once:
  pinMode(_srInData, INPUT);

  pinMode(_srOutData, OUTPUT);
  pinMode(_srInClock, OUTPUT);
  pinMode(_srOutClock, OUTPUT);
  pinMode(_srInLatch, OUTPUT);
  pinMode(_srOutLatch, OUTPUT);

  Serial.begin(9600);
  Serial.println(F("Start..."));
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(_srInLatch, LOW);
  digitalWrite(_srInLatch, HIGH);

  // Read each Torpedo-LaunchButton
  for(int i = 0; i < _srInNumBits; i++) {
    _ReadData[i] = digitalRead(_srInData);

    digitalWrite(_srInClock, HIGH); // Shift out the next bit
    digitalWrite(_srInClock, LOW);
  }

  //Serial.print("Button-State change to: ");
  //Serial.println(_ReadData[7]);


  _FrontTorpedoLauncherBTNCurrentState = _ReadData[7];
  if(_FrontTorpedoLauncherBTNCurrentState != _FrontTorpedoLauncherBTNLastState)
  {
    Serial.print("Button-State change to: ");
    Serial.println(_ReadData[7]);

    _FrontTorpedoLauncherBTNLastState = _FrontTorpedoLauncherBTNCurrentState;    
  }
}

In short:
two Pushbuttons were connected to the 74HC 165.
I've noticed, that the Input Pin for the 165 will be set to high when I push the button - and then stays high until the next push.
Since I want to use the pushbutton as a trigger for a specific action, I only need to know the change of the state - this is why I have this part of code:


int _WriteData[_srOutNumBits];
int _ReadData[_srInNumBits];

bool _FrontTorpedoLauncherBTNLastState;
bool _FrontTorpedoLauncherBTNCurrentState;

  // Read each Torpedo-LaunchButton
  for(int i = 0; i < _srInNumBits; i++) {
    _ReadData[i] = digitalRead(_srInData);

    digitalWrite(_srInClock, HIGH); // Shift out the next bit
    digitalWrite(_srInClock, LOW);
  }

  _FrontTorpedoLauncherBTNCurrentState = _ReadData[7];
  if(_FrontTorpedoLauncherBTNCurrentState != _FrontTorpedoLauncherBTNLastState)
  {
    Serial.print("Button-State change to: ");
    Serial.println(_ReadData[7]);

    _FrontTorpedoLauncherBTNLastState = _FrontTorpedoLauncherBTNCurrentState;    
  }

With each state of the Input, the variable "current state" should be set and compared with the last state... pretty simple - and just working as expected, if I add a delay of 100 to the code after the if block...

  _FrontTorpedoLauncherBTNCurrentState = _ReadData[7];
  if(_FrontTorpedoLauncherBTNCurrentState != _FrontTorpedoLauncherBTNLastState)
  {
    Serial.print("Button-State change to: ");
    Serial.println(_ReadData[7]);

    _FrontTorpedoLauncherBTNLastState = _FrontTorpedoLauncherBTNCurrentState;    
  }

  delay(100);

but without delay, the variable change looks like this:

Start...
Button-State change to: 1
Button-State change to: 0
Button-State change to: 1
Button-State change to: 0
Button-State change to: 1

It will always be trigered 4 or 5 times in a row - and I do not understand why...
I already tried to disable the Bounce of the pushbutton within the simulator ... but it seems that this did not change the behave in my case ...

Do you have an idea, how I could get rid of this (without using any kind of delay in this part of code?)

Thanks

Please provide a readable circuit diagram without Fritzing decoration. Your button connections look wrong to me.

Buttons should pull pins low when pressed. A pullup resistor should pull the pin high when released.

ok, thanks :slight_smile:
Actually, that's not "Fritzing" - that's an online simulator... I'll try to change this accordingly and will come back later.

the pull-up or pull-down resistor is connected directly to the pin and the switch to ground or +5V respectively, not in series with the switch.

the resistor pulls the pin one way and the switch the other way.

1 Like

You have no bypass caps for your chips. That may work on a simulator but can cause strange malfunctions that can waste a lot of your time if you forget them in a real circuit.

Also if you plan to add 8 LEDs to the '595, increase your series resistors to at least 390R to avoid exceeding the chip's 70ma limit.

1 Like

I already tried to disable the Bounce

Do you mean you tried to disable the bounce, or that you tried disabling the bounce? There’s a difference…

How does your simulator afford debounce pushbuttons?

I’ve been using

after being variously disappointed by several others over the years.

Did I miss the link to your simulation?

a7

Hi, I am also using the Wokwi Simulator...
and at least, there is an attribute for the buttons which should disabling the bounce...

{
      "type": "wokwi-pushbutton",
      "id": "btn2",
      "top": -125.53,
      "left": 256.72,
      "attrs": { "color": "red", "bounce": 0, "key": "F1", "label": "TorpedoLauncher Front" }
    },

Anyway ... I think I've now found an issue in my code.
After some of the changes I've made, I noticed, that my Serial Monitor is still putting out multiple triggers of the "Button Pushed" event.

But it seems, that this is now related to some of my added code, because I compare the current Button State with the last Button State - and right now, the state will change when the Button will be pushed - and released.

So - it might now work... I still need to do some corrections on my Code... I will update here soon.

What is the usefulness of disabling bounce in your simulation? Are you planning on using bounce free switches in your actual circuit?

Good. The bounce attribute works.

Did I miss the link to your wokwi?

a7

It's an easy way to rule out a bounce, a bad algorithm for debouncing and/or just postponing adding proper real debouncing.

And to do a sim w/o needing to worry about it, even if you know that you'd have to do IRL, as a convenience.

I use it quite a bit.

a7

I will later use some capacitive touch sensors - but they are not available in the Simulator.
Therefore, I want to avoid any "issue" that could be related to the buttons itself (such as bouncing, etc)

OK, my current attempt seems to work...
The input will change as long as the button is pressed.

But:
I need some further help on the code :frowning:

void loop() {
  digitalWrite(_srInLatch, LOW);
  digitalWrite(_srInLatch, HIGH);
   
  byte _dataIn;
  
  digitalWrite(_srInClock, HIGH);
  digitalWrite(_srInClockEn, LOW);

  _dataIn = shiftIn(_srInData, _srInClock, MSBFIRST);
  digitalWrite(_srInClockEn, HIGH);

  Serial.print("Data of _dataIn: ");
  Serial.println(_dataIn, BIN);
}

this will provide _dataIn as following:

Data of _dataIn: 11111111
Data of _dataIn: 11111110
Data of _dataIn: 11111110
Data of _dataIn: 11111111

Since I need the specific Bit 7 (here change from 1 to 0), I am struggling a bit how I can access this.

is there a way to read the _dataIn as an array?
Can I assign the Bits from the 165 directly into an array?

If you posted a link to your wokwi, some of us might be more interested in playing with your code and helping you find whatever it is that makes it not do what you want, or do something you don’t want it to.

Just sayin’.

a7

Hi @alto777
sorry - I haven't yet used the "share" function on wokwi - and since I haven't any account yet, I was not sure, if sharing the project would work.

Right now, it's nothing else than a 74HC165 with two buttons attached,
A small code for testing and a 74HC595 with two LEDs attached.... but as of now, no code for the LED is implemented...

But here's the link to the project:

THX NP.

wokwi is weird a bit.You don’t need an account, when you share we can only make a copy for ourselves so yours, whoever owns it, won’t change.

If you get an account, you can ”lock” anything you share: I do that so I don’t inadvertently change a shared project which woukd make trash of a thread discussing it…

Evidently the wokwi boys have an infinite a great deal of space to store the projects, my own account is littered with one-offs built for use here, and I’ve also created quite a few “anonymous maker” sims, goodness knows how many from all of us are just clogging up somewhere.

I’ve managed to lose some work because I don’t quite get the renaming, saving, anonymity and so forth, so I am also in the habit of grabbing off any brilliant source code I manage to create and saving it on my own storages.

Which of course are tots beautifully clean and managed and I can always find what I am looking for no matter it was Tuesday or 2017. :wink:

a7

ok, I've created an account now... considering, how often I started creating test projects, I think, it would make sense having an account and some oppertunities to save stuff I did there :smiley:

so, comming back to my initial issue:

Right now, the Bits of the 74HC 165 will be written into byte VarX by

  digitalWrite(_srInLatch, LOW);
  digitalWrite(_srInLatch, HIGH);
   
  byte _dataIn;
  
  digitalWrite(_srInClock, HIGH);
  digitalWrite(_srInClockEn, LOW);

  _dataIn = shiftIn(_srInData, _srInClock, MSBFIRST);
  digitalWrite(_srInClockEn, HIGH);

The output of byte _dataIn is 11111111 or - when the button is pushed 11111110

I need to only work with the last bit of this - which is either 1 or 0...
So I thought, there should be a way to read this var somehow as an array like:
_dataIn[7] (will then be 1 or 0)

You know, what I mean?


  digitalWrite(_srOutLatch, LOW);
  shiftOut(_srOutData, _srOutClock, MSBFIRST, ~_dataIn);
  digitalWrite(_srOutLatch, LOW);

I am off to the beach, but I did see this, haven't tried changing it.

a7

That's bit 0, not bit 7!

Try
bitRead(_dataIn, 0);

1 Like

thanks, that exactly, what I wanted :slight_smile:
In my previous code, I've tried to read the data into an array (which has worked, but had other issues) and there, I had to use position 7 of the array ... so maybe, it was a bit misleading or something like that... sorry.

I know what you meant :wink:

It would be the last bit to arrive from the '165, so if you were filling an array starting from index 0, that bit would get stored in index 7.

But because of the "Most Significant Bit First" setting

_dataIn = shiftIn(_srInData, _srInClock, MSBFIRST);

the last bit to arrive gets stored in bit 0 of the byte.