Skip to content

Instantly share code, notes, and snippets.

@matt448
Created January 13, 2016 02:30
Show Gist options
  • Save matt448/2c3742d1df04862b3fb9 to your computer and use it in GitHub Desktop.
Save matt448/2c3742d1df04862b3fb9 to your computer and use it in GitHub Desktop.
Digital speedometer using a TM1637 type display
// 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 3 //pin definitions for TM1637 and can be changed to other ports
#define DIO 2
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.
}
@aj6828
Copy link

aj6828 commented May 15, 2019

im tryign this one out keeps giving me a error ..

any idea thanks ..

C:\Users\Angelo\Documents\Arduino\testt\testt.ino: In function 'void loop()':

C:\Users\Angelo\Documents\Arduino\testt\testt.ino:87:31: warning: invalid conversion from 'int' to 'int8_t* {aka signed char*}' [-fpermissive]

 tm1637.display(previousMph);

                           ^

In file included from C:\Users\Angelo\Documents\Arduino\testt\testt.ino:19:0:

C:\Users\Angelo\Documents\Arduino\libraries\DigitalTube/TM1637.h:51:10: note: initializing argument 1 of 'void TM1637::display(int8_t*)'

 void display(int8_t DispData[]);

      ^

C:\Users\Angelo\Documents\Arduino\testt\testt.ino:89:30: warning: invalid conversion from 'int' to 'int8_t* {aka signed char*}' [-fpermissive]

 tm1637.display(roundedMph);

                          ^

In file included from C:\Users\Angelo\Documents\Arduino\testt\testt.ino:19:0:

C:\Users\Angelo\Documents\Arduino\libraries\DigitalTube/TM1637.h:51:10: note: initializing argument 1 of 'void TM1637::display(int8_t*)'

 void display(int8_t DispData[]);

@aj6828
Copy link

aj6828 commented May 17, 2019

having problem finding the library you used can you help me out thanks

@MaxPower472
Copy link

having problem finding the library you used can you help me out thanks

I have a same problem. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment