Facebook
From Gamboge Moth, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 569
  1. /**********************************
  2.  * name:Building a Quiz Buzzer System
  3.  * function: first press button 4 to start. If you press button 1 first, you will see the corresponding LED light up and the buzzer will beep. Then press button 4 again to reset before you press other buttons.
  4.  **********************************/
  5.  
  6.  
  7. #define button1 2  //the number of the button 1
  8. #define button2 3  //button2 attach to
  9. #define button3 4  //button3 attach to
  10. #define button4 9  //button4 attach to
  11. #define buzzerPin 5  //the buzzer attach to    
  12. #define LED1 6  //LED 1attach to      
  13. #define LED2 7  //LED2attach to    
  14. #define LED3 8  //LED3 attach to
  15. #define LED4 10  //LED4 attach to
  16. #define uint8 unsigned char
  17. uint8 flag = 0;  //used to indicate the state of button4 key
  18. uint8 b1State,b2State,b3State,b4State = 0;
  19.  
  20. void setup()
  21. {
  22.   //initialize buzzer,LED1, LED2, LED3 and LED4 as output
  23.   pinMode(buzzerPin, OUTPUT);    
  24.   pinMode(LED1, OUTPUT);
  25.   pinMode(LED2, OUTPUT);    
  26.   pinMode(LED3, OUTPUT);
  27.   pinMode(LED4, OUTPUT);
  28.   //initialize button1,button2 andbutton3 as input,combined with pullup
  29.   pinMode(button1, INPUT_PULLUP);
  30.   pinMode(button2, INPUT_PULLUP);
  31.   pinMode(button3, INPUT_PULLUP);    
  32.   pinMode(button4, INPUT_PULLUP);
  33.   //turn all the led off
  34.   digitalWrite(LED1, LOW);
  35.   digitalWrite(LED2, LOW);  
  36.   digitalWrite(LED3, LOW);
  37.   digitalWrite(LED4, LOW);
  38. }
  39. void loop()
  40. {
  41.   //turn all the led off
  42.   digitalWrite(LED1, LOW);
  43.   digitalWrite(LED2, LOW);  
  44.   digitalWrite(LED3, LOW);  
  45.   digitalWrite(LED4, LOW);
  46.   //read the state of the button4
  47.   b4State = digitalRead(button4);
  48.   //when button4 pressed
  49.   if(b4State == 0)
  50.   {
  51.     if(b4State == 0)  //confirm that the button4 is pressed
  52.     {
  53.       flag = 1;  //if so,flag is 1
  54.       digitalWrite(LED4, HIGH);  //turn the host LED on
  55.       delay(200);  
  56.     }
  57.   }
  58.   if(1 == flag)
  59.   {
  60.     //read the state of the button of buttons
  61.     b1State = digitalRead(button1);  
  62.     b2State = digitalRead(button2);
  63.     b3State = digitalRead(button3);
  64.     //If the button1 press the first
  65.     if(b1State == 0)
  66.     {
  67.       flag = 0;
  68.       digitalWrite(LED4, LOW);
  69.       Alarm();  //buzzer sound
  70.       digitalWrite(LED1,HIGH);  //turn the LED1 on only
  71.       digitalWrite(LED2,LOW);
  72.       digitalWrite(LED3,LOW);
  73.       while(digitalRead(button4));  //detect the button4,if pressed,out of the while loop
  74.     }
  75.     //If the button2 press the first
  76.     if(b2State == 0)
  77.     {
  78.       flag = 0;
  79.       digitalWrite(LED4, LOW);
  80.       Alarm();
  81.       digitalWrite(LED1,LOW);
  82.       digitalWrite(LED2,HIGH);
  83.       digitalWrite(LED3,LOW);
  84.       while(digitalRead(button4));
  85.     }
  86.     //If the button3 press the first
  87.     if(b3State == 0)
  88.     {
  89.       flag = 0;
  90.       digitalWrite(LED4, LOW);
  91.       Alarm();
  92.       digitalWrite(LED1,LOW);
  93.       digitalWrite(LED2,LOW);
  94.       digitalWrite(LED3,HIGH);
  95.       while(digitalRead(button4));
  96.     }
  97.   }
  98. }
  99. //buzzer sound
  100. void Alarm()        
  101. {
  102.   for(int i=0;i<100;i++){
  103.     digitalWrite(buzzerPin,HIGH); //the buzzer sound
  104.     delay(2);
  105.     digitalWrite(buzzerPin,LOW);  //without sound
  106.     delay(2);                     //when delay time changed,the frequency changed
  107.   }
  108. }
  109.  
  110.