Pages: [1]   Go Down
Author Topic: Help with counting from 0 to 99 with two separated displays  (Read 554 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, i am making a project about scoreboard for basketball, then I'm using two buttons decoders and two display. I have achieved increasing an decreasing point using the buttons but only from 0 to 9; i ont know how to make that when it pass nine to obviously ten make that display turn to zero and the other to one to continue with the increasing of the point. Also i wonder how can I reduce my code for showing the number I thought using multidimensional arrays but didnt work till now... thanks for the help

Code:
const int botonIncremento = 53;
const int botonDecremento = 52;
const int deco_entradas1[4] = {
  26,27,28,29};//A, B, C, C entradas
int contador = 0;
int estadoBotonIncremento= 0;
int estadoAnteriorBotonIncremento = 0;
int estadoBotonDecremento = 0;
int estadoAnteriorBotonDecremento = 0;
int tiempoBotonIncremento = 0;

void setup()
{
  pinMode(53, INPUT);
  for(int a = 0; a < 4; a++){
    pinMode(deco_entradasa[a], OUTPUT);
  }

}

void loop()
{
  //Incremento
  estadoBotonIncremento = digitalRead(botonIncremento);
  if(estadoBotonIncremento != estadoAnteriorBotonIncremento){
    if(estadoBotonIncremento == HIGH)
    {
      delay(50);
      contador++;

      if (contador > 9)
      {
        contador = 0;
      }
    }
  }
  estadoAnteriorBotonIncremento = estadoBotonIncremento;


  //Decremento
  estadoBotonDecremento = digitalRead(botonDecremento);
  if(estadoBotonDecremento != estadoAnteriorBotonDecremento){
    if(estadoBotonDecremento == HIGH)
    {
      delay(50);
      contador--;

      if (contador < 0)
      {
        contador = 0;

      }
    }
  }
  estadoAnteriorBotonDecremento = estadoBotonDecremento;

  switch(contador)
  {
  case 0:
    digitalWrite(26, LOW);
    digitalWrite(27, LOW);
    digitalWrite(28, LOW);
    digitalWrite(29, LOW);
    break;

  case 1:
    digitalWrite(26, LOW);
    digitalWrite(27, LOW);
    digitalWrite(28, LOW);
    digitalWrite(29, HIGH);
    break;

  case 2:
    digitalWrite(26, LOW);
    digitalWrite(27, LOW);
    digitalWrite(28, HIGH);
    digitalWrite(29, LOW);
    break;

  case 3:
    digitalWrite(26, LOW);
    digitalWrite(27, LOW);
    digitalWrite(28, HIGH);
    digitalWrite(29, HIGH);
    break;

  case 4:
    digitalWrite(26, LOW);
    digitalWrite(27, HIGH);
    digitalWrite(28, LOW);
    digitalWrite(29, LOW);
    break;

  case 5:
    digitalWrite(26, LOW);
    digitalWrite(27, HIGH);
    digitalWrite(28, LOW);
    digitalWrite(29, HIGH);
    break;

  case 6:
    digitalWrite(26, LOW);
    digitalWrite(27, HIGH);
    digitalWrite(28, HIGH);
    digitalWrite(29, LOW);
    break;

  case 7:
    digitalWrite(26, LOW);
    digitalWrite(27, HIGH);
    digitalWrite(28, HIGH);
    digitalWrite(29, HIGH);
    break;

  case 8:
    digitalWrite(26, HIGH);
    digitalWrite(27, LOW);
    digitalWrite(28, LOW);
    digitalWrite(29, LOW);
    break;

  case 9:
    digitalWrite(26, HIGH);
    digitalWrite(27, LOW);
    digitalWrite(28, LOW);
    digitalWrite(29, HIGH);
    break;
  }
}
moderatore upfdate: added code tags (#button above smileys)  and indentation  (CTRL-T in IDE)
« Last Edit: June 14, 2014, 06:06:40 am by robtillaart » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, i am making a project about scoreboard for basketball, then I'm using two buttons decoders and two display. I have achieved increasing an decreasing point using the buttons but only from 0 to 9; i ont know how to make that when it pass nine to obviously ten make that display turn to zero and the other to one to continue with the increasing of the point. Also i wonder how can I reduce my code for showing the number I thought using multidimensional arrays but didnt work till now... thanks for the help

const int botonIncremento = 53;
const int botonDecremento = 52;
const int deco_entradas1[4] = {26,27,28,29};//A, B, C, C entradas
int contador = 0;
int estadoBotonIncremento= 0;
int estadoAnteriorBotonIncremento = 0;
int estadoBotonDecremento = 0;
int estadoAnteriorBotonDecremento = 0;
int tiempoBotonIncremento = 0;

void setup()
  {
    pinMode(53, INPUT);
    for(int a = 0; a < 4; a++){
    pinMode(deco_entradasa[a], OUTPUT);}
   
  }
 
void loop()
  {
    //Incremento
    estadoBotonIncremento = digitalRead(botonIncremento);
    if(estadoBotonIncremento != estadoAnteriorBotonIncremento){
    if(estadoBotonIncremento == HIGH)
      {
        delay(50);
        contador++;
 
      if (contador > 9)
      {
        contador = 0;
      }
    }
    }
    estadoAnteriorBotonIncremento = estadoBotonIncremento;
   
     
    //Decremento
    estadoBotonDecremento = digitalRead(botonDecremento);
    if(estadoBotonDecremento != estadoAnteriorBotonDecremento){
    if(estadoBotonDecremento == HIGH)
      {
        delay(50);
        contador--;
       
      if (contador < 0)
      {
        contador = 0;
   
      }
    }
    }
    estadoAnteriorBotonDecremento = estadoBotonDecremento;
 
    switch(contador)
  {
    case 0:
    digitalWrite(26, LOW);
    digitalWrite(27, LOW);
    digitalWrite(28, LOW);
    digitalWrite(29, LOW);
    break;
   
    case 1:
    digitalWrite(26, LOW);
    digitalWrite(27, LOW);
    digitalWrite(28, LOW);
    digitalWrite(29, HIGH);
    break;
   
    case 2:
    digitalWrite(26, LOW);
    digitalWrite(27, LOW);
    digitalWrite(28, HIGH);
    digitalWrite(29, LOW);
    break;
   
    case 3:
    digitalWrite(26, LOW);
    digitalWrite(27, LOW);
    digitalWrite(28, HIGH);
    digitalWrite(29, HIGH);
    break;
   
    case 4:
    digitalWrite(26, LOW);
    digitalWrite(27, HIGH);
    digitalWrite(28, LOW);
    digitalWrite(29, LOW);
    break;
   
    case 5:
    digitalWrite(26, LOW);
    digitalWrite(27, HIGH);
    digitalWrite(28, LOW);
    digitalWrite(29, HIGH);
    break;
   
    case 6:
    digitalWrite(26, LOW);
    digitalWrite(27, HIGH);
    digitalWrite(28, HIGH);
    digitalWrite(29, LOW);
    break;
   
    case 7:
    digitalWrite(26, LOW);
    digitalWrite(27, HIGH);
    digitalWrite(28, HIGH);
    digitalWrite(29, HIGH);
    break;
   
    case 8:
    digitalWrite(26, HIGH);
    digitalWrite(27, LOW);
    digitalWrite(28, LOW);
    digitalWrite(29, LOW);
    break;
   
    case 9:
    digitalWrite(26, HIGH);
    digitalWrite(27, LOW);
    digitalWrite(28, LOW);
    digitalWrite(29, HIGH);
    break;
   }
    }
Logged

UK
Offline Offline
Tesla Member
***
Karma: 141
Posts: 7975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This isn't a very elegant solution but it should be simple if you need to keep the two digit values separate

Code:
int contador = 0;
int contador2 = 0;

////

 if (contador > 9)
      {
        contador = 0;
        contador2 ++;
        if (contador2 > 9) {
            contador = 0;
            contador2 = 0;
        }
      }

...R
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14009
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


the big switch can be written in a small function

Code:
void displayCount(int contador)
{
    digitalWrite(26, contador & 0x08);
    digitalWrite(27, contador & 0x04);
    digitalWrite(28, contador & 0x02);
    digitalWrite(29, contador & 0x01);
}

you could extend that to

Code:
void displayCount(int contador, int display)
{
  if (display == 0)
  {
    digitalWrite(26, contador & 0x08);  // google  for bit wise and 
    digitalWrite(27, contador & 0x04);
    digitalWrite(28, contador & 0x02);
    digitalWrite(29, contador & 0x01);
  }
  else
  {
    digitalWrite(36, contador & 0x08);  // pins as example
    digitalWrite(37, contador & 0x04);
    digitalWrite(38, contador & 0x02);
    digitalWrite(39, contador & 0x01);
  }
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 227
Posts: 14009
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Topics merged:   please do not crosspost - that wastes time...
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This isn't a very elegant solution but it should be simple if you need to keep the two digit values separate

Code:
int contador = 0;
int contador2 = 0;

////

 if (contador > 9)
      {
        contador = 0;
        contador2 ++;
        if (contador2 > 9) {
            contador = 0;
            contador2 = 0;
        }
      }

...R

Thank for ur help, but could u help me with the decreasing event? I really dont get it so much :/
Logged

Pages: [1]   Go Up
Jump to: