Go Down

Topic: I'm stuck in ledControl and a Max7219 board. (Solved) (Read 1 time) previous topic - next topic

Peter_I

Jun 02, 2013, 10:03 pm Last Edit: Jun 06, 2013, 06:37 pm by Peter_I Reason: 1
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"

Code: [Select]


#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.
"Nothing is foolproof to a sufficiently talented fool"

marco_c

#1
Jun 03, 2013, 03:10 am Last Edit: Jun 03, 2013, 03:13 am by marco_c Reason: 1
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.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Peter_I


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.
Code: [Select]
#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);
}
"Nothing is foolproof to a sufficiently talented fool"

Peter_I

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)
"Nothing is foolproof to a sufficiently talented fool"

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.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Peter_I


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:
Code: [Select]
   if(v<0) {
       negative=true;
       v=v*-1;
   }


somehow triggers
Code: [Select]
   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:
Code: [Select]
 /*  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)
"Nothing is foolproof to a sufficiently talented fool"

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.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Peter_I


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)
"Nothing is foolproof to a sufficiently talented fool"

marco_c

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.

Code: [Select]
    if (v<0)
{
        c = '-';
        v = -v;
}
else
    c = ' ';

...

       lc.setChar(0, 4, c, false);
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

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.

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

Code: [Select]
boolean negative = false;
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Peter_I


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

Code: [Select]
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.
:)
"Nothing is foolproof to a sufficiently talented fool"

marco_c

I am still amazed at how the most obvious things take time to fix because they are hiding in plain sight!
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Go Up