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: 822
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!

Post Reply