Go Down

Topic: Help with counting from 0 to 99 with two separated displays (Read 601 times) previous topic - next topic

adrian10

Jun 14, 2014, 07:47 am Last Edit: Jun 14, 2014, 01:06 pm by robtillaart Reason: 1
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: [Select]

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)

adrian10

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

Robin2

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

Code: [Select]
int contador = 0;
int contador2 = 0;

////

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


...R

robtillaart


the big switch can be written in a small function

Code: [Select]

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: [Select]

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);
  }
}
Rob Tillaart

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

robtillaart

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

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

adrian10


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

Code: [Select]
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 :/

Go Up