What am I doing wrong with tm1637 quad 7 segment display

Hi all

So I have a 4 digit display, it is four 7 segment displays controlled via a tm1637.

I have a sonar spitting out inches, but (with a lot of help from users here) got it to format it into Feet and Inches.

So this ends up with a variable that is 4 digits long.

// UKHeliBob's math to work out Feet and Inches seperatly 
  int feet = inches / 12 ;  //divides inches by 12, discards any remainder and puts the result in the feet variable (integer division)
  inches %= 12 ;            //divides inches by 12 and puts the remainder in the inches variable (modulo operator)


// Format Feet and Inches from above into a buffer variable, then print via serial. 
//int buffer;
char buffer[4];
sprintf (buffer, "%02d%02d", feet, inches);
Serial.println (buffer);

//Display code from example 
    distance = buffer;   /* read the value from the sensor */   
    memset(bits, 0, 4);                             /* reset array when we use it */
    for(int i = 3; i >= 0; i--) {
        /* get single bits of the analog value */
        bits[i] = distance % 10;
        distance = distance / 10;  
        tm1637.display(i, bits[i]);                 /* display by 4-digital display */
    }
  delay(100);
}

The problem is, while it prints fine in the serial output, the display only shows "2286".
No matter what the sonar is reporting or what is shown on the serial output.

Any ideas on what I am doing wrong?
Thanks.

Display library libraries/DigitalTube at master · reeedstudio/libraries · GitHub

char buffer[4];
sprintf (buffer, "%02d%02d", feet, inches);

You can NOT fit 4 digits and a NULL in a 4 element array.

    distance = buffer;   /* read the value from the sensor */

WTF is this supposed to do?

PaulS:

char buffer[4];

sprintf (buffer, "%02d%02d", feet, inches);



You can NOT fit 4 digits and a NULL in a 4 element array.



distance = buffer;  /* read the value from the sensor */



WTF is this supposed to do?

So "char buffer[5];" ?

The LCD code use to take the long variable from the sonar before so they had that line to make it easier on the eyes.
I kept it in as the code uses the variable 3 times so I can change the bit after the = to make a mass change.
If that makes sense?

So "char buffer[5];" ?

Yes. I prefer even sizes, but odd works, too.

If that makes sense?

It does not, since the code snippet you posted does not have all the data types specified. Even if buffer and distance are the same type, that is NOT how to copy the contents from one array to another.

PaulS:
Yes. I prefer even sizes, but odd works, too.
It does not, since the code snippet you posted does not have all the data types specified. Even if buffer and distance are the same type, that is NOT how to copy the contents from one array to another.

I really don't understand any of this :stuck_out_tongue:

I just don't get how to put the output of that "buffer" into this display..

I just don't get how to put the output of that "buffer" into this display..

There are 4 characters (that are digits) in the buffer. There are 4 places in the display. What is the problem?

        tm1637.display(i, buffer[i]);

in a for loop, for i = 0 to 3 (or 3 to 0).

Don't know which TM1637 library you're using, but this works for me:

#include <TM1637Display.h>

#define CLK 6
#define DIO 5

TM1637Display display(CLK, DIO);

const byte blank = 0;
byte feet, inches;

void setup()
{
  Serial.begin(9600);
  for(byte i = 0;i < 4;i++)
    display.setSegments(blank,1,i);
  display.setBrightness(0x0C);
//  display.showNumberDec(tempF, false, 4 , 0);
}
void loop()
{
  inches = random(0,1200);
  Serial.println(inches);
  feet = inches / 12;
  inches %= 12;
  display.showNumberDec(feet, false, 2, 0);
  display.showNumberDec(inches, false, 2 , 2);
  delay(2000);

  
}

outsider:
Don't know which TM1637 library you're using, but this works for me:

#include <TM1637Display.h>

#define CLK 6
#define DIO 5

TM1637Display display(CLK, DIO);

const byte blank = 0;
byte feet, inches;

void setup()
{
  Serial.begin(9600);
  for(byte i = 0;i < 4;i++)
    display.setSegments(blank,1,i);
  display.setBrightness(0x0C);
//  display.showNumberDec(tempF, false, 4 , 0);
}
void loop()
{
  inches = random(0,1200);
  Serial.println(inches);
  feet = inches / 12;
  inches %= 12;
  display.showNumberDec(feet, false, 2, 0);
  display.showNumberDec(inches, false, 2 , 2);
  delay(2000);

}

Tried running that and it runs but only outputs to serial. Yes I changed the pins.

Had a go with all the libraries I have found to match.

But nothing :confused:

Post a link to the display and the lib you're using, also, which Arduino?

outsider:
Post a link to the display and the lib you're using, also, which Arduino?

Display, http://www.ebay.co.uk/itm/4-Bits-Digital-Tube-LED-Display-Module-With-Clock-Display-for-Arduino-UK-/281367800443?hash=item4182d3ea7b:g:pgoAAOxy~dNTKM8Q

Lib, libraries/DigitalTube at master · reeedstudio/libraries · GitHub

Arduino Nano v3.0 compatible ATMEGA328

outsider:
Don't know which TM1637 library you're using, but this works for me:

#include <TM1637Display.h>

#define CLK 6
#define DIO 5

TM1637Display display(CLK, DIO);

const byte blank = 0;
byte feet, inches;

void setup()
{
  Serial.begin(9600);
  for(byte i = 0;i < 4;i++)
    display.setSegments(blank,1,i);
  display.setBrightness(0x0C);
//  display.showNumberDec(tempF, false, 4 , 0);
}
void loop()
{
  inches = random(0,1200);
  Serial.println(inches);
  feet = inches / 12;
  inches %= 12;
  display.showNumberDec(feet, false, 2, 0);
  display.showNumberDec(inches, false, 2 , 2);
  delay(2000);

}

Tried to run your code again.

C:\Users\Jack\AppData\Local\Temp\ccUctUMZ.ltrans0.ltrans.o: In function `setup':

C:\Users\Jack\Documents\Arduino\sketch_jan01a/sketch_jan01a.ino:16: undefined reference to `TM1637Display::setBrightness(unsigned char, bool)'

collect2.exe: error: ld returned 1 exit status

exit status 1
Error compiling for board Arduino Nano.

Using the library that is linked on the Arduino Playground - HomePage

Pulled apart the breadboard, restarted the PC, put it all into a new sketch and it all worked...

Thank you so much Outsider, I own you a Coffee.

And thank you to everyone else in here who also helped.

Way to go! :slight_smile: K++