HCMAX7219 - LED driver library

Useful guides, libraries, and example sketches to support our Arduino based products.
PeterB
Posts: 4
Joined: Wed Dec 05, 2018 8:40 am

Re: HCMAX7219 - LED driver library

Post by PeterB » Thu Dec 06, 2018 6:30 pm

Many Thanks for your help, found the entry and deleted, new file loaded and tried. I was sure others must have used one of these displays for a similar purpose as they are ideal with the wide viewing angle, clarity, and brightness.

The displayed numbers needs to read from right to left ie start at 0.000 and increment to 0.001, 0.002 etc. The received pulses are stored in a variable from the rotary sensor on the milling machine as the table is moved and continue incrementing, a final reading may be 2.237 . The is number is processed and displayed. When the table is moved the other way the variable will decrement and the reading may go to say -1.336 depending on when the table ends up.

This is the display part of the code from the dro sketch.

Code: Select all

tmp = encoder0Pos / Slots * Mpitch; 
            lcd.setCursor(0,1); 
            lcd.print(tmp,3); 
            lcd.print(" mm");
I have added my simple test routine to your sketch but this results in a display of 1.000 then 2.000 whereas I want it to go 0.001, 0.002 and so on as count increases.

I tried adding some pics to help my description but failed miserable due to my lack of knowledge, sorry, need to do more reading!

Code: Select all

    #include <HCMAX7219.h>

    #include <HCMAX7219.h>
    #include "SPI.h
    #define LOAD 10
     
    HCMAX7219 HCMAX7219(LOAD);
   int count;  
    void setup()
    {  
      HCMAX7219.Init();
      HCMAX7219.Clear();
      Serial.begin(9600);
    }
    void loop()
    {
      count ++;
      delay (1000);
      Serial.println(count);
      
      float Number = count;
      byte DecimalPlaces = 3;
      byte NumberOfDigits = 8;
      byte Offset = 8;
     
      HCMAX7219.print7Seg(Number, DecimalPlaces, NumberOfDigits, Offset);
     
      HCMAX7219.Refresh();  
     
    }

andrew
Site Admin
Posts: 865
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCMAX7219 - LED driver library

Post by andrew » Fri Dec 07, 2018 9:58 am

Your problem is more of a general C programming issue in not knowing how variables work and how to manipulate numbers so it's a bit off topic to discuss it in this thread however I'll point you in the right direction:

You have defined the variable 'count' as type 'int'. This means it can only store integer numbers and so can't store numbers with a fractional part. I.e. it can store values such as 1, 2 ,3 10, -100 etc but it cant store numbers like 1.1, -0.05, 0.001 etc. These would just get rounded to the nearest integer. However you'll notice the variable 'Number' has been defined as type 'float' so it can store a floating point number.

The line count++ is a short hand way of saying count = count + 1. I.e. take the value stored in count, add one to it and then save it back to count. So all it will do is increment count by 1.

To make the sketch count up in 0.001 increments you could just simply divide count by 1000 by changing this line:

  1. float Number = count;
to:
  1. float Number = (float)count / 1000;

Note the (float) before the variable count. This is because as count has been defined to be an integer the C compiler will assume you also want to do the sum count/1000 as an integer and so will ignore any fractional part. Placing (float) next to the variable is called casting and it tells the compiler that although the variable is of type 'int', you actually want to calculate the sum as floating point number.

Or of course you could simply define 'Count' as a 'float' instead of an 'int'.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

PeterB
Posts: 4
Joined: Wed Dec 05, 2018 8:40 am

Re: HCMAX7219 - LED driver library

Post by PeterB » Sat Dec 08, 2018 8:13 am

Many thanks for pointing me in the right direction. Its a problem gaining experience while learning at the same time!

Phil-S
Posts: 6
Joined: Wed Jan 30, 2019 10:17 am

Re: HCMAX7219 - LED driver library

Post by Phil-S » Wed Mar 06, 2019 10:34 pm

Hello
Do you have a code example of cascading two MAX7219 displays to get me started?
I have the single driver examples and a basic counter working fine, but I would like to to display a count of Watthours of electrical energy (pulses from the electricity meter) on one display, and the average energy use on the other display.
I have a "fudged" example working that uses a constant (addr) and a pointer like HCMAX7219.print7Seg(i, 1 * addr); where i is a numerical value.
Counts might be in the order of 15000 and energy might be 0.51-kWhr.
Ideally, one display would show 15000 Wh, the other 0.51-kWhr, a mixture of text and a changing value,
It actually seems to work well by setting addr to 8, not 2.
Any help appreciated.

andrew
Site Admin
Posts: 865
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCMAX7219 - LED driver library

Post by andrew » Thu Mar 07, 2019 10:41 am

Firstly if you're using two displays, and assuming you haven't already done this, make sure you set the NUMBEROFDRIVERS definition in the HCMAX7219.h file to 2 (it defaults to 1). If you using Windows don't use notepad to edit as it messes up the formatting of the file - use something like notepad++ to edit library files.

Secondly, make sure your using the latest version of the library (currently V0.5) which can downloaded from the first post of this thread. This version contains an updated print function that has better control of how numbers are printed to the display:

  1. HCMAX7219.print7Seg(Value, DP, Digits, Offset)

Where:

Value is the number you wish to print

DP specifies the number of decimal places to display the number to.

Digits is the total number of digits to display. If the number is smaller than the specified number of digits it will pad
the left side of the number with spaces.

Offset is the column position from where the value will start from. Valid values are from 0 to 65535 (Uno/Nano etc) where:
1 = right most column
8 = left most column


So for printing a number on each display you could do something like this:


  1. void loop()
  2. {
  3.   float Value1 = 1500;
  4.   float Value2 = 0.51;
  5.  
  6.   HCMAX7219.Clear();
  7.  
  8.   HCMAX7219.print7Seg(Value1, 0, 8, 16);
  9.   HCMAX7219.print7Seg(Value2, 2, 8, 8);
  10.  
  11.   HCMAX7219.Refresh();  
  12.  
  13.   while(1);
  14. }
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Phil-S
Posts: 6
Joined: Wed Jan 30, 2019 10:17 am

Re: HCMAX7219 - LED driver library

Post by Phil-S » Thu Mar 07, 2019 11:33 am

Hello
Thanks for that
Notepad/Notepad++ advice noted.
Drivers set to 2 OK
Version 0.5 in use
Fudge comes from here
https://forum.arduino.cc/index.php?topic=500347.0

Do you have any thoughts on the 1*addr method? (in the fudge link). I'm not really up to speed on pointers and the like.
I will try your code out today.
I'm guessing a bit here, but I presume when you set the number of drives to 2, instead of 8 digits available, you now have 16 hence value 1 to 8,8 and value 2 to 8,16 - three drives would take you to 24.
Nick Gammon http://www.gammon.com.au/forum/?id=11516 has a nice item on MAx7219.
Guessing again, but if you wanted the values (dynamic) and some static text like kWh, you would essentially define the start point and the number of digits?
Surprised, but shouldn't be, just how much current is used . I am using a separate supply for the displays, but even with just one display, the USB supply (instead of Vin) was dropping to the point where the Nano was resetting.. A large electrolytic might help.

andrew
Site Admin
Posts: 865
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCMAX7219 - LED driver library

Post by andrew » Thu Mar 07, 2019 2:26 pm

Do you have any thoughts on the 1*addr method? (in the fudge link). I'm not really up to speed on pointers and the like.
That's not actually a pointer, or rather what a pointer in C++ is. He's just multiplying the value 8 held in the variable addr by 1 , 2, 3, 4 to get 8, 16, 24, 32 etc. These then set the print cursor position to the first (left most) digit of each module.

I'm guessing a bit here, but I presume when you set the number of drives to 2, instead of 8 digits available, you now have 16 hence value 1 to 8,8 and value 2 to 8,16 - three drives would take you to 24.
That's correct. The NUMBEROFDRIVERS value specifies how many MAX7219 ICs you're talking to. There is one in each module which in turn can drive eight 7 segment digits. So setting it to two will allow the library to talk to two modules each having 8 digits.

Guessing again, but if you wanted the values (dynamic) and some static text like kWh, you would essentially define the start point and the number of digits?
I would suggest playing around with the example in my previous post. Try changing some of the numbers to see what they do.

I am using a separate supply for the displays, but even with just one display, the USB supply (instead of Vin) was dropping to the point where the Nano was resetting.
That's odd, they can draw a lot of current but I certainly wouldn't expect a Nano to have trouble driving two displays. I've done this on numerous occasions without issue.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Phil-S
Posts: 6
Joined: Wed Jan 30, 2019 10:17 am

Re: HCMAX7219 - LED driver library

Post by Phil-S » Thu Mar 07, 2019 3:54 pm

Ref the current used, I wouldn't have thought it would be a problem.
It's clone Nano but a good one.
Maybe there is something else going on. Nothing on the Nano was getting warm.
I was counting up to 10million using a while loop in the main loop with a delay of 1-millisecond in the while loop.
I tried in the main setup and still had the problem.
With a meter on the 5-V line it was dropping down to between 3.9 and 3.5 volts.
Transferring the displays to a lab PSU seems to have fixed it.
It was a definite reset and the count never got past 5 digits. The count was uint32_t.
On the PSU, it ran for 24-hrs with no problem.
The MAX7219 ICs were quite warm.
I'm not worrying about it though.
Just out of interest, I did change all the headers.
To get over the non-2.54 spacing of the headers problem and perfboard, I fitted one right angle header at one end of the display and a low profile socket on the other. The RA pins go into a horizontal socket and the LP socket fits onto a male header.
The idea is that the RA socket allows quite a bit of adjustment and fits nicely.

Post Reply