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