硬件图:
程序调试图片:
Arduino源代码:
const int echopin = 3; // echo接3端口
const int trigpin = 2; // trig接2端口
const int led = 13;
//const int led = A1;
const int DIG1 = 4;
const int DIG2 = 12;
//A = 5;B = 6;C = 7;D = 8;E = 9;F = 10;G = 11;
byte SEG7[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
};
void setup()
{
Serial.begin(9600);
pinMode(echopin, INPUT); //设定echo为输入模式
pinMode(trigpin, OUTPUT); //设定trig为输出模式
for (int i = 4; i < 13; i++)
pinMode(i, OUTPUT);
pinMode(led, OUTPUT);
}
void loop()
{
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW); //发一个10ms的高脉冲去触发TrigPin
int pin=5;
int distance = pulseIn(echopin, HIGH); //接收高电平时间
distance = distance / 58.0; //计算距离
Serial.print(distance); //输出距离
Serial.println("cm"); //输出单位
if (distance < 100) {
int shi = distance / 10;
int ge = distance % 10;
//ge
digitalWrite(DIG2, LOW);
digitalWrite(DIG1, HIGH);
if (ge == 0) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[0][s]);
pin++;
}
} else if (ge == 1) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[1][s]);
pin++;
}
} else if (ge == 2) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[2][s]);
pin++;
}
} else if (ge == 3) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[3][s]);
pin++;
}
} else if (ge == 4) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[4][s]);
pin++;
}
} else if (ge == 5) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[5][s]);
pin++;
}
} else if (ge == 6) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[6][s]);
pin++;
}
} else if (ge == 7) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[7][s]);
pin++;
}
} else if (ge == 8) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[8][s]);
pin++;
}
} else if (ge == 9) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[9][s]);
pin++;
}
}
delay(10);
//shi
digitalWrite(DIG1, LOW);
digitalWrite(DIG2, HIGH);
if (shi == 0) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[0][s]);
pin++;
}
} else if (shi == 1) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[1][s]);
pin++;
}
} else if (shi == 2) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[2][s]);
pin++;
}
} else if (shi == 3) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[3][s]);
pin++;
}
} else if (shi == 4) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[4][s]);
pin++;
}
} else if (shi == 5) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[5][s]);
pin++;
}
} else if (shi == 6) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[6][s]);
pin++;
}
} else if (shi == 7) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[7][s]);
pin++;
}
} else if (shi == 8) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[8][s]);
pin++;
}
} else if (shi == 9) {
pin = 5;
for (int s = 0; s < 7; s++) {
digitalWrite(pin, SEG7[9][s]);
pin++;
}
}
} else {
digitalWrite(DIG1, HIGH);
digitalWrite(DIG2, HIGH);
for (int s = 0; s < 6; s++) {
digitalWrite(pin, HIGH);
pin++;
}
digitalWrite(pin, LOW);
}
//距离小于10cm时,led写入高电平
if (distance <= 20)
{
//digitalWrite(led, HIGH);
analogWrite(A1, 255);
//tone(led,1000,1000);
//delay(15);
tone(led,2000,100);
//delay(15);
//noTone(led);
} else {
analogWrite(A1, 0);
//digitalWrite(led, LOW);
}
delay(15); //循环间隔60uS
}