Loading...
Pages: [1]   Go Down
Author Topic: Beginner 7 segment display  (Read 248 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am a beginner, but i have managed to develop a 2 digit seven segment display.
I am utilizing an external 12vdc power source, max7219, common anode digits, source driver and sink drivers.
I have been developing source code to scroll through 0 to 99. Unfortunately i go from 00 to 01 then i am out of the loop and nothing else happens. I have been through the arduino foundations, tutorials etc... . But I still can not figure out what I am doing wrong. Maybe somebody can lead me in the right direction to get this to work.
(I hope my code is posted correctly, my first time for posting).
I can set "v" to any value and it displays correctly.
Thank you in advance.
   
Code:
[quote]
#include [color=#006699]"LedControl.h"[/color]

[color=#CC6600]LedControl[/color] lc=[color=#CC6600]LedControl[/color](12,11,10,1);
[color=#CC6600]unsigned[/color] [color=#CC6600]long[/color] delaytime=200;[color=#7E7E7E]//  v*75[/color]
[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]() {
  [color=#7E7E7E]/*[/color]
[color=#7E7E7E]   The MAX72XX is in power-saving mode on startup,[/color]
[color=#7E7E7E]   we have to do a wakeup call[/color]
[color=#7E7E7E]   */[/color]
  lc.[color=#CC6600]shutdown[/color](0,[color=#CC6600]false[/color]);
  [color=#7E7E7E]/* Set the brightness to a medium values */[/color]
  lc.[color=#CC6600]setIntensity[/color](0,8);
  [color=#7E7E7E]/* and clear the display */[/color]
  lc.[color=#CC6600]clearDisplay[/color](0);
  [color=#CC6600]int[/color] v = 0;
}
[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color]()
  
[color=#7E7E7E]// function to send digits to 7 seg led[/color]

{
  [color=#CC6600]for[/color]([color=#CC6600]int[/color] v=0;v<99;v++)  {
  
  [color=#CC6600]int[/color] ones;
  [color=#CC6600]int[/color] tens;
  [color=#CC6600]boolean[/color] zero;
  
  [color=#CC6600]if[/color](v > 99)
    [color=#CC6600]return[/color];
  ones=v%10;
  v=v/10;
  tens=v%10;
  v=v/10;
  
  lc.[color=#CC6600]setDigit[/color](0,1,([color=#CC6600]byte[/color])tens,[color=#CC6600]false[/color]);
  lc.[color=#CC6600]setDigit[/color](0,0,([color=#CC6600]byte[/color])ones,[color=#CC6600]false[/color]);
  [color=#CC6600]delay[/color](3000);
}
}

[/quote]
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry,
updated code posted correctly.
Did I say I am new to this.

Code:
#include "LedControl.h"

LedControl lc=LedControl(12,11,10,1);
unsigned long delaytime=200;//  v*75
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);
  int v = 0;
}
void loop()
 
// function to send digits to 7 seg led

{
  for(int v=0;v<99;v++)  {
 
  int ones;
  int tens;
  boolean zero;
 
  if(v > 99)
    return;
  ones=v%10;
  v=v/10;
  tens=v%10;
  v=v/10;
 
  lc.setDigit(0,1,(byte)tens,false);
  lc.setDigit(0,0,(byte)ones,false);
  delay(3000);
}
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 315
Posts: 35519
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if(v > 99)
    return;
Is this necessary? In the for loop, v will never be more than 99.

Code:
  int ones;
  int tens;
  boolean zero;
 
  if(v > 99)
    return;
  ones=v%10;
  v=v/10;
  tens=v%10;
  v=v/10;
It's perfectly OK (preferred even) to declare and initialize the variable at the same time:
Code:
  byte ones=v%10;
  v=v/10;
  byte tens=v%10;
  v=v/10;

Reassigning a value to the loop index inside the loop is generally not a good idea. In this case, though, it is a particularly bad idea.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thankor the response,
I made changes based on recommendations.
The same result, my digits scroll from "00" to "01" then stops.
They stay nicely lit but stop scrolling.
I have attached my updated code.
Code:
#include "LedControl.h"

LedControl lc=LedControl(12,11,10,1);
unsigned long delaytime=200;//  v*75
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,15);
  /* and clear the display */
  lc.clearDisplay(0);
}
void loop() // function to send digits to 7 seg led
{
  for(int v=0;v<100;v++)  {
 
  byte ones=v%10;
  v=v/10;
  byte tens=v%10;
  v=v/10;
 
  lc.setDigit(0,1,(byte)tens,false);
  lc.setDigit(0,0,(byte)ones,false);
  delay(3000);
}
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 315
Posts: 35519
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Was there some part of
Quote
Reassigning a value to the loop index inside the loop is generally not a good idea. In this case, though, it is a particularly bad idea.
that wasn't clear? If so, I can explain it smaller words.

If not, why are you still doing it?
Logged

East Anglia (UK)
Offline Offline
Edison Member
*
Karma: 48
Posts: 1416
May all of your blinks be without delay
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  for(int v=0;v<100;v++)  {
    byte ones=v%10;
    v=v/10;
    byte tens=v%10;
    v=v/10;

    lc.setDigit(0,1,(byte)tens,false);
    lc.setDigit(0,0,(byte)ones,false);
    delay(3000);
  }
What's this all about ?
The loop variable is v and you presumably want it its value to go from 0 to 99 but you change its value inside the for loop, not just once but twice.  If you need the value of v/10 for some reason then put it in another variable to avoid messing with the operation of the for loop.
Logged

Pages: [1]   Go Up
Print
 
Jump to: