CD4026 Stability

Dear all:

Im working on a project that requieres the use of the DECADE counters CD4026.
It is a project in which a button will be activated when pepople steps on it. Depending on the area it will activate differnt buttons. Those are read by Arduino, and it send a clean pulse t the CD4026 counter. It has also a TOTAL count in which im having problems:

I dont know where is the piece of programming that sends a lot of pulses to the total with every push of the button.
Ill attatch a couple of videos.

Attatched is the basic Schamatic

Here it is how it DOESNT work (top right corner is the total count)

and the coode i have is:

const int bot0 = A0;
const int bot1 = A1;
const int bot2 = A2;


const int con0 = 6;
const int con1 = 3;
const int con2 = 1;
const int total = 0;
const int buzz = 8;

void setup()
{
 pinMode(bot0, OUTPUT);
 pinMode(bot1, OUTPUT);
 pinMode(bot2, OUTPUT);
 
 digitalWrite(bot0, HIGH);
 digitalWrite(bot1, HIGH);
 digitalWrite(bot2, HIGH);
 digitalWrite(total, LOW);   //i have changed  tried delating this but still not working
 
 pinMode(con0,OUTPUT);
 pinMode(con1,OUTPUT);
 pinMode(con2,OUTPUT);
 pinMode(total,OUTPUT);
}

void loop ()
{
  if(digitalRead(bot0) == LOW)   //read button 0. Increase counter0 one and increase total
  {
    digitalWrite(con0, HIGH);
    digitalWrite(total, HIGH);

    }
    else
   {
    digitalWrite(con0, LOW);
    digitalWrite(total, LOW);
    }
    
    
    
   if(digitalRead(bot1) == LOW)   //read button 1. Increase counter 1 and total
  {
    digitalWrite(con1, HIGH);
    digitalWrite(total, HIGH);
    }
    else
   {
    digitalWrite(con1, LOW);
    digitalWrite(total, LOW);
    }
    
      if(digitalRead(bot2) == LOW)   ///read button 2. Increase counter 2 and total
  {
    digitalWrite(con2, HIGH);
    digitalWrite(total, HIGH);
    }
    else
   {
    digitalWrite(con2, LOW);
    digitalWrite(total, LOW);
    }
    
 
 
        }

When I remove the other buttons in the program, it works perfectly:

const int bot0 = A0;
const int bot1 = A1;
const int bot2 = A2;


const int con0 = 6;
const int con1 = 3;
const int con2 = 1;
const int total = 0;


void setup()
{
 pinMode(bot0, OUTPUT);
 pinMode(bot1, OUTPUT);
 pinMode(bot2, OUTPUT);

 
 digitalWrite(bot0, HIGH);
 digitalWrite(bot1, HIGH);
 digitalWrite(bot2, HIGH);
 digitalWrite(total, LOW);
 
 pinMode(con0,OUTPUT);
 pinMode(con1,OUTPUT);
 pinMode(con2,OUTPUT);
 pinMode(total,OUTPUT);
 
}

void loop ()
{
  if(digitalRead(bot0) == LOW)   //
  {
    digitalWrite(con0, HIGH);
    digitalWrite(total, HIGH);

    }
    else
   {
    digitalWrite(con0, LOW);
    digitalWrite(total, LOW);
    }

Im sure it could be something easy but I still cant figure it out. If someone has any recomendation, let me know.

Thanks

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

TomGeorge:
Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

Hi Tom. Thanks for the warm welcome. I have edited the message in order to folow the rules and I attatched a basic Schematic of the circuit.
Extra information:
The CD4026 is a decade counter that directly can be connected to 7 segment. It works with pulses and with every pulse it will increase the count. they can be conected in cascade mode ( as shown in the videos). Im pretty sure that the behavior of the total count is not noise rather than a mistake in the code or loop that i cant figure it out.

Today Ill do extra tests and keep you informed of any news.

thanks for your suppor

Hi,
OPs diagram.

Thanks.. Tom.. :slight_smile:

Hi,
Shouldn't these be inputs?

pinMode(bot0, OUTPUT);
 pinMode(bot1, OUTPUT);
 pinMode(bot2, OUTPUT);

You have declared a lot of OUTPUTs but NO INPUTs.

Tom.... :slight_smile:

TomGeorge:
Hi,
Shouldn't these be inputs?

pinMode(bot0, OUTPUT);

pinMode(bot1, OUTPUT);
pinMode(bot2, OUTPUT);



You have declared a lot of OUTPUTs but NO INPUTs.


Tom.... :)

Hey Tom, thanks for keeping track. I though the same, but as i was investigating I found the following post arduino mega - Using analog in to read a push button - Arduino Stack Exchange

where it explains it should be declared as outputs. As a 6o years old enthusiast I have to learn from the others.

However, changed it to Input and it works the same way. The total count still recieves a lot of pulses.
mmm do you wonder any other way to send jus a pulse through that pin?

Delta_G:
I see two probably issues here. You're reading buttons and saying that you're getting multiple pulses out. That sounds like the switches are bouncing. Google "Arduino Debounce" for more information.

The other issue that is evident when you say you cut it down to one button and it works. With one button you press the button and the output goes HIGH and it doesn't go LOW again until you let off of it. The loop turns over really fast, but as long as your finger stays on the button it can't get to any part that sets total pin low again. So one press and release gets one pulse, high and low.

Now walk through your multi-button code for a second. If I press the first button then it makes the total output go high. Immediately thereafter if I'm not pressing also the second button it goes into the else part for button2 and makes that pin LOW again. That's one pulse. Now the loop can run a few thousand times over in the time it takes you to press the button and remove your finger. And each time the loop repeats with your finger on the button you send another pulse as button1 turns it on and button2 turns it back off.

You need to look at edge detection and fire the complete pulse once on a good edge, that is if you see button 1 get pressed send it high and then back to low and don't fire another pulse for button 1 until it gets released and pressed again. See the "State Change" example.

Delta: Thanks for the detailed repply. As you have notice I didnt knew that analog inputs were Digital thats why I looked for extra information on how to deal with it... I think it is not a bouncng issue, as the normal counter (not total) responds very well to the imput, however its worth to investigate and add some debouncing code.
It looks that the problem is, as you said, the "else" part of the other buttons, causing the total going from high to low. THANKS! for helping me to note that. Let me think for tomorrow how I can remake that part... maybe a "while" ?

Ill keep informed

Hey. I managed to solve tha"stability". As it was noticed, the problem whas in the "else" of the other statements.
I managed to solve it with a "while" instead and adding a delay for stability.

void loop (){
   state0 = digitalRead(bot0);
   state1 = digitalRead(bot1);
   state2 = digitalRead(bot2);
   
   digitalWrite(con0, LOW);
   digitalWrite(con1, LOW);
   digitalWrite(con2, LOW);
   digitalWrite(total, LOW);
   
   while (state0 == LOW) {
     digitalWrite(con0, HIGH);
     digitalWrite(total, HIGH);
     }
     
   while (state1 == LOW) {
     digitalWrite(con1, HIGH);
     digitalWrite(total, HIGH);
     }

With this, I was able to control the pulses of the CD4026.
Thanksfor those who dedicated some time to help me out. Ill make a video as son as I can.

   while (state0 == LOW) {
     digitalWrite(con0, HIGH);
     digitalWrite(total, HIGH);
     }

If state0 does happen to be LOW, you have an infinite loop here.

   while (state1 == LOW) {
     digitalWrite(con1, HIGH);
     digitalWrite(total, HIGH);
     }

If state1 does happen to be LOW, you have an infinite loop here.

Are you CERTAIN that while is the correct statement? Seems to me an if statement is far more appropriate.

Why do you need to keep setting the state of the pin? An output pin will not randomly change state. It will stay in the state you tell it to be in until you tell it differently.

Hi,
What happens if you hold your button down?

Tom.. :slight_smile:

Sorry, I posted the wrong code.

This how it is working:

 void loop (){
   state0 = digitalRead(bot0);
   state1 = digitalRead(bot1);
   state2 = digitalRead(bot2);
   state3 = digitalRead(bot3);
   state4 = digitalRead(bot4);
   state5 = digitalRead(bot5);
   
   digitalWrite(con0, LOW);
   digitalWrite(con1, LOW);
   digitalWrite(con2, LOW);
   digitalWrite(con3, LOW);
   digitalWrite(con4, LOW);
   digitalWrite(con5, LOW);
   digitalWrite(total, LOW);
   digitalWrite(buzz, LOW);
   
   while (state0 == LOW) {
     delay(50);
     digitalWrite(con0, HIGH);
     digitalWrite(total, HIGH);
   state0 = digitalRead(bot0);
   state1 = digitalRead(bot1);
   state2 = digitalRead(bot2);
   state3 = digitalRead(bot3);
   state4 = digitalRead(bot4);
   state5 = digitalRead(bot5); 
     }

Here is the video:
https://www.youtube.com/watch?v=Egv9OJYIaQk

No problems in Holding the buttons. If I hold it It triggers just one pulse (as i wanted).
Pressing both at the same time do not affect and just triggers the one that catches the input first. The delay 50. gives a pretty good debounce. I can press them as fast as I can and no bouncing issues. Im using OMRON 3f.

@Paul. I used earlier the if statement but I had problems with it.. couldnt solve but this works

The next step is to trigger an alarm for 3 secs when reaching a total of 200. Ill use example "StateChange Detection", using the module.

Thank

Hi,
You have a PCB made.
Where is your schematic?
You must have used a CAD to do it, can you post an exported jpg of the circuit diagram?

Please post a schematic as this will make this thread complete for anyone wanting to do a similar thing.

Tom... :slight_smile: