本网页已闲置超过3分钟,按键盘任意键或点击空白处,即可回到网页

带蜂鸣器的七段RNG
发布时间:2021-02-02
分享到:
带蜂鸣器的七段RNG
发布时间:2021-02-02
分享到:

按下按钮时,RNG会在蜂鸣器上随机播放音符。

硬件部件:

  • 7段LED显示屏,InfoVue    ×    1个    
  • Arduino UNO    ×    1个    
  • 蜂鸣器    ×    1个    
  • 触觉开关,顶部致动    ×    1个    

软件应用程序和在线服务:

  • Arduino IDE

七段显示模型:

  • 将Sev Seg的A钩到第二个引脚。
  • 将Sev Seg的B钩到第三针。
  • 继续直到将Sev Seg的G连接到第8针为止。
  • 使用电阻器将Sev Seg的“ Com”接地。
  • 将按钮连接到第9针。
  • 通过单独的电阻将按钮输出接地。
  • 将蜂鸣器连接到第10针。
  • 将蜂鸣器的接地连接到与Sev Seg连接的相同电阻。

码:
int a = 2, b = 3, c = 4, d = 5, e = 6, f = 7, g = 8, buzzerPin = 10, buttonPin = 9, rn; //Declares all the variables
void setup() { //Sets the pin
 pinMode(a, OUTPUT);
 pinMode(b, OUTPUT);
 pinMode(c, OUTPUT);
 pinMode(d, OUTPUT);
 pinMode(e, OUTPUT);
 pinMode(f, OUTPUT);
 pinMode(g, OUTPUT);
 pinMode(buzzerPin, OUTPUT);
 pinMode(buttonPin, INPUT);
 Serial.begin(9600);
}
void loop() {
 if(digitalRead(buttonPin) == HIGH){ //Generates random number, matches the number with the seven segment display function, and runs the shuffle loop 20 times when the button is pressed
   for(int ctr = 0; ctr < 20; ctr++){
     rn = random(10);
     if(rn == 0){
       zero();
       tone(buzzerPin, 523);
     }
     if(rn == 1){
       one();
       tone(buzzerPin, 587);
     }
     if(rn == 2){
       two();
       tone(buzzerPin, 659);
     }
     if(rn == 3){
       three();
       tone(buzzerPin, 698);
     }
     if(rn == 4){
       four();
       tone(buzzerPin, 784);
     }
     if(rn == 5){
       five();
       tone(buzzerPin, 880);
     }
     if(rn == 6){
       six();
       tone(buzzerPin, 988);
     }
     if(rn == 7){
       seven();
       tone(buzzerPin, 1047);
     }
     if(rn == 8){
       eight();
       tone(buzzerPin, 1175);
     }
     if(rn == 9){
       nine();
       tone(buzzerPin, 1319);
     }
     delay(75);
     noTone(buzzerPin);
   }
 }
}
void off() { //Turnes off the seven segmant display
 digitalWrite(a, LOW);
 digitalWrite(b, LOW);
 digitalWrite(c, LOW);
 digitalWrite(d, LOW);
 digitalWrite(e, LOW);
 digitalWrite(f, LOW);
 digitalWrite(g, LOW);
}
void zero() { //Displays a zero on the seven segment display
 digitalWrite(a, HIGH);
 digitalWrite(b, HIGH);
 digitalWrite(c, HIGH);
 digitalWrite(d, HIGH);
 digitalWrite(e, HIGH);
 digitalWrite(f, HIGH);
 digitalWrite(g, LOW);
}
void one() { //Displays a one on the seven segment display
 digitalWrite(a, LOW);
 digitalWrite(b, HIGH);
 digitalWrite(c, HIGH);
 digitalWrite(d, LOW);
 digitalWrite(e, LOW);
 digitalWrite(f, LOW);
 digitalWrite(g, LOW);
}
void two() { //Displays a two on the seven segment display
 digitalWrite(a, HIGH);
 digitalWrite(b, HIGH);
 digitalWrite(c, LOW);
 digitalWrite(d, HIGH);
 digitalWrite(e, HIGH);
 digitalWrite(f, LOW);
 digitalWrite(g, HIGH);
}
void three() { //Displays a three on the seven segment display
 digitalWrite(a, HIGH);
 digitalWrite(b, HIGH);
 digitalWrite(c, HIGH);
 digitalWrite(d, HIGH);
 digitalWrite(e, LOW);
 digitalWrite(f, LOW);
 digitalWrite(g, HIGH);
}
void four() { //Displays a four on the seven segment display
 digitalWrite(a, LOW);
 digitalWrite(b, HIGH);
 digitalWrite(c, HIGH);
 digitalWrite(d, LOW);
 digitalWrite(e, LOW);
 digitalWrite(f, HIGH);
 digitalWrite(g, HIGH);
}
void five() { //Displays a five on the seven segment display
 digitalWrite(a, HIGH);
 digitalWrite(b, LOW);
 digitalWrite(c, HIGH);
 digitalWrite(d, HIGH);
 digitalWrite(e, LOW);
 digitalWrite(f, HIGH);
 digitalWrite(g, HIGH);
}
void six() { //Displays a six on the seven segment display
 digitalWrite(a, HIGH);
 digitalWrite(b, LOW);
 digitalWrite(c, HIGH);
 digitalWrite(d, HIGH);
 digitalWrite(e, HIGH);
 digitalWrite(f, HIGH);
 digitalWrite(g, HIGH);
}
void seven() { //Displays a seven on the seven segment display
 digitalWrite(a, HIGH);
 digitalWrite(b, HIGH);
 digitalWrite(c, HIGH);
 digitalWrite(d, LOW);
 digitalWrite(e, LOW);
 digitalWrite(f, LOW);
 digitalWrite(g, LOW);
}
void eight() { //Displays an eight on the seven segment display
 digitalWrite(a, HIGH);
 digitalWrite(b, HIGH);
 digitalWrite(c, HIGH);
 digitalWrite(d, HIGH);
 digitalWrite(e, HIGH);
 digitalWrite(f, HIGH);
 digitalWrite(g, HIGH);
}
void nine() { //Displays a nine on the seven segment display
 digitalWrite(a, HIGH);
 digitalWrite(b, HIGH);
 digitalWrite(c, HIGH);
 digitalWrite(d, HIGH);
 digitalWrite(e, LOW);
 digitalWrite(f, HIGH);
 digitalWrite(g, HIGH);
}

 

加入微信技术交流群

技术交流,职业进阶

关注与非网服务号

获取电子工程师福利

加入电路城 QQ 交流群

与技术大牛交朋友

讨论