7段顯示器

資料來源

示範

電路圖

Code

/*This is the code to show numerals 0 to 9 on a 7 segment LED display
It represents seconds*/

//bits representing numerals 0-9
const byte numeral[11]= {
  B11111100, //0
  B01100000, //1
  B11011010, //2
  B11110010, //3
  B01100110, //4
  B10110110, //5
  B00111110, //6
  B11100000, //7
  B11111110, //8
  B11100110, //9
  B00000000, //shows nothing
};

//pins for each segment (a-g) on the 7 segment LED display with the corresponding arduino connection
const int segmentPins[8]= { 5,8,9,7,6,4,3,2 };

void setup(){
  for (int i=0; i < 8; i++) {
    pinMode(segmentPins[i], OUTPUT);
  }
}
void loop() {
  for (int i=0; i <=10; i++){
    showDigit(i);
    delay(1000);
  }
  delay(2000); //after LED segment shuts off, there is a 2-second delay
}

void showDigit (int number) {
  boolean isBitSet;
  for (int segment=1; segment < 8; segment++){
    isBitSet= bitRead(numeral[number], segment);
    digitalWrite(segmentPins[segment], isBitSet);
  }
}