Hola, estoy tratando de aprender a usar arduino, por ahora no entiendo casi nada.
Estoy queriendo hacer un velocímetro con display de 7 segmentos, encontré un código pero lleva una librería TM1637.h y no logro hacerla funcionar, usé los códigos de prueba de esa librería y no me funciona ninguno, no enciende el display.
Para probar que funciona el display usé el código de prueba de la librería TM1637Display.h y con esa funciona todo bien.
será que TM1637.h es para otra placa? o que puede estar pasando?
El código del velocímetro es
// Matthew McMillan
// @matthewmcmillan
// http://matthewcmcmillan.blogspot.com
//
// Digital speedometer that uses a TM1637 type display
//
// Code is written for an Arduino UNO
//
// VSS on car connects to digital pin 5
// CLK on display to digital pin 3
// DIO on display to digital pin 2
//
//
// http://playground.arduino.cc/Main/TM1637
//
//
#include "TM1637.h" // Seven Segment display library
// Setup TM1637 Display
#define CLK 2 //pin definitions for TM1637 and can be changed to other ports
#define DIO 3
TM1637 tm1637(CLK,DIO);
const int hardwareCounterPin = 5;
const int samplePeriod = 1000; //in milliseconds
const float pulsesPerMile = 4000; //This value is different for different vehicles
const float convertMph = pulsesPerMile/3600;
unsigned int count;
float mph;
unsigned int imph;
int roundedMph;
int previousMph;
int prevCount;
void setup(void) {
Serial.begin(9600);
//Serial.println("Startup...");
tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
tm1637.init();
TCCR1A = 0; //Configure hardware counter
TCNT1 = 0; // Reset hardware counter to zero
}
void loop() {
/////////////////////////////////////////////////////////////
// This uses the hardware pulse counter on the Arduino.
// Currently it collects samples for one second.
//
bitSet(TCCR1B, CS12); // start counting pulses
bitSet(TCCR1B, CS11); // Clock on rising edge
delay(samplePeriod); // Allow pulse counter to collect for samplePeriod
TCCR1B = 0; // stop counting
count = TCNT1; // Store the hardware counter in a variable
TCNT1 = 0; // Reset hardware counter to zero
mph = (count/convertMph)*10; // Convert pulse count into mph.
imph = (unsigned int) mph; // Cast to integer. 10x allows retaining 10th of mph resolution.
int x = imph / 10;
int y = imph % 10;
// Round to whole mile per hour
if(y >= 5){
roundedMph = x + 1;
}else{
roundedMph = x;
}
//If mph is less than 1mph just show 0mph.
//Readings of 0.9mph or lower are some what erratic and can
//occasionally be triggered by electrical noise.
if(x == 0){
roundedMph = 0;
}
// Don't display mph readings that are more than 50 mph higher than the
// previous reading because it is probably a spurious reading.
// Accelerating 50mph in one second is rocketship fast so it is probably
// not real.
if((roundedMph - previousMph) > 50){
tm1637.display(previousMph);
}else{
tm1637.display(roundedMph);
}
previousMph = roundedMph; // Set previousMph for use in next loop.
}
Hola, viendo otros códigos conseguí cambiar la librería, lo probé con un taladro y parece andar, no se si hará bien las cuentas. el fin de semana si tengo tiempo voy a instalar los imanes en el cardan para darle los pulsos al arduino y voy a probar ambos codigos.
Asi me quedó con la librería TM1637Display.h.
Muchas gracias!!
// Matthew McMillan
// @matthewmcmillan
// http://matthewcmcmillan.blogspot.com
//
// Digital speedometer that uses a TM1637 type display
//
// Code is written for an Arduino UNO
//
// VSS on car connects to digital pin 5
// CLK on display to digital pin 3
// DIO on display to digital pin 2
//
//
// http://playground.arduino.cc/Main/TM1637
//
//
#include "TM1637Display.h" // Seven Segment display library
// Setup TM1637 Display
#define CLK 2 //pin definitions for TM1637 and can be changed to other ports
#define DIO 3
TM1637Display display(CLK,DIO);
const int hardwareCounterPin = 5;
const int samplePeriod = 1000; //in milliseconds
const float pulsesPerMile = 4080; //This value is different for different vehicles
const float convertMph = pulsesPerMile/3600;
unsigned int count;
float mph;
unsigned int imph;
int roundedMph;
int previousMph;
int prevCount;
void setup(void) {
Serial.begin(9600);
//Serial.println("Startup...");
display.setBrightness(4);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
TCCR1A = 0; //Configure hardware counter
TCNT1 = 0; // Reset hardware counter to zero
}
void loop() {
/////////////////////////////////////////////////////////////
// This uses the hardware pulse counter on the Arduino.
// Currently it collects samples for one second.
//
bitSet(TCCR1B, CS12); // start counting pulses
bitSet(TCCR1B, CS11); // Clock on rising edge
delay(samplePeriod); // Allow pulse counter to collect for samplePeriod
TCCR1B = 0; // stop counting
count = TCNT1; // Store the hardware counter in a variable
TCNT1 = 0; // Reset hardware counter to zero
mph = (count/convertMph)*10; // Convert pulse count into mph.
imph = (unsigned int) mph; // Cast to integer. 10x allows retaining 10th of mph resolution.
int x = imph / 10;
int y = imph % 10;
// Round to whole mile per hour
if(y >= 5){
roundedMph = x + 1;
}else{
roundedMph = x;
}
//If mph is less than 1mph just show 0mph.
//Readings of 0.9mph or lower are some what erratic and can
//occasionally be triggered by electrical noise.
if(x == 0){
roundedMph = 0;
}
// Don't display mph readings that are more than 50 mph higher than the
// previous reading because it is probably a spurious reading.
// Accelerating 50mph in one second is rocketship fast so it is probably
// not real.
if((roundedMph - previousMph) > 50){
display.showNumberDec(previousMph);
}else{
display.showNumberDec(roundedMph);
}
previousMph = roundedMph; // Set previousMph for use in next loop.
}
1h despues de que yo posteo el trabajo te sales con una solución que no creo hayas hecho inmediatamente, sino que tenías desde hace algun tiempo. Bien por ti, mal por mi.
Me llevó un par de horas simular y ver cual era tu error.
Surbyte te pido mil disculpas por no haber dicho que conseguí solucionar el problema de la librería, empecé con este proyecto sin entender nada, busqué y busqué hasta conseguir entender algo y me metí tanto en estudiar y escribir código que no me acordé de escribir acá, como me sobraba espacio en el arduino le agregué un vigia de motor, que avisa falla en temperatura, aceite y batería.
volviendo al código, estuve probando el tuyo, marca lo mismo que el de Matthew McMillan, pero me gustó mas que marca 0000 en vez de 0 como el otro, y refresca mas rápido los números. Voy a usarlo. Todavía no logro entender nada de como funciona el void loop de tu código, voy a estudiarlo a ver si lo entiendo.
Gracias y disculpas nuevamente
Te explico la parte importante. Tu haces una cuenta usando interrupciones. El contador lee un número de pulsos. Gracias a esto
mph = (count/convertMph)*10; // Convert pulse count into mph.
conviertes esos pulsos en milllas por hora. Hasta ahi okay.
Pero ahora resulta que el TM1637 requiere que los números se envien 1 a 1. La unidad de mil, la centena la decena y la unidad.
El resto del código solo separa usando enteros un número cualquiera en sus partes indviduales.
Supon que tiene 1234 para mostrar.
Como muestras la unidad de mil? pues divides el numero en forma entera por 1000
te da 1 con resto 234. El número 1 lo almaceno en TimeDisp[0] que es una variable con 4 datos de 0 a 3. Todos albergaràn un nùmero.
TimeDisp[0] = imph / 1000;
Con ese resultado calculo el siguiente. Como hallo el siguiente pues solo observa lo que hice porque es repetitivo.
Al final y por eso me detengo tengo esto % que es mòdulo de algo.
Hola Surbyte, muchas gracias por la explicación!, ahora lo entendí, me está gustando mucho esto de programar Arduino, me está costando un montón, pero voy entendiendo
Saludos!