Hello everyone,
the picture below shows my circuit which uses 4021 shift in register to get input from buttons and turn LEDs on. It seems my pins are bouncing if I keep my finger on the button non stop, so i tried a debounce method but doesn't seem to work out. What i need is to click the button, LED on. Click again, LED goes Off.
I am using an Arduino Nano.
(edited)
If I keep my finger on the pushbutton the LED will flash non-stop.
So I need help please on how to debounce the pins of the shift in register.
here is my code(edited):
//Define pins
int dataPin = 7; // Pin 0 of DigiSpark connected to Pin 3 of CD4021
int clockPin = 2; // Pin 1 of DigiSpark connected to Pin 10 of CD4021
int latchPin = 4; // Pin 2 of DigiSpark connected to Pin 9 of CD4021
//Define variable
byte RegisterValue = 0; // Used to hold data from DC4021
boolean lastPushButton1 = LOW;
boolean currentPushButton1 = LOW;
boolean led1On = false;
int button1 = 0;
boolean lastPushButton2 = LOW;
boolean currentPushButton2 = LOW;
boolean led2On = false;
int button2 = 0;
boolean lastPushButton3 = LOW;
boolean currentPushButton3 = LOW;
boolean led3On = false;
int button3 = 0;
boolean lastPushButton4 = LOW;
boolean currentPushButton4 = LOW;
boolean led4On = false;
int button4 = 0;
boolean lastPushButton5 = LOW;
boolean currentPushButton5 = LOW;
boolean led5On = false;
int button5 = 0;
boolean lastPushButton6 = LOW;
boolean currentPushButton6 = LOW;
boolean led6On = false;
int button6 = 0;
int led1 = 3;
int led2 = 5;
int led3 = 6;
int led4 = 8;
void setup() {
Serial.begin(9600);
//define pins used to connect to the CD4021 Shift Register
pinMode(dataPin, INPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
//the arduino Pins that the LEDs are connected to
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
}
void loop() {
//Set latch pin to 1 to get recent data into the CD4021
digitalWrite(latchPin,1);
delayMicroseconds(20);
//Set latch pin to 0 to get data from the CD4021
digitalWrite(latchPin,0);
//Get CD4021 register data in byte variable
RegisterValue = shiftIn(dataPin, clockPin, MSBFIRST);
if (RegisterValue == B10000000){
//Serial.print("Button 1 pressed -->");
//Serial.println(RegisterValue, BIN);
currentPushButton1 = !currentPushButton1;
//if(lastPushButton1 != currentPushButton1){
if(lastPushButton1 == LOW && currentPushButton1 == HIGH){
delay(5);
led1On = !led1On;
}
Serial.print("Led 1 is: ");
Serial.println(led1On);
digitalWrite(led1, led1On);
lastPushButton1 = currentPushButton1;
}
if (RegisterValue == B01000000) {
//Serial.print("Button 2 pressed -->");
//Serial.println(RegisterValue, BIN);
currentPushButton2 = !currentPushButton2;
if(lastPushButton2 != currentPushButton2){
digitalWrite(led2, currentPushButton2);
}
lastPushButton2 = currentPushButton2;
}
if (RegisterValue == B00100000) {
Serial.print("Button 3 pressed -->");
Serial.println(RegisterValue, BIN);
}
if (RegisterValue == B00010000) {
Serial.print("Button 4 pressed -->");
Serial.println(RegisterValue, BIN);
}
delay(100);
}
It won't show for me even right clicking on it.
You can upload your images here. Just edit your post above and scroll down below the message form. You'll see the area to attach files there.
It would also benefit you to read "How to use this forum"
DangerToMyself:
It won't show for me even right clicking on it.
You can upload your images here. Just edit your post above and scroll down below the message form. You'll see the area to attach files there.
It would also benefit you to read "How to use this forum"
thanks for your reply, ok done
OPs image
@firashelou
all I did there was copy the URL from the link of your image and put this in my post
[img]https://forum.arduino.cc/index.php?action=dlattach;topic=645593.0;attach=332285[/img]
DangerToMyself:
@firashelou
all I did there was copy the URL from the link of your image and put this in my post
[img]https://forum.arduino.cc/index.php?action=dlattach;topic=645593.0;attach=332285[/img]
DangerToMyself thank you for the help with that 
delayMicroseconds(0.2)
delayMicroseconds(0.5)
? ? ?
Est-ce possible?
runaway_pancake:
delayMicroseconds(0.2)
delayMicroseconds(0.5)
? ? ?
Est-ce possible?
c'était de quelque ancient sketch. On peut la changer pas de problème 
Runaway Pancake, the code has changed by the way, I was trying different stuff and changing code to more simple approach until this version which we will go from to try to debounce the pins
So is the problem then that holding the pushbutton down (active) results in another event, as opposed to initiating an event and then when waiting for an actual pushbutton release (one event per press)?
runaway_pancake:
So is the problem then that holding the pushbutton down (active) results in another event, as opposed to initiating an event and then when waiting for an actual pushbutton release (one event per press)?
yes right, i am trying to emulate the way a debounced button would work, you press led on you press again led off, and if you keep pressing an event would not be triggered again and again
and the buttons are bouncing sometimes i have to press more than once to make the LED go off or on
how can i change my status to online on this forum ?
SOLUTION
I finally manage to find a solution for this problem !! And this must be shared on the internet because there is no solution for 4021 for this !
What i did is took an old debouncing sketch i have and i studied it, again ! and here it is ! I had to think the same way as if i had a lonely button that i am trying to debounce !
The final code for 4021 using 6 push buttons to control 6 LEDs:
//Define pins
int dataPin = 7; // Pin 0 of DigiSpark connected to Pin 3 of CD4021
int clockPin = 2; // Pin 1 of DigiSpark connected to Pin 10 of CD4021
int latchPin = 4; // Pin 2 of DigiSpark connected to Pin 9 of CD4021
//Define variable
byte RegisterValue = 0; // Used to hold data from DC4021
boolean lastPushButton1 = LOW;
boolean currentPushButton1 = LOW;
boolean led1On = false;
boolean lastPushButton2 = LOW;
boolean currentPushButton2 = LOW;
boolean led2On = false;
boolean lastPushButton3 = LOW;
boolean currentPushButton3 = LOW;
boolean led3On = false;
boolean lastPushButton4 = LOW;
boolean currentPushButton4 = LOW;
boolean led4On = false;
boolean lastPushButton5 = LOW;
boolean currentPushButton5 = LOW;
boolean led5On = false;
boolean lastPushButton6 = LOW;
boolean currentPushButton6 = LOW;
boolean led6On = false;
int led1 = 3;
int led2 = 5;
int led3 = 6;
int led4 = 8;
int led5 = 9;
int led6 = 10;
void setup() {
Serial.begin(9600);
//define pins used to connect to the CD4021 Shift Register
pinMode(dataPin, INPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
//the arduino Pins that the LEDs are connected to
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
}
void loop() {
//Set latch pin to 1 to get recent data into the CD4021
digitalWrite(latchPin,1);
delayMicroseconds(20);
//Set latch pin to 0 to get data from the CD4021
digitalWrite(latchPin,0);
//Get CD4021 register data in byte variable
RegisterValue = shiftIn(dataPin, clockPin, MSBFIRST);
// This is for first push button -----------------------
currentPushButton1 = debounce(lastPushButton1, 0);
if(lastPushButton1 == LOW && currentPushButton1 == HIGH)
{
led1On = !led1On;
}
lastPushButton1 = currentPushButton1;
digitalWrite(led1, led1On);
// -------------------------------------------------
// This is for second push button
currentPushButton2 = debounce(lastPushButton2, 1);
if(lastPushButton2 == LOW && currentPushButton2 == HIGH)
{
led2On = !led2On;
}
lastPushButton2 = currentPushButton2;
digitalWrite(led2, led2On);
// --------------------------------------------------
// This is for third push button
currentPushButton3 = debounce(lastPushButton3, 2);
if(lastPushButton3 == LOW && currentPushButton3 == HIGH)
{
led3On = !led3On;
}
lastPushButton3 = currentPushButton3;
digitalWrite(led3, led3On);
// --------------------------------------------------
// This is for fourth push button
currentPushButton4 = debounce(lastPushButton4, 3);
if(lastPushButton4 == LOW && currentPushButton4 == HIGH)
{
led4On = !led4On;
}
lastPushButton4 = currentPushButton4;
digitalWrite(led4, led4On);
// --------------------------------------------------
// This is for fifth push button
currentPushButton5 = debounce(lastPushButton5, 4);
if(lastPushButton5 == LOW && currentPushButton5 == HIGH)
{
led5On = !led5On;
}
lastPushButton5 = currentPushButton5;
digitalWrite(led5, led5On);
// --------------------------------------------------
// This is for sixth push button
currentPushButton6 = debounce(lastPushButton6, 5);
if(lastPushButton6 == LOW && currentPushButton6 == HIGH)
{
led6On = !led6On;
}
lastPushButton6 = currentPushButton6;
digitalWrite(led6, led6On);
// --------------------------------------------------
delay(100);
}
boolean debounce(boolean last, int bitToRead)
{
boolean current = bitRead(RegisterValue, bitToRead);
if(last != current)
{
delay(5);
current = bitRead(RegisterValue, bitToRead);
}
return current;
}