Pages: [1]   Go Down
Author Topic: Using 72xx LedControl with an existing sketch.  (Read 963 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to format an existing sketch (https://github.com/ethermania/MPCountdown) to drive a nine digit display by using the LedControl Library (http://playground.arduino.cc/Main/LedControl).

The main program sketch is at https://github.com/ethermania/MPCountdown/blob/master/firmware/masterDigit/masterDigit.ino and the other files included in the top link are header files for the 74hc595 display multiplexing. I am wishing to use the masterDigit.ino sketch to drive a nine digit display to do the same thing that the program already does but with the 7221 drivers. I am tight on space so the dual 7221 set with the Arduino makes for a much better solution than using one 74hc595 per digit. I already have a sketch made up for the 7221 LedControl library that I have been using the experiment with, but at this point I am just throwing stuff into it and hoping it works without understanding what is going on.

I have been trying to figure out how to split the sketch apart and use it with the LedControl sketch that I already have with absolutely no success. I am even more confused than I was when I first started trying. I am not that fluent in the programming language but I can usually understand the logic, as long as I know what the commands in the program are doing.

My best guess as to the goal that I am trying to achieve is to create an array somehow (which is already done in the MPCountdown sketch) and use it to assign a variable for each digit (DDD:HH:MM:SS) like and set up the time in such a way that it resembles:

//days
setDigit(1,2, 'variable from array here-days_hundred', false);
setDigit(1,1, 'variable from array here-days_dec', false);
setDigit(1,0, 'variable from array here-days_ones', false);
//hours
setDigit(0,0, 'variable from array here-hours_dec', false);
setDigit(0,1, 'variable from array here-hours_ones', false);
//minutes
setDigit(0,2, 'variable from array here-minutes_dec', false);
setDigit(0,3, 'variable from array here-minutes_ones', false);
//seconds
setDigit(0,4, 'variable from array here-seconds_dec', false);
setDigit(0,5, 'variable from array here-seconds_ones', false);

However, I have no idea how to achieve that with C/C++. Even more so, I don't know how to utilize the additional functions in the MPCountdown sketch to work with the LedControl library like to make it countdown, set the time, blink while setting the time, etc. I'm guessing that its a quick and easy replacement of a few variables here and there and changing a couple of loops, but again, I'm am lost.

I posted a similar thing on Project Guidance, but it never received any suggestions, so I'm guessing that I was in the wrong forum, and since this is mainly an LED driving issue, I would assume that this is the correct place for such a question. I still may stand corrected though.

Any suggestions would be appreciated, and I can provide any additional information if needed. Thank you.
Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 30
Posts: 1708
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

but at this point I am just throwing stuff into it and hoping it works without understanding what is going on.

I posted a similar thing on Project Guidance, but it never received any suggestions,

The lack of suggestions may be because there seems to be little interest in multiplexed LED displays.  This is understandable but I wanted just four 20mm high digits and the code below is what I have been working on. It might provide some direction.

Code:
/* Countdown FOUR digit 4x7
 pin 12 is connected to the MAX7219 pin 1
 pin 11 is connected to the CLK pin 13
 pin 10 is connected to LOAD pin 12
 1 as we are only using 1 MAX7219
*/
#include "LedControl.h" //  need the library
LedControl lc=LedControl(12,11,10,1); // lc is our object
static int num = 456;
int digit1, digit2, digit3, digit4;

void setup()
{
  // the zero refers to the MAX7219 number, it is zero for 1 chip
  lc.shutdown(0,false);// turn off power saving, enables display
  lc.setIntensity(0,4);// sets brightness (0~15 possible values)
  lc.clearDisplay(0);// clear screen
  Serial.begin(9600);
}

void loop()
{
  Serial.print (num);
  Serial.print("    ");
  if (num < 0)
    num = 1116;
 
  if (num > 999)
  {
    float result = num / 1000;  // = 3.456
 
    digit4 = result; //digit4 is int, so it's just thous  =3
    Serial.print("   digit 4 = "); 
    Serial.print(digit4);

    int remain = num%1000; // result is int so this gets the remainder = 456
    Serial.print("  num%1000  ");
    Serial.print(remain);  //456

    digit3=remain/100; //digit3 is int, so its just the huns = 4
    Serial.print("  digit Three = ");
    Serial.print(digit3);
   
    Serial.print("  num%100  ");
    remain = num%100; // result is float so this gets the remainder =56.00
    Serial.print(remain);
    Serial.print("  digit Two = ");
    digit2=remain/10; //digitTwo is int, so its just the tens = 5
    Serial.print(digit2);
   
    Serial.print("  digitOne = ");
    digit1 = remain % 10;  //this gets the remainder of 56.00/10 =6
    Serial.print(digit1);
   
    Serial.print("    ");
   
    Serial.print(digit4);
    Serial.print(digit3);
    Serial.print(digit2);
    Serial.println(digit1);


    Serial.println("");
  }

else if (num > 99)
  {
    float result = num / 100;  // = 4.56
    digit4=0; 
   
    Serial.print("   digit 3 = ");   
    digit3 = result; //digitThree is int, so it's just huns  =4
    Serial.print(digit3);
   
    Serial.print("  num%100  ");
    int remain = num%100; // result is float fo this gets the remainder =56.00
    Serial.print(remain);
    Serial.print("  digit Two = ");
    digit2=remain/10; //digitTwo is int, so its just the tens = 5
    Serial.print(digit2);
   
    Serial.print("  digitOne = ");
    digit1 = remain % 10;  //this gets the remainder of 56.00/10 =6
    Serial.print(digit1);
   
    Serial.print("    ");
   
    Serial.print(digit3);
    Serial.print(digit2);
    Serial.print(digit1);


    Serial.println("");
  }

  else if (num>9)
  {
    float result = num / 10;  //s/b 4.2 resulting in 4
    digit1 = num % 10;  //this gets the remainder of 42/10 or 2
    digit2 = result;
    digit3 = 0;
    digit4 = 0;   
  }

  else
  {
    digit4 = 0; 
    digit3 = 0;
    digit2 = 0;
    digit1 = num; 
  }
  lc.setDigit(0,0,digit1,false); 
  lc.setDigit(0,1,digit2,false);
  lc.setDigit(0,2,digit3,false);
  lc.setDigit(0,3,digit4,false);   
  delay(500);
  num --;//count down!!
}
Logged

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

I've noticed that there has been diminished interest in seven segment display, especially when dot matrix is easily done with the same hardware set. But my project does call for seven segments specifically because its supposed to be "old-school" looking. I have only see that one MPCountdown that uses a nine digit time display, most other projects out there only have 4 digits, which is understandable, because I see the practicality for a project having huge diminishing returns for anything keeping time over the period of hours, much less days.

Right now, my time function looks like:

Code:
#define secondsinaday 60*60*24
#define secondsinhour 60*60
#define secondsinminute 60
unsigned long sectime = 3600;   //just a filler number for testing, in the end, this will be a variable from an input

void displayDate(){
unsigned int days = sectime/secondsinaday;
sectime = sectime % secondsinaday;
unsigned int hours = sectime/secondsinhour;
sectime = sectime % secondsinhour;
unsigned int minutes = sectime/secondsinminute;
unsigned int seconds = sectime % secondsinminute;
}

Where it calculates the variables, days, hours, minutes and seconds from the total amount of seconds. My main problem here, I believe, is to somehow split these days, hours, minutes, seconds, variable into separate numbers equal to something like: days_hundred(hundreds number in days), days_dec(tens number in days), days_ones(ones number in days), hours_dec(tens number in hours), etc...
I believe this is done in a string, or array or something similar, but so far everything I have tried has been unsuccessful, and furthermore, if I were to be successful, it would be pure luck, because I am just guessing syntax at this point.

In the end, my time values would end up being something like yours:

  lc.setDigit(1,0,days_hundreds,false); 
  lc.setDigit(1,1,days_dec,false); 
  lc.setDigit(1,2,days_ones,false); 
  lc.setDigit(0,0,hours_dec,false); 
  lc.setDigit(0,1,hours_ones,false);
  lc.setDigit(0,2,minutes_dec,false);
  lc.setDigit(0,3,minutes_ones,false); 
  lc.setDigit(0,4,seconds_dec,false); 
  lc.setDigit(0,5,seconds_ones,false); 

And that is how it will ultimately display the time using the LedControl library after doing the calculations. However, the MPCountdown project that I found already includes all of the functions for setting the time and everything, so it would save me a lot of steps and headache if I were to get that to work with the LedControl library, and then just go through the code to figure out how it is all happening. Like I said, I understand the logic behind what I want to do, its just the syntax is the unfamiliar part. I've already learned so much about the Arduino platform, but right now its just not enough to not get brick-walled at a stopping point.
Logged

London
Offline Offline
Edison Member
*
Karma: 23
Posts: 1083
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My main problem here, I believe, is to somehow split these days, hours, minutes, seconds, variable into separate numbers equal to something like: days_hundred(hundreds number in days), days_dec(tens number in days), days_ones(ones number in days), hours_dec(tens number in hours), etc...
Code:
days_hundreds = days/100;
days_tens = (days %100)/10;
days_units = (days %100)%10;
Logged

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

Ohh of course, I had to complicate that whole thing and think that I needed strings and arrays, when simple math would do the trick.
Thank you for that, its really broken through the fog.

Code:

#include "LedControl.h"

/*
 pin 12 is DataIn
 pin 11 is CLK
 pin 10 is LOAD
 Two devices 7221
 */
LedControl lc=LedControl(12,11,10,2);


//buttons and inputs
const int button0 = A0;
const int button1 = A1;
const int button2 = A2;
const int button3 = A3;
const int button4 = A4;
const int pot1 = A5;
const int greenpin = 5;
const int redpin = 6;
const int yellowpin = 8;

unsigned long totalsectime = 25;

void setup() {

  pinMode(button0, INPUT);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
  pinMode(pot1, INPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(redpin, OUTPUT);
  pinMode(yellowpin, OUTPUT);

  int devices=lc.getDeviceCount();
  //init all devices in a loop
  for(int address=0;address<devices;address++) {
 
    lc.shutdown(address,false);
    /* Set brightness
    lc.setIntensity(0,15);
    lc.setIntensity(1,1);
    /* clear display */
    lc.clearDisplay(address);
    lc.setScanLimit(0, 6);
    lc.setScanLimit(1, 6);
  }
}

#define secondsinaday 86400  //((60*60)*24)
#define secondsinhour 3600  //(60*60)
#define secondsinminute 60

void displayDate(){

  for(totalsectime; totalsectime >= 0; totalsectime--){
    unsigned long sectime = totalsectime;
   
    unsigned long days = sectime/secondsinaday;
    sectime = sectime % secondsinaday;
    unsigned long hours = sectime/secondsinhour;
    sectime = sectime % secondsinhour;
    unsigned long minutes = sectime/secondsinminute;
    unsigned long seconds = sectime % secondsinminute;

    unsigned long days_hundreds = days/100;
    unsigned long days_tens = (days %100)/10;
    unsigned long days_units = (days %100)%10;

    unsigned long hours_tens = (hours %100)/10;
    unsigned long hours_units = (hours %100)%10;

    unsigned long minutes_tens = (minutes %100)/10;
    unsigned long minutes_units = (minutes %100)%10;

    unsigned long seconds_tens = (seconds %100)/10;
    unsigned long seconds_units = (seconds %100)%10;

    lc.setDigit(1,0,days_hundreds,false);
    lc.setDigit(1,1,days_tens,false);
    lc.setDigit(1,2,days_units,false);

    lc.setDigit(0,0,hours_tens,false);
    lc.setDigit(0,1,hours_units,false);
    lc.setDigit(0,2,minutes_tens,false);
    lc.setDigit(0,3,minutes_units,false);
    lc.setDigit(0,4,seconds_tens,false);
    lc.setDigit(0,5,seconds_units,false);
    delay(1000);
    if(sectime==16){
      displayWrap();
    }
   
    if(sectime==0){
      displayWrap();
      displayFade();
      genserOne();
      displayWrap();
      totalsectime = random(7852151); 
    }
  }
}

void loop() {

 displayDate();

}


And there is the program that works the way that I want it to so far in case anyone was curious about the whole picture. I left out a few functions that were just long and unimportant to the overall syntax. I'm sure there are a lot of newbie errors, but it works. Any suggestions are always appreciated, especially ones that help conserve memory.
Logged

London
Offline Offline
Edison Member
*
Karma: 23
Posts: 1083
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you for that, its really broken through the fog.
You're welcome.
Quote
Any suggestions are always appreciated, especially ones that help conserve memory.
I don't know much about LCD displays, but you could try:
Code:
    lc.setDigit(1,0,(days/100),false);  //hundreds of days
    lc.setDigit(1,1,((days %100)/10),false);  //tens of days
    lc.setDigit(1,2,((days %100)%10),false);  //units of days
    //etc.,etc.
and get rid of all the variables you don't need.
Logged

Pages: [1]   Go Up
Jump to: