Lesson 20--Microduino Two Digital Tube Countdown Display

From Microduino Wiki
Jump to: navigation, search
Language: English  • 中文

Objective

We have learned display the number statically, this lesson will teach you how to display number dynamically. Only need change the code, no hardware change.

Equipment


Schematic

第二十课-Microduino 2位数码管倒计时原理图.jpg

Pin Table

Microduino Pin Digital Tube Pin
D2 10(A)
D3 9(B)
D4 1(C)
D5 4(D)
D6 3(E)
D7 6(F)
D8 5(G)
9 2(DP)
D10 8(LED1)
D11 7(LED2)

Program

byte digit0 = 10;	//Ten digits Microduino pin
byte digit1 = 11;	//Single digit Microduino pin
byte dotPin = 2;	//Decimal point pin

byte sevenSegmentPins[] = {2,3,4,5,6,7,8};	//A,B,C,D,E,F,G Microduino pin
byte sevenSegment[10][7] =
{
  //a b c d e f g
  { 0,0,0,0,0,0,1 },  // = 0
  { 1,0,0,1,1,1,1 },  // = 1
  { 0,0,1,0,0,1,0 },  // = 2
  { 0,0,0,0,1,1,0 },  // = 3
  { 1,0,0,1,1,0,0 },  // = 4
  { 0,1,0,0,1,0,0 },  // = 5
  { 0,1,0,0,0,0,0 },  // = 6
  { 0,0,0,1,1,1,1 },  // = 7
  { 0,0,0,0,0,0,0 },  // = 8
  { 0,0,0,1,1,0,0 }   // = 9
};

void setup()
{
	//Initialize all lights up
   pinMode(dotPin, OUTPUT); //pin 9
   pinMode(digit0, OUTPUT); //pin 10
   pinMode(digit1, OUTPUT); //pin 11

  for(int i=0; i<7; i++)
  {
    pinMode(sevenSegmentPins[i], OUTPUT);
  }
  digitalWrite(dotPin, HIGH);
  digitalWrite(digit0, HIGH);
  digitalWrite(digit1, HIGH);
}

//Display number
void segmentWrite(byte digit)
{
  byte pin = 2;
  for (byte i=0; i<7; ++i)
  {
    digitalWrite(pin, sevenSegment[digit][i]);
      ++pin;
  }
}

void loop()
{

  for(int i=9;i>=0;i--) {// Countdown ten digits
    for(int j=9;j>=0;j--) {//Countdown to single digit
      
      unsigned long startTime = millis();
      for (unsigned long elapsed=0; elapsed < 600; elapsed = millis() - startTime) {//Display 600ms
          digitalWrite(digit0, LOW);   //Trun on ten digits tube
          segmentWrite(j);             //Display single tube
          delay(10);                  //Delay 10ms
          digitalWrite(digit0, HIGH);  //Turn on ten digits tube
          digitalWrite(digit1, LOW);   //Turn off single tube
          segmentWrite(i);             //Display ten digits 
          delay(10);                  //Delay 10ms
          digitalWrite(digit1, HIGH);  //Turn on single digit
      }
      
    }
  }

}

Debug

Step 1:Identify the digital tube's Pin

Step 2: Copy code to IDE and compile it

Step 3: Set up circuit, as follows:

第二十课-Microduino 2位数码管倒计时连接图.jpg

Step 4: Run program

Step 5: Observe the digital tube


Result

The number will countdown from 99 to 0, and then in circle.

Video

http://v.youku.com/v_show/id_XNjc4NjcxMzY0.html