hello.
i have a school project and i need to make a counter using 6 buttons and a 2 digit segmented display to make a counter that counts up to 99 and i couldnt find any examples to make a counter with 6 push buttons, button one counts with +1 button two +2 button three +3 and so on to +6 can someone help me with the code and the arduino build
thx
Welcome to the forum
Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE
Can you find an example that add 1 to the display when a button is pressed ?
yes i can i have an example
In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the </> icon from the ‘reply menu’ to attach the copied sketch.
Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.
const int trig = 13;
const int reset = 12;
int lastswpos = 0;
int segA = 10; //1's
int segB = 11;
int segC = A0;
int segD = A1;
int segE = A2;
int segF = 9;
int segG = 8;
int segA1 = 6; //10's
int segB1 = 7;
int segC1 = A3;
int segD1 = 3;
int segE1 = 2;
int segF1 = 5;
int segG1 = 4;
int onesDigit = 0; //onesDigit integer for 0-9 increment
int tensDigit = 0; //tensDigit integer for 0-6 increment
void setup()//Setup code that will only run once, when the program is started
//Setting pins to output mode (pin numbers found using variables intialized previously)
{ pinMode(segA, OUTPUT);
pinMode(segB, OUTPUT);
pinMode(segC, OUTPUT);
pinMode(segD, OUTPUT);
pinMode(segE, OUTPUT);
pinMode(segF, OUTPUT);
pinMode(segG, OUTPUT);
pinMode(segA1, OUTPUT);
pinMode(segB1, OUTPUT);
pinMode(segC1, OUTPUT);
pinMode(segD1, OUTPUT);
pinMode(segE1, OUTPUT);
pinMode(segF1, OUTPUT);
pinMode(segG1, OUTPUT);
pinMode(trig, INPUT);
pinMode(reset, INPUT);
/*
digitalWrite(segA, HIGH);//Sets pin 0 (pin segA) to have a low output, which makes the A segment on the ones digit display to light up
digitalWrite(segB, HIGH);//Sets pin 1 (pin segB) to have a low output, which makes the B segment on the ones digit display to light up
digitalWrite(segC, HIGH);//Sets pin 2 (pin segC) to have a low output, which makes the C segment on the ones digit display to light up
digitalWrite(segD, HIGH);//Sets pin 3 (pin segD) to have a low output, which makes the D segment on the ones digit display to light up
digitalWrite(segE, HIGH);//Sets pin 4 (pin segE) to have a low output, which makes the E segment on the ones digit display to light up
digitalWrite(segF, HIGH);//Sets pin 5 (pin segF) to have a low output, which makes the F segment on the ones digit display to light up
digitalWrite(segG, LOW);
digitalWrite(segA1, HIGH);
digitalWrite(segB1, HIGH);
digitalWrite(segC1, HIGH);
digitalWrite(segD1, HIGH);
digitalWrite(segE1, HIGH);
digitalWrite(segF1, HIGH);
digitalWrite(segG1, LOW);
delay(3000);
*/
countones(0);
counttens(tensDigit);
}
void loop()//Main code that will run repeatedly until stopped
{
int shotmade = digitalRead(trig);
int toreset = digitalRead(reset);
if (toreset == HIGH) {
onesDigit = 0;
tensDigit = 0;
}
if (shotmade != lastswpos) {
if (shotmade == HIGH) {
onesDigit = onesDigit + 1;
}
if (onesDigit > 9) {
tensDigit = tensDigit + 1;
onesDigit = 0;
}
}
countones(onesDigit);
counttens(tensDigit);
lastswpos = shotmade;
}
void countones(int onesDigit) {
switch (onesDigit)//Switch statement using the onesDigit variable as an index
{
//When the pin is set to low output, the corresponding LED segment lights up, and when the pin is set to high, the corresponding LED segment does not light up
case 0://When the onesDigit variable value is equal to 0 display the number ”0” on the ones digit display
digitalWrite(segA, HIGH);//Sets pin 0 (pin segA) to have a low output, which makes the A segment on the ones digit display to light up
digitalWrite(segB, HIGH);//Sets pin 1 (pin segB) to have a low output, which makes the B segment on the ones digit display to light up
digitalWrite(segC, HIGH);//Sets pin 2 (pin segC) to have a low output, which makes the C segment on the ones digit display to light up
digitalWrite(segD, HIGH);//Sets pin 3 (pin segD) to have a low output, which makes the D segment on the ones digit display to light up
digitalWrite(segE, HIGH);//Sets pin 4 (pin segE) to have a low output, which makes the E segment on the ones digit display to light up
digitalWrite(segF, HIGH);//Sets pin 5 (pin segF) to have a low output, which makes the F segment on the ones digit display to light up
digitalWrite(segG, LOW);
delay(25);//Creates a delay of 0.75 seconds (750 miliseconds), later in the code there is a 250 milisecond delay to create an overall 1 second delay
break;//Breaks the current case (case 0) so that the rest of the code is not included in it
case 1://When the onesDigit variable value is equal to 1 show ”1” on the ones digit display
digitalWrite(segA, LOW);//Same as the case 0 comments, however with different segments lit up to display 1
digitalWrite(segB, HIGH);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
delay(25);
break;
case 2://When the onesDigit variable value is equal to 2 show ”2” on the ones digit display
digitalWrite(segA, !LOW);//Same as the case 0 comments, however with different segments lit up to display 2
digitalWrite(segB, !LOW);
digitalWrite(segC, !HIGH);
digitalWrite(segD, !LOW);
digitalWrite(segE, !LOW);
digitalWrite(segF, !HIGH);
digitalWrite(segG, !LOW);
delay(25);
break;
case 3://When the onesDigit variable value is equal to 3 show ”3” on the ones digit display
digitalWrite(segA, !LOW);//Same as the case 0 comments, however with different segments lit up to display 3
digitalWrite(segB, !LOW);
digitalWrite(segC, !LOW);
digitalWrite(segD, !LOW);
digitalWrite(segE, !HIGH);
digitalWrite(segF, !HIGH);
digitalWrite(segG, !LOW);
delay(25);
break;
case 4://When the onesDigit variable value is equal to 4 show ”4” on the ones digit display
digitalWrite(segA, !HIGH);//Same as the case 0 comments, however with different segments lit up to display 4
digitalWrite(segB, !LOW);
digitalWrite(segC, !LOW);
digitalWrite(segD, !HIGH);
digitalWrite(segE, !HIGH);
digitalWrite(segF, !LOW);
digitalWrite(segG, !LOW);
delay(25);
break;
case 5://When the onesDigit variable value is equal to 5 show ”5” on the ones digit display
digitalWrite(segA, !LOW);//Same as the case 0 comments, however with different segments lit up to display 5
digitalWrite(segB, !HIGH);
digitalWrite(segC, !LOW);
digitalWrite(segD, !LOW);
digitalWrite(segE, !HIGH);
digitalWrite(segF, !LOW);
digitalWrite(segG, !LOW);
delay(25);
break;
case 6://When the onesDigit variable value is equal to 6 show ”6” on the ones digit display
digitalWrite(segA, !LOW);//Same as the case 0 comments, however with different segments lit up to display 6
digitalWrite(segB, !HIGH);
digitalWrite(segC, !LOW);
digitalWrite(segD, !LOW);
digitalWrite(segE, !LOW);
digitalWrite(segF, !LOW);
digitalWrite(segG, !LOW);
delay(25);
break;
case 7://When the onesDigit variable value is equal to 7 show ”7” on the ones digit display
digitalWrite(segA, !LOW);//Same as the case 0 comments, however with different segments lit up to display 7
digitalWrite(segB, !LOW);
digitalWrite(segC, !LOW);
digitalWrite(segD, !HIGH);
digitalWrite(segE, !HIGH);
digitalWrite(segF, !HIGH);
digitalWrite(segG, !HIGH);
delay(25);
break;
case 8://When the onesDigit variable value is equal to 8 show ”8” on the ones digit display
digitalWrite(segA, !LOW);//Same as the case 0 comments, however with different segments lit up to display 8
digitalWrite(segB, !LOW);
digitalWrite(segC, !LOW);
digitalWrite(segD, !LOW);
digitalWrite(segE, !LOW);
digitalWrite(segF, !LOW);
digitalWrite(segG, !LOW);
delay(25);
break;
case 9://When the onesDigit variable value is equal to 9 show ”9” on the ones digit display
digitalWrite(segA, !LOW);//Same as the case 0 comments, however with different segments lit up to display 9
digitalWrite(segB, !LOW);
digitalWrite(segC, !LOW);
digitalWrite(segD, !LOW);
digitalWrite(segE, !HIGH);
digitalWrite(segF, !LOW);
digitalWrite(segG, !LOW);
delay(25);
//break;
break;//Breaks the switch statement, so the next code is not included
}
return;
}
void counttens(int tensDigit) {
switch (tensDigit)//Switch statement using the tensDigit variable as an index//Nested switch statement using the tensDigit variable as an index
{
case 0://when tensDigit value is zero show ”0” on display
digitalWrite(segA1, HIGH);
digitalWrite(segB1, HIGH);
digitalWrite(segC1, HIGH);
digitalWrite(segD1, HIGH);
digitalWrite(segE1, HIGH);
digitalWrite(segF1, HIGH);
digitalWrite(segG1, LOW);
delay(25);
break;
case 1:// when tensDigit value is 1 show ”1” on display
digitalWrite(segA1, !HIGH);
digitalWrite(segB1, !LOW);
digitalWrite(segC1, !LOW);
digitalWrite(segD1, !HIGH);
digitalWrite(segE1, !HIGH);
digitalWrite(segF1, !HIGH);
digitalWrite(segG1, !HIGH);
delay(25);
break;
case 2:// when tensDigit value is 2 show ”2” on display
digitalWrite(segA1, !LOW);
digitalWrite(segB1, !LOW);
digitalWrite(segC1, !HIGH);
digitalWrite(segD1, !LOW);
digitalWrite(segE1, !LOW);
digitalWrite(segF1, !HIGH);
digitalWrite(segG1, !LOW);
delay(25);
break;
case 3:// when tensDigit value is 3 show ”3” on display
digitalWrite(segA1, !LOW);
digitalWrite(segB1, !LOW);
digitalWrite(segC1, !LOW);
digitalWrite(segD1, !LOW);
digitalWrite(segE1, !HIGH);
digitalWrite(segF1, !HIGH);
digitalWrite(segG1, !LOW);
delay(25);
break;
case 4:// when tensDigit value is 4 show ”4” on display
digitalWrite(segA1, !HIGH);
digitalWrite(segB1, !LOW);
digitalWrite(segC1, !LOW);
digitalWrite(segD1, !HIGH);
digitalWrite(segE1, !HIGH);
digitalWrite(segF1, !LOW);
digitalWrite(segG1, !LOW);
delay(25);
break;
case 5:// when tensDigit value is 5 show ”5” on display
digitalWrite(segA1, !LOW);
digitalWrite(segB1, !HIGH);
digitalWrite(segC1, !LOW);
digitalWrite(segD1, !LOW);
digitalWrite(segE1, !HIGH);
digitalWrite(segF1, !LOW);
digitalWrite(segG1, !LOW);
delay(25);
break;
case 6:// when tensDigit value is 6 show ”6” on display
digitalWrite(segA1, !LOW);
digitalWrite(segB1, !HIGH);
digitalWrite(segC1, !LOW);
digitalWrite(segD1, !LOW);
digitalWrite(segE1, !LOW);
digitalWrite(segF1, !LOW);
digitalWrite(segG1, !LOW);
delay(25);
break;
case 7:// when tensDigit value is 7 show ”7” on display
digitalWrite(segA1, HIGH);
digitalWrite(segB1, HIGH);
digitalWrite(segC1, HIGH);
digitalWrite(segD1, !HIGH);
digitalWrite(segE1, !HIGH);
digitalWrite(segF1, !HIGH);
digitalWrite(segG1, LOW);
delay(25);
break;
case 8:// when tensDigit value is 8 show ”8” on display
digitalWrite(segA1, HIGH);
digitalWrite(segB1, HIGH);
digitalWrite(segC1, HIGH);
digitalWrite(segD1, HIGH);
digitalWrite(segE1, HIGH);
digitalWrite(segF1, HIGH);
digitalWrite(segG1, !LOW);
delay(25);
break;
case 9:// when tensDigit value is 9 show ”9” on display
digitalWrite(segA1, HIGH);
digitalWrite(segB1, HIGH);
digitalWrite(segC1, HIGH);
digitalWrite(segD1, HIGH);
digitalWrite(segE1, !HIGH);
digitalWrite(segF1, HIGH);
digitalWrite(segG1, !LOW);
delay(25);
break;
break;//Breaks the switch statement, so the next code is not included
}
}
The clumsy way to do what you want with 6 buttons would be to repeat the button reading and number setting code 6 times but have it add a different number depending on which button is pressed
Try it starting with 2 buttons
NOTE : there is a much better way to do this
can you explain the better way please?
Use an array to hold the button pin numbers. Iterate through the array and read the pins. If you find a button pressed then use the current array index to determine what to add to the count
but the problem is that i dont have pins left so only 1 and 2 and a couple analog pins
Were you keeping that a secret ?
You can read and differentiate between multiple different button presses using a single analogue pin
Multiple button inputs using Arduino analog pin « RAYSHOBBY.NET.
as an example
i tried and it didnt work isnt there a example availibe whereby i can add buttons ?
Post the sketch that you tried and a schematic of your circuit. A picture of a hand drawn circuit is good enough
What exactly doe "didn't work" mean ?
You can also use a Arduino Mega or a shift register to increase the number of inputs as well.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
