Pages: [1]   Go Down
Author Topic: I'm stuck in ledControl and a Max7219 board. (Solved)  (Read 909 times)
0 Members and 1 Guest are viewing this topic.
Denmark
Offline Offline
Full Member
***
Karma: 8
Posts: 196
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

#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 (59.74 KB, 640x480 - viewed 19 times.)
« Last Edit: June 06, 2013, 11:37:31 am by Peter_I » Logged

Bob Pease: "My favorite programming language is ... solder."

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1179
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: June 02, 2013, 08:13:02 pm by marco_c » Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Denmark
Offline Offline
Full Member
***
Karma: 8
Posts: 196
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
}
Logged

Bob Pease: "My favorite programming language is ... solder."

Denmark
Offline Offline
Full Member
***
Karma: 8
Posts: 196
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Bob Pease: "My favorite programming language is ... solder."

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1179
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Denmark
Offline Offline
Full Member
***
Karma: 8
Posts: 196
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

somehow triggers
Code:
   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:
 /*  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)
Logged

Bob Pease: "My favorite programming language is ... solder."

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1179
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Denmark
Offline Offline
Full Member
***
Karma: 8
Posts: 196
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Bob Pease: "My favorite programming language is ... solder."

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1179
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
    if (v<0)
 {
        c = '-';
        v = -v;
 }
else
    c = ' ';

...

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

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 1
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1179
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
boolean negative = false;
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Denmark
Offline Offline
Full Member
***
Karma: 8
Posts: 196
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
 smiley
Logged

Bob Pease: "My favorite programming language is ... solder."

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1179
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Pages: [1]   Go Up
Jump to: