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;
}