Go Down

Topic: Led 7 Segments 2 buttons. Counter and change between displys (Read 326 times) previous topic - next topic

soydepueblo

Hello,  I want your help  to do this please (I've multiplexed 3 LED's):
One of the two buttons allow you to select the module you want to display (only displayed 1 simultaneously), ie, each press will advance to the next module, considering that there are 3 modules are numbered from 1 to 3. The other button, when clicked, will increase by 1 the value of the selected display to the previous button. The program should remember that value had each of the displays.
-All displays start in first round '0' and when the first round is over, each display will remember their value, starting with that value.  For example:
First round [ 0,0,0] We put: [2,5,8] So: Second round will start [2,5,8]
I tried putting how to turn on each display with button 2 (P2) but I couldn't check it, I would fail to do that and to remember the value, I have been told that it works with a vector
Code: [Select]
nt num = 0;
int delayTime;
int counter=1;
// estadoBoton1 será el que pulse para contar
//estadoBoton2 será el que cambie entre módulos
int estadoBoton1 = 0;
int estadoBoton2 = 0;
int P1 = 3;
int P2 = 2;
//Displays multiplexados
int LED1=4;
int LED2=5;
int LED3=6;
void setup (){
//Entradas digitales de los Botones
pinMode(P2, INPUT);
pinMode(P1, INPUT);
//Salidas digitales
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
//3 Leds multiplexados
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);

}

void loop(){

estadoBoton1 = digitalRead(P1);
estadoBoton2 = digitalRead(P2);
Serial.println(estadoBoton1);
delay(50);
Serial.println(estadoBoton2);
delay(50);

//Contador del 1 al 9 y vuelve al 0
if (estadoBoton1 == HIGH) {
    num = num+ 1;
  }
if (num >= 10){
  num=0;
}
//A partir de aqui lo he copiado de internet que se supone que es para cambiar entre displays, pero no he comprobado si funciona
if (estadoBoton2 == HIGH) {
    counter + 1;
}
    //resetear contador
    if(counter == 4) {
      counter = 1;
    }
   
  }
 
  if(counter == 1)
  {
    delayTime = 250;
  }
  else if(counter == 2)
  {
    delayTime = 100;
  }
  else if(counter == 3)
  {
    delayTime = 50;
  }
 
  //Light show!
  digitalWrite(LED4, HIGH);
  delay(delayTime);
  digitalWrite(LED4, LOW);
  digitalWrite(LED5, HIGH);
  delay(delayTime);
  digitalWrite(LED5, LOW);
  digitalWrite(LED6, HIGH);
  delay(delayTime);
  digitalWrite(LED6, LOW);
  delay(delayTime);
}


//Esto ya lo teniamos de antes, es para mostrar cada uno de los numeros
switch (num) {
case 0:
limpiar();
cero();
break;
case 1:
limpiar();
uno();
break;
case 2:
limpiar();
dos();
break;
case 3:
limpiar();
tres();
break;
case 4:
limpiar();
cuatro();
break;
case 5:
limpiar();
cinco();
break;
case 6:
limpiar();
seis();
break;
case 7:
limpiar();
siete();
break;
case 8:
limpiar();
ocho();
break;
case 9:
limpiar();
nueve();
}

}

void cero(){
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
}

void uno(){
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
}
void dos(){
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(13,HIGH);
}
void tres(){
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(13,HIGH);
}
void cuatro(){
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
}
void cinco(){
digitalWrite(7,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
}
void seis(){
digitalWrite(7,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);

}
void siete(){
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);

}
void ocho(){

digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
}

void nueve(){

digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);

}
void limpiar(){
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
digitalWrite(13,LOW);
}

arduinodlb

You never store the numbers for later use.

You need to look up arrays, and create an array of 3 numbers, You also need to store which element of the array you are currently using for your changes.

Also, if you post the code again, can you please fix the indentation of the functions. It's very difficult to read without correct indentation.
Do not IM me. I will not respond. Ask questions in the forum.

soydepueblo


You never store the numbers for later use.

You need to look up arrays, and create an array of 3 numbers, You also need to store which element of the array you are currently using for your changes.

Also, if you post the code again, can you please fix the indentation of the functions. It's very difficult to read without correct indentation.


Ok, please help me! How I put the Arrays¿
Code: [Select]
/*
Realizar un programa en el Arduino para controlar un display de 7 segmentos x 3.
Para la realización de la práctica se necesitan un display de 7 segmentos de 3 módulos (x3)
y dos pulsadores (no se cuentan los cables y resistencias necesarias).
Uno de los dos pulsadores permitirá seleccionar el módulo que se quiere mostrar
(sólo se muestra 1 simultáneamente), es decir, cada pulsación se avanzará al módulo siguiente,
considerando que hay 3 módulos que se numerarán del 1 al 3. El otro pulsador, cuando se pulse,
incrementará en 1 el valor del display seleccionado con el pulsador anterior.
El programa debe recordar que valor tenía cada uno de los displays.Nos ha dicho el profesor que esto
ultimo se hace con vectores */

int num = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
int counter=0;
// estadoBoton1 será el que pulse para contar
//estadoBoton2 será el que cambie entre módulos
int estadoBoton1 = 0;
int estadoBoton2 = 0;
int P1 = 3;
int P2 = 2;
//Displays multiplexados
int LED4=4;
int LED5=5;
int LED6=6;
void setup (){
//Entradas digitales de los Botones
pinMode(P2, INPUT);
pinMode(P1, INPUT);
//Salidas digitales
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
//3 Leds multiplexados
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);

}

void loop(){

estadoBoton1 = digitalRead(P1);
estadoBoton2 = digitalRead(P2);
Serial.println(estadoBoton1);
delay(50);
Serial.println(estadoBoton2);
delay(50);

//Contador del 1 al 9 y vuelve al 0
if(estadoBoton1==1 && estadoBoton1<=9){
  num2 = num2++;
} else if (num2 > 9){
// limpiar();
limpiar();
delay(100);
cero();// cero
num2 = 0;
}
if(estadoBoton1==1 && estadoBoton1<=9){
  num3 = num3++;
} else if (num3 > 9){
// limpiar();
limpiar();
delay(100);
cero();// cero
num3 = 0;
}

// suma del 3º
  if(estadoBoton1==1 && estadoBoton1<=9){
  num4 = num4++;
} else if (num4 > 9){
// limpiar();
limpiar();
delay(100);
cero();// cero
num4 = 0;
}

//Boton 2 cambia de módulo
if (estadoBoton2 == HIGH) {
    counter= counter + 1;
}
    if (counter >= 3) {
      counter=0;
}

switch (counter) {
   //LOW enciende el Led deseado
  case 0:
  digitalWrite(LED4, HIGH);
  digitalWrite(LED6, HIGH);
  digitalWrite(LED5, LOW);
          switch (num2) {
          case 0:
          limpiar();
          cero();
          break;
          case 1:
          limpiar();
          uno();
          break;
          case 2:
          limpiar();
          dos();
          break;
          case 3:
          limpiar();
          tres();
          break;
          case 4:
          limpiar();
          cuatro();
          break;
          case 5:
          limpiar();
          cinco();
          break;
          case 6:
          limpiar();
          seis();
          break;
          case 7:
          limpiar();
          siete();
          break;
          case 8:
          limpiar();
          ocho();
          break;
          case 9:
          limpiar();
          nueve();
          break;
          }
          break;

  case 1:
  //LOW enciende el Led deseado
  digitalWrite(LED4, HIGH);
  digitalWrite(LED6, LOW);
  digitalWrite(LED5, HIGH);
        switch (num3) {
        case 0:
        limpiar();
        cero();
        break;
        case 1:
        limpiar();
        uno();
      break;
      case 2:
      limpiar();
      dos();
      break;
      case 3:
      limpiar();
      tres();
      break;
      case 4:
      limpiar();
      cuatro();
      break;
      case 5:
      limpiar();
      cinco();
      break;
      case 6:
      limpiar();
      seis();
      break;
      case 7:
      limpiar();
      siete();
      break;
      case 8:
      limpiar();
      ocho();
      break;
      case 9:
      limpiar();
      nueve();
      break;
      }
  break;
  case 2:
    //LOW enciende el Led deseado
    digitalWrite(LED4, LOW);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED5, HIGH);
    switch (num4) {
        case 0:
        limpiar();
        cero();
        break;
        case 1:
        limpiar();
        uno();
        break;
        case 2:
        limpiar();
        dos();
        break;
        case 3:
        limpiar();
        tres();
        break;
        case 4:
        limpiar();
        cuatro();
        break;
        case 5:
        limpiar();
        cinco();
        break;
        case 6:
        limpiar();
        seis();
        break;
        case 7:
        limpiar();
        siete();
        break;
        case 8:
        limpiar();
        ocho();
        break;
        case 9:
        limpiar();
        nueve();
        break;
        }
        break;
         
}
 
   
}


//Mostrar numeros
switch (num) {
case 0:
limpiar();
cero();
break;
case 1:
limpiar();
uno();
break;
case 2:
limpiar();
dos();
break;
case 3:
limpiar();
tres();
break;
case 4:
limpiar();
cuatro();
break;
case 5:
limpiar();
cinco();
break;
case 6:
limpiar();
seis();
break;
case 7:
limpiar();
siete();
break;
case 8:
limpiar();
ocho();
break;
case 9:
limpiar();
nueve();
}

void cero(){
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
}

void uno(){
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
}
void dos(){
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(13,HIGH);
}
void tres(){
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(13,HIGH);
}
void cuatro(){
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
}
void cinco(){
digitalWrite(7,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
}
void seis(){
digitalWrite(7,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);

}
void siete(){
digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);

}
void ocho(){

digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);
}

void nueve(){

digitalWrite(7,HIGH);
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);

}
void limpiar(){
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
digitalWrite(13,LOW);
}


I have an error: jueves_digital:248: error: expected unqualified-id before 'switch'

arduinodlb

I can't read your code. Your need to fix your indentation so you can see what functions are inside what code blocks.
Do not IM me. I will not respond. Ask questions in the forum.

Go Up