/*
功能:一開機pin10顯示0hz,7段顯示器顯示0
再壓一下顯示250hz,7段顯示器顯示1
再壓一下顯示500hz,7段顯示器顯示2
再壓一下顯示1khz,7段顯示器顯示3
再壓一下顯示2khz,7段顯示器顯示4
再壓一下顯示4khz,7段顯示器顯示5
*/
unsigned int seg7_pin[]={2,3,4,5,6,7,8};
unsigned char seg7_table[]={~0xc0,~0xf9,~0xa4,~0xb0,~0x99,~0x92,~0x82,~0xf8,~0x80,~0x90};
int buzzer=10;
int speed0=0;//0hz 一開機
int speed1=2000;//0.25khz 1/(2000+2000)u
int speed2=1000;//0.5khz 1/(1000+1000)u
int speed3=500;//1khz 1/(500+500)u
int speed4=250;//2khz 1/(250+250)u
int speed5=125;//4khz 1/(125+125)u
int btn1=9;
int num_val;
int num=-1;
void setup()
{
Serial.begin(9600);
for(int i=0;i<7;i++)
{
pinMode(seg7_pin[i],OUTPUT);
}
for(int i=0;i<7;i++)
{
digitalWrite(seg7_pin[i],LOW);
}
pinMode(btn1,INPUT);
pinMode(buzzer,OUTPUT);
}
//--------------------------
void outport(byte data)
{
for(int j=0;j<7;j++)
{
if(data %2==1)
digitalWrite(seg7_pin[j],HIGH);
else
digitalWrite(seg7_pin[j],LOW);
data=data/2;
}
}
void loop()
{
num_val=digitalRead(btn1);
if(num_val==LOW)
{
delay(1000);
while(digitalRead(btn1)==LOW);
if(num==5)//012345,index執行到5(5 already執行過了)就歸0
num=0;
else
num++;
}
if(num==0)
{
digitalWrite(buzzer,HIGH);
delayMicroseconds(speed0);
digitalWrite(buzzer,LOW);
delayMicroseconds(speed0);
outport(seg7_table[0]);
Serial.println(speed0);
}
if(num==1)
{
digitalWrite(buzzer,HIGH);
delayMicroseconds(speed1);
digitalWrite(buzzer,LOW);
delayMicroseconds(speed1);
outport(seg7_table[1]);
Serial.println(speed1);
}
if(num==2)
{
digitalWrite(buzzer,HIGH);
delayMicroseconds(speed2);
digitalWrite(buzzer,LOW);
delayMicroseconds(speed2);
outport(seg7_table[2]);
Serial.println(speed2);
}
if(num==3)
{
digitalWrite(buzzer,HIGH);
delayMicroseconds(speed3);
digitalWrite(buzzer,LOW);
delayMicroseconds(speed3);
outport(seg7_table[3]);
Serial.println(speed3);
}
if(num==4)
{
digitalWrite(buzzer,HIGH);
delayMicroseconds(speed4);
digitalWrite(buzzer,LOW);
delayMicroseconds(speed4);
outport(seg7_table[4]);
Serial.println(speed4);
}
if(num==5)
{
digitalWrite(buzzer,HIGH);
delayMicroseconds(speed5);
digitalWrite(buzzer,LOW);
delayMicroseconds(speed5);
outport(seg7_table[5]);
Serial.println(speed5);
}
}
沒有留言:
張貼留言