binary counter using pulses

hello; this is my vacation project which has 2 parts. It works the following way.

when I push a button1 or give it a negative pulse it should go to the “automatic counter”. It should count untill I press “button2”. When I press “button2” it should intermediately go to the manual counter. In the manualcounter the program will need a pulse to count,so, when I press button2 it should count +1 untill I press "button1"where it should go back to the automatic counter.

My problem is working with pulses, I’m trying to “catch” every pulse anytime by using many "if"conditions. Is there another way to work with pulses as the way I did does not work very well.
Also I have some problems with contact bounce with the pulses in the manual counter, how do I solve this?
The counter is binary(4bits).

Thanks you

``````const int button1 = 2;
const int button2 = 3; // the number of the pushbutton pin
int outPin[] = {12, 5, 6, 9}; //binary counter pins
int i;
int button1condition = 1;
int button2condition = 1;
long time;

void setup() {
for ( i = 0; i < 4; i++) {
pinMode(outPin[i], OUTPUT);
}
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);

}

void loop() {
button2condition = digitalRead(button2);
button1condition = digitalRead(button1);

if (button1condition == 0  && button2condition == 1 ) {
while (button2condition == 1) {
autocounter();
}
}

if (button2condition == 0 && button1condition == 1) {
while (button1condition == 1) {
manualcounter();

}
}

}

void  autocounter() {
int i , j;
for ( i = 0; i < 16; i++) {
button2condition = digitalRead(button2);
if (button2condition == 1) {
for ( j = 0; j < 4; j++) {

if ( ( (i >> j) & 1 )  == 1 )
digitalWrite(outPin[j], HIGH);
else digitalWrite(outPin[j], LOW);
}
button2condition = digitalRead(button2);
if (button2condition == 0) {
i = 16;
digitalWrite(outPin[0], LOW); digitalWrite(outPin[1], LOW); digitalWrite(outPin[2], LOW); digitalWrite(outPin[3], LOW);
}
time = millis();
while (time + 500 > millis()) {

button2condition = digitalRead(button2);
if (button2condition == 0) {
i = 16;

digitalWrite(outPin[0], LOW); digitalWrite(outPin[1], LOW); digitalWrite(outPin[2], LOW); digitalWrite(outPin[3], LOW);
}

}
}
else {
digitalWrite(outPin[0], LOW); digitalWrite(outPin[1], LOW); digitalWrite(outPin[2], LOW); digitalWrite(outPin[3], LOW);
i = 16;

}
}
}

void  manualcounter () {
int i = 0, j = 0;

for ( i = 0; i < 16; i++) {

button1condition = digitalRead(button1);
if (button1condition == 0) {
i = 16;

digitalWrite(outPin[0], LOW); digitalWrite(outPin[1], LOW); digitalWrite(outPin[2], LOW); digitalWrite(outPin[3], LOW);
}
for ( j = 0; j < 4; j++) {

if ( ( (i >> j) & 1 )  == 1  )
digitalWrite(outPin[j], HIGH);
else digitalWrite(outPin[j], LOW);
button1condition = digitalRead(button1);
if (button1condition == 0) {
i = 16;
digitalWrite(outPin[0], LOW); digitalWrite(outPin[1], LOW); digitalWrite(outPin[2], LOW); digitalWrite(outPin[3], LOW);
}

}

button2condition = 1;
while (button2condition == 1) {
delay(100);
button2condition = digitalRead(button2);
button1condition = digitalRead(button1);

if (button1condition == 0) {
i = 16;

digitalWrite(outPin[0], LOW); digitalWrite(outPin[1], LOW); digitalWrite(outPin[2], LOW); digitalWrite(outPin[3], LOW);
goto etiqueta;

}
}
button1condition = digitalRead(button1);

if (button1condition == 0) {
i = 16;
digitalWrite(outPin[0], LOW); digitalWrite(outPin[1], LOW); digitalWrite(outPin[2], LOW); digitalWrite(outPin[3], LOW);
}
}
etiqueta:
i = 16;
}
``````

Is this an assignment?

Please read this:- How to use this forum Because your post is breaking the rules about posting code.

Don't use delay if you don't want to miss pulses. When you use delay the processor is not doing anything for you and things will happen that it will miss.

hello; this is my vacation project which has 2 parts. It works the following way.

when I push a button1 or give it a negative pulse it should go to the “automatic counter”. It should count untill I press “button2”. When I press “button2” it should intermediately go to the manual counter. In the manualcounter the program will need a pulse to count,so, when I press button2 it should count +1 untill I press "button1"where it should go back to the automatic counter.

My problem is working with pulses, I’m trying to “catch” every pulse anytime by using many "if"conditions. Is there another way to work with pulses as the way I did does not work very well.
Also I have some problems with contact bounce with the pulses in the manual counter, how do I solve this?
The counter is binary(4bits).

Thanks you

``````const int button1 = 2;
const int button2 = 3; // the number of the pushbutton pin
int outPin[] = {12, 5, 6, 9}; //binary counter pins
int i;
int button1condition = 1;
int button2condition=1;
long time;

void setup() {
for ( i = 0; i < 4; i++){
pinMode(outPin[i], OUTPUT);
}
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);

}

void loop(){
button2condition=digitalRead(button2);
button1condition=digitalRead(button1);

if (button1condition == 0  && button2condition==1 ){
while(button2condition==1){
autocounter();
}
}

if(button2condition==0 && button1condition==1){
while(button1condition==1){
manualcounter();

}
}

}

void  autocounter(){
int i , j;
for ( i = 0; i < 16; i++){
button2condition=digitalRead(button2);
if(button2condition==1){
for ( j = 0; j < 4; j++){

if ( ( (i >> j) & 1 )  == 1 )
digitalWrite(outPin[j], HIGH);
else digitalWrite(outPin[j], LOW);
}
button2condition=digitalRead(button2);
if(button2condition==0){
i=16;
digitalWrite(outPin[0], LOW);digitalWrite(outPin[1], LOW);digitalWrite(outPin[2], LOW);digitalWrite(outPin[3], LOW);
}
time=millis();
while(time+500>millis()){
delayMicroseconds(900);
button2condition=digitalRead(button2);
if(button2condition==0){
i=16;

digitalWrite(outPin[0], LOW);digitalWrite(outPin[1], LOW);digitalWrite(outPin[2], LOW);digitalWrite(outPin[3], LOW);
}

}
}
else{
digitalWrite(outPin[0], LOW);digitalWrite(outPin[1], LOW);digitalWrite(outPin[2], LOW);digitalWrite(outPin[3], LOW);
i=16;

}
}
}

void  manualcounter (){
int i = 0, j=0;

for ( i = 0; i < 16; i++){

button1condition=digitalRead(button1);
if(button1condition==0){
i=16;

digitalWrite(outPin[0], LOW);digitalWrite(outPin[1], LOW);digitalWrite(outPin[2], LOW);digitalWrite(outPin[3], LOW);
}
for ( j = 0; j < 4; j++){

if ( ( (i >> j) & 1 )  == 1  )
digitalWrite(outPin[j], HIGH);
else digitalWrite(outPin[j], LOW);
button1condition=digitalRead(button1);
if(button1condition==0){
i=16;
digitalWrite(outPin[0], LOW);digitalWrite(outPin[1], LOW);digitalWrite(outPin[2], LOW);digitalWrite(outPin[3], LOW);
}

}

button2condition=1;
while(button2condition==1){

button2condition=digitalRead(button2);
button1condition=digitalRead(button1);

if(button1condition==0){
i=16;

digitalWrite(outPin[0], LOW);digitalWrite(outPin[1], LOW);digitalWrite(outPin[2], LOW);digitalWrite(outPin[3], LOW);
goto etiqueta;

}
}
button1condition=digitalRead(button1);

if(button1condition==0){
i=16;
digitalWrite(outPin[0], LOW);digitalWrite(outPin[1], LOW);digitalWrite(outPin[2], LOW);digitalWrite(outPin[3], LOW);
}
}
etiqueta:
i=16;
}
``````

Get some 555 timers, some flip-flops, maybe some AND or OR gates as well, and build it out of discrete logic, that could be quite interesting.

If you want to use an Arduino, read up on the usual stuff like Blink without delay, interrupts, and maybe state machines. You'll find a lot of information on Nick Gammon's site. That should get you started.

Pieter

P.s: please edit your initial post, use code tags, and read the forum rules, like Grumpy_Mike suggested.

Duplicate of http://forum.arduino.cc/index.php?topic=451217.

As you've discovered, you can edit posts. Please don't create multiple duplicate posts.

Thanks for the code tags. This bit

``````while(time+500>millis()){
delayMicroseconds(900);
``````

Can only ever do that loop once, because after a 0.9 second delay the millis timer would have advanced by 900 and so stop the second time round the loop. It sort of implies you don't know what is going on.

You are trying to write too much at once without testing it and you are getting muddled up with your logic. Start simple and build up. Break the problem down and get small pieces of it going, then add to it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

pert: Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read.

I did it now. sorry

Do not cross-post. Duplicate removed.

Do not cross-post. Threads merged.