O to 99 counter UP/DOWN on 7 segmant _ SCOREBOARD

hello , i want to make couunter from 0 to 99 with arduino and 2 seven segment controlled by transistors . Also i have 2 push buttons one for counting up and one for counting down .I'm stuck with the code .
i can write simple code for automatic counting on one 7 segmant i'm still trying but i didnt reach the write soltion yet
and thx.

You are going to have to post your code.

here is the code for counting from 0 to 9

int Button = 12;
int count = 0; // current display count
int val = 0;   // digital input from button


void setup() {               
  pinMode(11, OUTPUT);  
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(Button, INPUT);
  Serial.begin(9600);
}
   
    void loop() {
  val = digitalRead(Button);
  delay(500);
if (val == HIGH) {
    count++;
    
// write '0'
//Serial.println(count);

if    (count == 0)
{ 
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 0);
}
else if(count == 1)
{ 
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
}
else if(count == 2)
{
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 0);
digitalWrite(8, 1);
}
else if(count == 3)
{
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 1);
}
else if(count == 4)
{
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 1);
}
else if(count == 5)
{
digitalWrite(2, 1);
digitalWrite(3, 0);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 1);
}
else if(count == 6)
{
digitalWrite(2, 1);
digitalWrite(3, 0);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
}
else if(count == 7)
{
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
}
else if(count == 8)
{
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 1);
}
else if(count == 9)
{
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 1);
}
else
count = 0;
    }
    }

Where are you "stuck" with your code?

i have not a clear idea how to change that to make an up/down counter to 99 on 2 seven segmants
can you help me plz

You first need to know how to display digits on your display.
Do you know how to write code to do that?
Once you are able to display 00 to 99, then add in the ability to respond when a button is pressed.

that is my main problem can you guide to make a counter from 0 to 99?

Lots of tutorials on the web.

i wasnt able to find could you help or send me a link ?

yahyaz:
i wasnt able to find could you help or send me a link ?

Truly?
Click Here.

I'd 'help', probably by trying to get you to discuss and understand your sketch there, but then the MAX7219 salesman would impose and that would get me honked off.

Who, me?


The problem here is that we are not sure that you - the OP - should simply be given a code to do the job. I could write it for you and am tempted to do so but - is that a good idea? If you get a piece of code, can you learn from it sufficiently to understand every step in it and be able to develop it further?

I believe we would argue that it would be best for you to learn stepwise by writing successively, more neat and generalised code. There are a number of steps that whilst not all strictly necessary, should be done with your code to most efficiently achieve the desired result. These include:

Defining your interface pins. These should not be hidden amongst the workings, but specified "up front", at the start. You have put "int Button = 12;", but not similarly defined the segment outputs. But there is a clever trick to this.

Grouping things in arrays. This is what you do when a number of things go together. So you might say

char segs_1 = [2, 3, 4, 5, 6, 7, 8;]    // right hand digit

Mind you, I note your code writes to these segments, but only sets 5 to 11 as outputs!

You can now use an indexed loop ("for") to set these as outputs in setup(). But you can write a matching array - in fact a two dimensional array for the number patterns

const char font = [1,1,1,1,1,1,0], // 0
                  [0,1,1,0,0,0,0], // 1
                  [1,1,0,1,1,0,1], // 2
etc.

and use firstly an index to select the digit, and an indexed loop to write out the seven segment values to the corresponding output pins in the array that defines the segments.

And all this merely to write a neater (working) version of your current code.

But that would be a start. We would want you to correct the wiring of your button, so that it connects from a pin to ground, uses INPUT_PULLUP rather than requiring a pull-up resistor, and is then considered to be pressed when it reads LOW.

And after all this, it would be relatively easy to arrange down counting and extension to two digits, having verified that you are using resistors in series with each LED. You can use pins 9, 10, 11, 12, 13, 14 and 15 for the second digit, move the buttons to 16 and 17. Pins 14, 15, 16 and 17 are of course, also called A0 to A3.

So there is some work for you.

thanks for the help i 'm found this solution but its a little diffrnet from what i want ican control each 7 segment seperatly and it's acceptable but can you help to modify it to make one button for incrementing and the other for decrementing

const int A = 1;
const int B = 2;
const int C = 3;
const int D = 4;
const int E = 5;
const int F_SEG = 6;
const int G = 7;
const int BTN1 = 9;
const int BTN2 = 11;
const int CA1 = 8;
const int CA2 = 10;

// Pins for A B C D E F G, in sequence
const int segs[7] = { A, B, C, D, E, F_SEG, G };
// Segments that make each number
const byte numbers[10] = { 0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010,
0b0000010, 0b1111000, 0b0000000, 0b0010000 };

int digit1 = 0;
int digit2 = 0;
void setup() {
 pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F_SEG, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(BTN1, INPUT);
  pinMode(BTN2, INPUT);
  pinMode(CA1, OUTPUT);
  pinMode(CA2, OUTPUT);
}

void loop() {
   // check button1
  int val1 = digitalRead(BTN1);
  if (val1 == HIGH) {
    digit1++;
    digit1 %= 10;
    delay(10);
  }

  // check button2
  int val2 = digitalRead(BTN2);
  if (val2 == HIGH) {
    digit2++;
    digit2 %= 10;
    delay(10);
  }
    // display number
  unsigned long startTime = millis();
  for (unsigned long elapsed=0; elapsed < 200; elapsed = millis() - startTime) {
    lightDigit1(numbers[digit1]);
    delay(5);
    lightDigit2(numbers[digit2]);
    delay(5);
  }
}

void lightDigit1(byte number) {
  digitalWrite(CA1, HIGH);
  digitalWrite(CA2, LOW);
  lightSegments(number);
}

void lightDigit2(byte number) {
  digitalWrite(CA1, LOW);
  digitalWrite(CA2, HIGH);
  lightSegments(number);
}

void lightSegments(byte number) {
  for (int i = 0; i < 7; i++) {
    int bit = bitRead(number, i);
    digitalWrite(segs[i], bit);
  }
}

OK, here is some code for you to try.

// Two digit counter.

const char A = 2;
const char B = 3;
const char C = 4;
const char D = 5;
const char E = 6;
const char F_SEG = 7;
const char G = 8;
const char BTN1 = 10;
const char BTN2 = 11;
const char CA1 = 12;
const char CA2 = 13;

// Pins for A B C D E F G, in sequence
const char segs[7] = { A, B, C, D, E, F_SEG, G };
// Segments that make each number
const byte numbers[10] = { 0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010,
                           0b0000010, 0b1111000, 0b0000000, 0b0010000
                         };

char digit1 = 0;
char digit2 = 0;
void setup() {
  pinMode(A, OUTPUT);
  pinMode(B, OUTPUT);
  pinMode(C, OUTPUT);
  pinMode(D, OUTPUT);
  pinMode(E, OUTPUT);
  pinMode(F_SEG, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(BTN1, INPUT_PULLUP);
  pinMode(BTN2, INPUT_PULLUP);
  pinMode(CA1, OUTPUT);
  pinMode(CA2, OUTPUT);
}

void loop() {
  // check button1
  char val1 = digitalRead(BTN1);
  if (val1 == LOW) {
    digit1++;
    if (digit1 >= 10) {
      digit2++;
      digit1 = 0;
    }
    digitalWrite(CA1, LOW);
    digitalWrite(CA2, LOW);
    delay(200);
  }

  // check button2
  char val2 = digitalRead(BTN2);
  if (val2 == LOW) {
    digit1--;
    if (digit1 < 0) {
      digit2--;
      digit1 = 9;
    }
    digitalWrite(CA1, LOW);
    digitalWrite(CA2, LOW);
    delay(200);
  }
  // display number
  unsigned long startTime = millis();
  for (unsigned long elapsed = 0; elapsed < 200; elapsed = millis() - startTime) {
    lightDigit1(numbers[digit1]);
    delay(10);
    lightDigit2(numbers[digit2]);
    delay(10);
  }
}

void lightDigit1(byte number) {
  digitalWrite(CA2, LOW);
  lightSegments(number);
  digitalWrite(CA1, HIGH);
}

void lightDigit2(byte number) {
  digitalWrite(CA1, LOW);
  lightSegments(number);
  digitalWrite(CA2, HIGH);
}

void lightSegments(byte number) {
  for (int i = 0; i < 7; i++) {
    char bitt = bitRead(number, i);
    digitalWrite(segs[i], bitt);
  }
}

Note a number of corrections.

You will need to re-wire your segments (and everything following). You are not allowed to use pin 0 or pin 1, so the segments must run from pin 2 to pin 8.

Your buttons should - as I explained before - be connected from pins 10 and 11 to ground. You should not need pull-ups by using INPUT_PULLUP. I have corrected the logic to suit.

You have not posted your wiring diagram(, nor have you cited from where you found that code :astonished: ). I presume you have each segment common connected by a resistor from the Arduino pin and the common cathodes of each digit are connected to a NPN transistor whose emitter is grounded and whose base is driven from the respective CA1, CA2 pins by 470 Ohm resistors.

I have corrected sequence errors (ghosts) in changing from one digit to another, and put in delays (which necessarily blank the display) to restrict how fast the counting occurs if you happen to hold the buttons for more than the briefest time. There are better ways of doing this, but we can leave them for later.

Maybe rather than having code written for you, you should go to the effort of learning it?

I'm trying to learn this stuff and would never expect anyone to write this stuff for me... Find some YouTube tutorials on the Arduino and even in C to help you learn

Ewdie:
Maybe rather than having code written for you, you should go to the effort of learning it?

I'm trying to learn this stuff and would never expect anyone to write this stuff for me... Find some YouTube tutorials on the Arduino and even in C to help you learn

+1

What a novel idea! Learning.

I never understood the point of having code written for you.
Is the OP going to do that with every new project?
When does one actually learn anything when everything is done for you?

I'm using one of these to get myself comfortable with being around code prior to progressing on to C in a more 'proper' setting... There is no point in me doing this and having no understanding of the flashing lights on my board xD

Thanks for every one for the tips .I'm actually new to arduino but have the basic knowledge in arduino coding .The reason i did this post is because i didnt understand the solution made with the binary counting , i used that coding and made a real circuit and it's working but i still dont understand it . I dont have shift registers and im trying to find a solution without using neither registers neither binary .I have the general idea for the solution wich is using two variables 'i' and 'j' for exemple to roll the 2 digits but when i think about adding a counter and a decounter to the program i frankly dont have an idea how to do that and im still trying to find a solution so can anyone guide to that .And thanks again

Sorry, your comments are a bit confusing.

Did the code I wrote perform the job you wanted, a 0 to 99 counter, or did it not? You did need to correct your wiring for it.

If it did, I (or others) can explain the details where you are unsure how it works; if it did not, what did it actually do?

hello thanks for the support and help , you code worked perfectly and i just modified it to suit my wiring.Can you tell why am not allowed to use pins 0 and 1?
One problem (really not a big deal ) is that after 99 is continues to count (hexadecimal numbers ) but that's fine. Thanks again for the help .I'm still working on the other solution without using the binary system .

yahyaz:
Can you tell why am not allowed to use pins 0 and 1?

Because they are the serial interface.