Go Down

Topic: Timing of an led matrix (Read 667 times) previous topic - next topic

cabyambo

Dear arduino community i have a working code for an LED matrix but much of it was derived from help I recieved on the forums. I understand almost all of it accept for the part that keeps the letter displayed for the time specified. To be specific in the part of code I don't understand wouldn't start + duration always be greater than millis? This makes no sense to me. If anyone could explain it to me i would be very appreciative.

P.S. it's a 5X5 matrix

Specific piece of code  I don't understand

Code: [Select]
unsigned long start = millis(); //Begin timing the animation
  while(start + duration > millis()) //loop untill duration period. still dont understand this but oh well



Entire code
Code: [Select]
/* Hopefully the last LED matrix code I ever make

BY: Chris Yambo
*/

int CPR = 8;  // clock pin row
int DPR = 10; // data pin row
int CPC = 11; // clock pin column
int DPC = 12; // data pin column
int LP = 9;   // combined the latch pins so theres only one

byte RA[] = { B10000000, B01000000, B00100000, B00010000, B00001000,}; // way to address rows individually
byte letters[26][5] = {
{B10001000, B10001000, B11111000, B10001000, B11111000}, // A           
{B11111000, B10001000, B11110000, B10001000, B11111000}, // B
{B11111000, B10001000, B10000000, B10001000, B11111000}, // C
{B11110000, B10001000, B10001000, B10001000, B11110000}, // D
{B11111000, B10000000, B11110000, B10000000, B11111000}, // E
{B10000000, B10000000, B11110000, B10000000, B11111000}, // F
{B11111000, B10001000, B10011000, B10000000, B11111000}, // G
{B10001000, B10001000, B11111000, B10001000, B10001000}, // H
{B01000000, B01000000, B01000000, B01000000, B01000000}, // I
{B11111000, B10001000, B00001000, B00001000, B00001000}, // J
{B10001000, B10010000, B11100000, B10010000, B10001000}, // K
{B11111000, B10000000, B10000000, B10000000, B10000000}, // L
{B10101000, B10101000, B10101000, B10101000, B11011000}, // M
{B10001000, B10011000, B10101000, B11001000, B10001000}, // N
{B11111000, B10001000, B10001000, B10001000, B11111000}, // O
{B10000000, B10000000, B11111000, B10001000, B11111000}, // P
{B11111000, B10011000, B10001000, B10001000, B11111000}, // Q
{B10001000, B10010000, B11111000, B10001000, B11111000}, // R
{B11111000, B00001000, B11111000, B10000000, B11111000}, // S
{B00100000, B00100000, B00100000, B00100000, B11111000}, // T
{B11111000, B10001000, B10001000, B10001000, B10001000}, // U
{B00100000, B01010000, B10001000, B10001000, B10001000}, // V
{B11011000, B10101000, B10101000, B10101000, B10101000}, // W
{B10001000, B01010000, B00100000, B01010000, B10001000}, // X
{B11111000, B00001000, B11111000, B10001000, B10001000}, // Y
{B11111000, B01000000, B00100000, B00010000, B11111000}, // Z
};

byte space[1][5] = {
{B00000000, B00000000, B00000000, B00000000, B00000000}, // Space
};



void setup() {

  pinMode(CPR, OUTPUT);
  pinMode(DPR, OUTPUT); 
  pinMode(CPC, OUTPUT); // setting pins as outputs
  pinMode(DPC, OUTPUT);
  pinMode(LP, OUTPUT);
 
  for (int allOn = 0; allOn < 256; allOn++)
  {
    digitalWrite(LP, LOW);
    shiftOut(DPR, CPR, LSBFIRST, allOn); // count up on rows and columns in binary
    shiftOut(DPC, CPC, LSBFIRST, allOn); // cool effect to start it out every ime
    digitalWrite(LP, HIGH);
    delay(5);
  }
 


int letter;
int row;
int timePassed; // just setting some int's in case i need them
int delayTime = 1000;
boolean pixel;
Serial.begin(9600);

}

void loop(){
   show('H', 650);
   show('E', 650);
   show('L', 650);
   show('L', 650);
   show('O', 650);
   show(' ', 650);
   show('W', 650);
   show('O', 650);
   show('R', 650);
   show('L', 650);
   show('D', 650);
   show(' ', 650);





}


void show(char character, unsigned long duration){
  unsigned long start = millis(); //Begin timing the animation
  while(start + duration > millis()) //loop untill duration period. still dont understand this but oh well
  {
    if(character == 32) // if loop for letters
    {
      digitalWrite(LP, LOW); // PUT LATCH PIN LOW
      shiftOut(DPR, CPR, LSBFIRST, 0); // shift out ruw number 'J'
      shiftOut(DPC, CPC, LSBFIRST, 0); //SHIFT OUT COULMN 'J' FROM THE ARRAY OF WHATEVER CHARACTER WE CHOSE TO SHOW
      digitalWrite(LP, HIGH); // Put latch pin high displayig everything shifted in
      delay(2);
    }

    if(character == 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90) // if loop for letters
    {
      for(int j=0; j<5; j++) // starts a count from 0- 4 and makes it j
      {
        int y = character - 'A';
        digitalWrite(LP, LOW); // PUT LATCH PIN LOW
        shiftOut(DPR, CPR, LSBFIRST, RA[j]); // shift out ruw number 'J'
        shiftOut(DPC, CPC, LSBFIRST, letters[y][j]); //SHIFT OUT COULMN 'J' FROM THE ARRAY OF WHATEVER CHARACTER WE CHOSE TO SHOW
        digitalWrite(LP, HIGH); // Put latch pin high displayig everything shifted in
        delay(2);
      }
    }
   
  }
}





   
   
   
   

       
       
     
   
   

       
 
     
   

   
 
   

CrossRoads

From the Reference page on while():

while loops will loop continuously, and infinitely, until the expression inside the parenthesis, () becomes false. Something must change the tested variable, or the while loop will never exit. This could be in your code, such as an incremented variable, or an external condition, such as testing a sensor.

So when the function is called:

unsigned long start = millis(); //Begin timing the animation
  while(start + duration > millis()) //loop untill duration period.

start +duration will be greater than millis() because start was just given the value millis, and the stuff in the following { } brackets will run.
Then millis() will increase as time goes on, while start+duration is fixed;  and at some point start + duration will not be greater (i.e. the condition becomes false) and the while exits.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

cabyambo

Oh, now i understand it. I thought start was continually increasing the same as millis. I didn't realize it wasn't. Thank you so much

Go Up