//a,b,c,d,e,f,g
byte segValue[10][7] = {
{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,0,1,0,0} //9
};
const int buttonPin = 10;
const int buzzerPin = 11;
const int ledPin = 12;
byte segPin[8]={2,3,4,5,6,7,8,9}; //{a,b,c,d,e,f,g,dp}
byte digitPin[2] = {A0,A1}; //segment
void setup() {
pinMode(buttonPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
for(int i=0;i<10;i++){
pinMode(segPin[i], OUTPUT);
}
pinMode(digitPin[0], OUTPUT);
pinMode(digitPin[1], OUTPUT);
digitalWrite(digitPin[0], LOW);
digitalWrite(digitPin[1], LOW);
}
void loop()
{
digitalWrite(ledPin, HIGH);
if (digitalRead(buttonPin)) {
for (int i = 0; i<100; i++)
{
display_N(i);
delay(1);
if (i==30) {
tone(buzzerPin, 440, 1500);
digitalWrite(ledPin, LOW);
delay(1500);
break;
}
}
}
}
void display_N(int num)
{
int hex = num % 10;
int dec = (num % 100) / 10;
for(int i=0; i<100; i++)
{
segOutput(1, hex, 1);
segOutput(0, dec, 1);
delay(5);
}
}
//LED
void segClear(){
for(int i=0;i<8;i++){
digitalWrite(segPin[i], HIGH);
}
}
//LED
void segOutput(int d, int Number, int dp){
segClear();
digitalWrite(digitPin[d], HIGH);
for(int i=0;i<7;i++){
digitalWrite(segPin[i], segValue[Number][i]);
}
digitalWrite(segPin[7], dp);
delayMicroseconds(1000);
digitalWrite(digitPin[d], LOW);
}