User Tools

Site Tools


informatica:arduino:esp32:cronometro

This is an old revision of the document!


tags: cronómetro cronometro

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Max72xxPanel.h>


long duration;

//Vcc - Vcc
//Gnd - Gnd
//Din - Mosi (Pin 11)
//Cs  - SS (Pin 10)
//Clk - Sck (Pin 18)
const int pinCS = 5;

int d=0;
int s=0;
int S=0;
int m=0;

int resetmillis=0;

const int trigPin = 2;
const int echoPin = 4;


const int numberOfHorizontalDisplays = 4;
//const int numberOfHorizontalDisplays = 1; // retirar comentario para una sola matriz 
const int numberOfVerticalDisplays = 1;
Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);
String tape = "0123456789: ";  //tu mensaje
const int spacer = 1;
const int width = 5 + spacer; // The font width is 5 pixels
void setup() {
  Serial.begin(115200); // Starts the serial communication
   
  matrix.setIntensity(7); // Use a value between 0 and 15 for brightness
   //
  matrix.setPosition(0, 0, 0); // The first display is at <0, 0>
  matrix.setPosition(1, 1, 0); // The second display is at <1, 0>
  matrix.setPosition(2, 2, 0); // The third display is at <2, 0>
  matrix.setPosition(3, 3, 0); // And the last display is at <3, 0>
  matrix.setRotation(0, 1);    // Display is position upside down
  matrix.setRotation(1, 1);    // Display is position upside down
  matrix.setRotation(2, 1);    // Display is position upside down
  matrix.setRotation(3, 1);    // Display is position upside down
  
resetmillis=millis();

}
void loop() {
  
  //Primero dibujamos los 2 puntos tape[10] son los puntos en la cadena tape y 9 y 23 la posición empezando a contar por la izquierda teniendo los pins a la izquierda y el 0 es la tercera columna

  //Cada número ocupa 5 espacios, mas 1 de separación a cada lado
  //matrix.drawChar(23, 1, tape[10], HIGH, LOW, 1);
  //matrix.drawChar(9, 1, tape[10], HIGH, LOW, 1);
  
  matrix.drawChar(23, 1, tape[10], HIGH, LOW, 1);  
  matrix.drawChar(9, 1, tape[10], HIGH, LOW, 1);  

  int Tmillis=(millis()-resetmillis)/100;
  
  int D = Tmillis/60;
  int m  = Tmillis/600;
  int S  = Tmillis/100-m*6;
  int s  = Tmillis/10-m*60-S*10;
  int d  = Tmillis-m*600-S*100-s*10;
  
  matrix.drawChar(27, 1, tape[d], HIGH, LOW, 1);
  matrix.drawChar(19, 1, tape[s], HIGH, LOW, 1);
  matrix.drawChar(13, 1, tape[S], HIGH, LOW, 1);
  matrix.drawChar(5, 1, tape[m], HIGH, LOW, 1);
Serial.println(tape[d]);
  matrix.write(); // Send bitmap to display
  
 delay(100);
}

Cronómetro con pulsador emergencia

Empieza con 0:00:00
Un click empieza a correr
Segundo click para y deja valor en pantalla
Tercer click vuelve al principio, vuelve a correr desde cero

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Max72xxPanel.h>

/*
Valores pulsación:
         aprieto suelto
Inicial:    0      1
1 click:    1      0
suelto:     1      1
2 click:    0      0
suelto:     0      1
*/


//Vcc - Vcc
//Gnd - Gnd
//Din - Mosi (Pin 11)
//Cs  - SS (Pin 10)
//Clk - Sck (Pin 18)
const int pinCS = 5;

// Dígitos del tiempo
int d=0; // décimas
int s=0; //  unidad de segundo (s minúscula)
int S=0; // decenas de segundo (S mayúscula)
int m=0; // minutos

int apretado = 0;
int suelto = 1;

const int buttonPin = 4;  // El pin donde está conectado el botón


int resetmillis=0;

const int numberOfHorizontalDisplays = 4;
//const int numberOfHorizontalDisplays = 1; // retirar comentario para una sola matriz 
const int numberOfVerticalDisplays = 1;
Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);
String tape = "0123456789: ";  //tu mensaje
const int spacer = 1;
const int width = 5 + spacer; // The font width is 5 pixels
void setup() {
  Serial.begin(115200); // Starts the serial communication

  pinMode(buttonPin, INPUT_PULLUP);  //funciona con botón sencillo
     
  matrix.setIntensity(7); // Use a value between 0 and 15 for brightness
   //
  matrix.setPosition(0, 0, 0); // The first display is at <0, 0>
  matrix.setPosition(1, 1, 0); // The second display is at <1, 0>
  matrix.setPosition(2, 2, 0); // The third display is at <2, 0>
  matrix.setPosition(3, 3, 0); // And the last display is at <3, 0>
  matrix.setRotation(0, 1);    // Display is position upside down
  matrix.setRotation(1, 1);    // Display is position upside down
  matrix.setRotation(2, 1);    // Display is position upside down
  matrix.setRotation(3, 1);    // Display is position upside down
  
resetmillis=millis();

//Ponemos el contador a Cero

//escribimos los : (dos puntos que separan minutos, segundos y décimas)
matrix.drawChar(23, 1, tape[10], HIGH, LOW, 1);  
matrix.drawChar(9, 1, tape[10], HIGH, LOW, 1);  

//Lo ponemos todo a CERO
matrix.drawChar(27, 1, tape[0], HIGH, LOW, 1);
matrix.drawChar(19, 1, tape[0], HIGH, LOW, 1);
matrix.drawChar(13, 1, tape[0], HIGH, LOW, 1);
matrix.drawChar(5, 1, tape[0], HIGH, LOW, 1);

matrix.write(); // Send bitmap to display

}
void loop() {
  
  //Primero dibujamos los 2 puntos tape[10] son los puntos en la cadena tape y 9 y 23 la posición empezando a contar por la izquierda teniendo los pins a la izquierda y el 0 es la tercera columna

  //Cada número ocupa 5 espacios, mas 1 de separación a cada lado
  //matrix.drawChar(23, 1, tape[10], HIGH, LOW, 1);
  //matrix.drawChar(9, 1, tape[10], HIGH, LOW, 1);
if (apretado == 1) {

  matrix.drawChar(23, 1, tape[10], HIGH, LOW, 1);  
  matrix.drawChar(9, 1, tape[10], HIGH, LOW, 1);  

  int Tmillis=(millis()-resetmillis)/100;
  
  int D = Tmillis/60;
  int m  = Tmillis/600;
  int S  = Tmillis/100-m*6;
  int s  = Tmillis/10-m*60-S*10;
  int d  = Tmillis-m*600-S*100-s*10;
  
  matrix.drawChar(27, 1, tape[d], HIGH, LOW, 1);
  matrix.drawChar(19, 1, tape[s], HIGH, LOW, 1);
  matrix.drawChar(13, 1, tape[S], HIGH, LOW, 1);
  matrix.drawChar(5, 1, tape[m], HIGH, LOW, 1);

  matrix.write(); // Send bitmap to display
}


//  if (!digitalRead(buttonPin)) {  // Si el botón está pulsado
  if (digitalRead(buttonPin)) {  // Si el botón está pulsado
    if ( suelto == 1 ) {
      suelto = 0;
      if ( apretado == 1 ) { // Paro el cronómetro
        apretado = 0;
      }
      else {  // empieza el cronómetro
        apretado = 1;
        resetmillis=millis(); 
      }
    }
  }
  else {
    suelto = 1;
  }
   
    Serial.print("A:");
    Serial.print(apretado);
    Serial.print(" S:");
    Serial.println(suelto);
    //delay(500); 
}
informatica/arduino/esp32/cronometro.1681769534.txt.gz · Last modified: 2023/04/17 22:12 by jose