User Tools

Site Tools


informatica:arduino:esp32:cronometro

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
informatica:arduino:esp32:cronometro [2023/04/17 12:13] – created joseinformatica:arduino:esp32:cronometro [2023/04/17 23:14] (current) jose
Line 1: Line 1:
 tags: cronómetro cronometro tags: cronómetro cronometro
  
 +
 +<code>
 +#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);
 +}
 +</code>
 +
 +====== 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
 +
 +<code>
 +#include <SPI.h>
 +#include <Adafruit_GFX.h>
 +#include <Max72xxPanel.h>
 +
 +/*
 +Valores pulsación:
 +         aprieto suelto
 +Inicial:    0      1
 +1 click:    1      0
 +suelto:          1
 +2 click:    0      0
 +suelto:          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); 
 +}
 +</code>
 +
 +====== Webserver ======
 +<code>
 +from http.server import BaseHTTPRequestHandler, HTTPServer
 +from urllib.parse import urlparse, parse_qs
 +from datetime import datetime
 +
 +class MiServidor(BaseHTTPRequestHandler):
 +    
 +    def do_GET(self):
 +        # Parsear la URL para obtener los parámetros
 +        url = urlparse(self.path)
 +        params = parse_qs(url.query)
 +        
 +        # Obtener el valor de la variable
 +        variable = params.get('variable', None)
 +        if variable is not None:
 +            variable = variable[0]
 +            
 +            # Obtener la fecha actual en el formato deseado
 +            fecha_actual = datetime.now().strftime('%Y%m%d')
 +            
 +            # Guardar la fecha y el valor de la variable en el archivo
 +            with open('archivo.txt', 'a') as archivo:
 +                archivo.write(f"{fecha_actual} {variable}\n")
 +            
 +            # Responder con un código 200
 +            self.send_response(200)
 +            self.end_headers()
 +            
 +            return
 +         
 +        # Si no se especifica la variable, responder con un código 400
 +        self.send_response(400)
 +        self.end_headers()
 +
 +def iniciar_servidor():
 +    servidor = HTTPServer(('localhost', 8080), MiServidor)
 +    print('Servidor iniciado en http://localhost:8080')
 +    servidor.serve_forever()
 +
 +if __name__ == '__main__':
 +    iniciar_servidor()
 +</code>
 +
 +Se hace una petición:
 +  curl localhost:8080/?variable=3
  
informatica/arduino/esp32/cronometro.1681733599.txt.gz · Last modified: 2023/04/17 12:13 by jose