I'm stuck in ledControl and a Max7219 board. (Solved)

I salvaged the PCB shown below from an old water cooling system.

I had great fun hacking into it.
At first glance, it has four 7-segment numbers. But it really has five.
Segment "0" is used to control six LEDs....... That took a while to figure out!

Using the ledControl library and a (somewhat butchered) version of one of the demos, I can make it write a number of choice, in this case "-345"

#include "LedControl.h"

/*
 Now we need a LedControl to work with.
 ***** These pin numbers will probably not work with your hardware *****
 pin 12 is connected to the DataIn 
 pin 11 is connected to the CLK 
 pin 10 is connected to LOAD 
 We have only a single MAX72XX.
 */
LedControl lc=LedControl(12,11,10,1);

/* we always wait a bit between updates of the display */
unsigned long delaytime=1000;

void setup() {
  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,8);
  /* and clear the display */
  lc.clearDisplay(0);
}


void loop() {
    int ones;
    int tens;
    int hundreds;
    boolean negative;	
 int v;
 v=-345;


    if(v < -999 || v > 999) 
       return;
    if(v<0) {
        negative=true;
        v=v*-1;
    }
    ones=v%10;
    v=v/10;
    tens=v%10;
    v=v/10;
    hundreds=v;			
    if(negative) {
       //print character '-' in the leftmost column	
       lc.setChar(0,4,'-',false);
    }
    else {
       //print a blank in the sign column
       lc.setChar(0,4,' ',false);
    }
    //Now print the number digit by digit
    lc.setDigit(0,3,(byte)hundreds,false);
    lc.setDigit(0,2,(byte)tens,false);
    lc.setDigit(0,1,(byte)ones,false);
}

And now I'm stranded.
All my attempts of making a function to input a number (instead of writing it manually into the sketch) just ends up in an error when compiling.
I'm afraid that I've made some obvious and embarrassing failure.

4X7 segment display (Small).jpg

It would help if you tell us what the compile error is, and you put the actual code that is causing the error in the post. The current code does not appear to have any way of inputting the number, unless I am missing something.

marco_c:
It would help if you tell us what the compile error is, and you put the actual code that is causing the error in the post. The current code does not appear to have any way of inputting the number, unless I am missing something.

You are correct.
It is the one that was working. I inputted the number by writing it in the sketch (-345)
But whenever I tried to get the value for "v" from somewhere else, it would tell me that "v is not declared in this scope".

I'm at work now on another computer, and when I tried to add an analogRead, it compiled.

I'll try it tonight, and report back.

#include "LedControl.h"

/*
 Now we need a LedControl to work with.
 ***** These pin numbers will probably not work with your hardware *****
 pin 12 is connected to the DataIn 
 pin 11 is connected to the CLK 
 pin 10 is connected to LOAD 
 We have only a single MAX72XX.
 */
LedControl lc=LedControl(12,11,10,1);

/* we always wait a bit between updates of the display */
unsigned long delaytime=100;

void setup() {
  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,8);
  /* and clear the display */
  lc.clearDisplay(0);
}


void loop() {
    int ones;
    int tens;
    int hundreds;
    boolean negative;	
 int v;
   int sensorValue = analogRead(A0);  //read a sensor and tell me about it
 v = sensorValue/4;


    if(v < -999 || v > 999) 
       return;
    if(v<0) {
        negative=true;
        v=v*-1;
    }
    ones=v%10;
    v=v/10;
    tens=v%10;
    v=v/10;
    hundreds=v;			
    if(negative) {
       //print character '-' in the leftmost column	
       lc.setChar(0,4,'-',false);
    }
    else {
       //print a blank in the sign column
       lc.setChar(0,4,' ',false);
    }
    //Now print the number digit by digit
    lc.setDigit(0,3,(byte)hundreds,false);
    lc.setDigit(0,2,(byte)tens,false);
    lc.setDigit(0,1,(byte)ones,false);
}

It is not good, but it is working.

A potentiometer hooked up to 5V-A0-GND gives me values from -000 to -255.
(I have to re-read the code and find out where the "-" is coming from)

Use Serial.print() to see what the value v is. If it is not negative (and it should not be) then the fault is in your logic for how you determine and/or print '-'.

Serial.print() is your friend in this sort of debugging.

marco_c:
Use Serial.print() to see what the value v is. If it is not negative (and it should not be) then the fault is in your logic for how you determine and/or print '-'.

Serial.print() is your friend in this sort of debugging.

The value of v is good and positive, and is the same as printed on the display (except for the "-").

It seems that even though v>0
this part:

    if(v<0) {
        negative=true;
        v=v*-1;
    }

somehow triggers

    if(negative) {
       //print character '-' in the leftmost column	
       lc.setChar(0,4,'-',false);
    }

I can put other characters in the '-', and have them printed at position 4 in the display.

If I comment out this part:

  /*  if(negative) {
       //print character '-' in the leftmost column	
       lc.setChar(0,4,'-',false);
    }
    else {
       //print a blank in the sign column
       lc.setChar(0,4,' ',false);
    }
    */

The "-" is not shown.... as expected.
(but that is not really a satisfying solution to the problem)

Then negative must be set wrong. Try printing it out in a few places?

There is not much to this program, so print things as you go through and see where things don't make sense.

marco_c:
Then negative must be set wrong. Try printing it out in a few places?

There is not much to this program, so print things as you go through and see where things don't make sense.

I've tried, and everything is looking right all the way. I printed it a line at a time, and the values are positive, until the number has been taken apart in positive ones, tens and hundreds.... And out of the blue, it then puts a "-" in position 4.

(I must be overlooking something elemental)

Just to try something different, instead of setting the boolea, set the characetr to be displayed in a char variable and then use that char in the display? I can't see it either but sometimes just changin what you are doing can shed light on the problem.

    if (v<0)
 {
        c = '-';
        v = -v;
 }
else
    c = ' ';

...

       lc.setChar(0, 4, c, false);

The reason you get all negative numbers is that once you get a negative number you set negative=true and then never set it back to false.

From what I can see, at the end of loop() set negative back to false and it should work.

Every time you exit loop() and re-enter, the variables are recreated.

However, what does make some sense is that the variables will occupy the same location as there is nothing else happening between calls (ie, they are recreated in the same order). Unless the number is negative, the variable is actually not initialised to false and keeps wahtever the value was when it was created. Try changing the variable declaration to

 boolean negative = false;

marco_c:
Every time you exit loop() and re-enter, the variables are recreated.

However, what does make some sense is that the variables will occupy the same location as there is nothing else happening between calls (ie, they are recreated in the same order). Unless the number is negative, the variable is actually not initialised to false and keeps wahtever the value was when it was created. Try changing the variable declaration to

 boolean negative = false;

It worked!

Thanks to both of you!

And if I subtract 100 from the measured value of "v", I can pass through zero from both directions without problems.
:slight_smile:

I am still amazed at how the most obvious things take time to fix because they are hiding in plain sight!

I am doing someting similar with my arduino and in this code I don't see where the value is entered.

here you said you will do it but I don't see it in your final code...

could you please send the part where it does it???
THX in advance

Hi Darkshadow.

I made it work, but it is 8 years ago, and I have kind of forgotten how.

The oldest Arduino sketches i still have are from 2014.

Sorry.

ok no problem peter, I was just trying to hope that you could still have it
sorry for disturbing you...

No problem.

Have you checked the previous posts?
The answer could be in one of them.

yes i've checked them.
But it's not in here .
Sorry

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.