Skip to content

Instantly share code, notes, and snippets.

@matt448
Created January 13, 2016 02:30
Show Gist options
  • Select an option

  • Save matt448/2c3742d1df04862b3fb9 to your computer and use it in GitHub Desktop.

Select an option

Save matt448/2c3742d1df04862b3fb9 to your computer and use it in GitHub Desktop.

Revisions

  1. matt448 created this gist Jan 13, 2016.
    93 changes: 93 additions & 0 deletions Speedometer_TM1637.ino
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    // 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.
    }