---
# System prepended metadata

title: Lab2 7-segment LED display
tags: [微算機]

---

# Lab2 7-segment LED display

## Members:
411085031 劉又瑄
410985004 陳文彥
411085020 林漢昕

## Principle: 
Use 7 pin to control the number we play and use 4 pin to determine which light is under control.

## Program:
```c=
char col[] = {47, 49, 51, 53};
char row[] = {38, 40, 42, 44, 46, 48, 50, 52};
char TAB[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
               0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E};

void setup()
{
  for (int i = 0; i < 4; i++)
  {
    pinMode(col[i], OUTPUT);
    digitalWrite(col[i], HIGH);
  }
  for (int j = 0; j < 8; j++)
  {
    pinMode(row[j], OUTPUT);
    digitalWrite(row[j], HIGH);
  }
}

void light(int index, int num) {
  digitalWrite(col[index], LOW);
  for (int k = 0; k < 8 ; k++)
    digitalWrite(row[k], TAB[num] >> k & 0x01);
    
  delay(5);
  digitalWrite(col[index], HIGH);
}

void setNumber(int num) {
  light(3, num % 10);
  light(2, num / 10 % 10);
  light(1, num / 100 % 10);
  light(0, num / 1000);
}

int minSec = 0;
void loop()
{
  int minSec = millis() / 100;
  setNumber(minSec / 600 * 1000 + minSec % 600);
}

```

## Description

### ``setup()``
Set up the output pin.

### ``light(int index, int num)``
Set the i'th LED to one digit num.

### ``setNumber(int num)``
Set all of light to the four digits of num.

### ``loop()``
Use``millis()``to get the current time, convert it to a four digits num and call``setNumber(int num)``with it in avery loop. 

## Result

### [影片連結](https://drive.google.com/drive/u/0/folders/1n6hdeguwyIUa-wu3iFgC_ic0AQoZTTU9)

![](https://i.imgur.com/UrnxYsX.jpg =600x)


