Advertisement
CreativeCoPilot

Gameshow Buzzer

Aug 20th, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. /* sketch
  2. turn on a fan using a relay and a button
  3. */
  4. int pinButton1 = 2;
  5. int pinButton2 = 3;
  6.  
  7. int Relay1 = 6;
  8. int Relay2 = 7;
  9. int Relay3 = 8;
  10. int Relay4 = 9;
  11.  
  12. long debouncing_time = 500; //Debouncing Time in Milliseconds
  13. volatile unsigned long last_micros;
  14.  
  15. boolean button1 = false;
  16. boolean button2 = false;
  17.  
  18. void setup() {
  19. pinMode(pinButton1, INPUT);
  20. pinMode(pinButton2, INPUT);
  21. pinMode(Relay1, OUTPUT);
  22. pinMode(Relay2, OUTPUT);
  23. pinMode(Relay3, OUTPUT);
  24. pinMode(Relay4, OUTPUT);
  25. digitalWrite(Relay1, LOW);
  26. digitalWrite(Relay2, LOW);
  27. digitalWrite(Relay3, LOW);
  28. digitalWrite(Relay4, LOW);
  29. attachInterrupt(digitalPinToInterrupt(pinButton1), debounceInterruptButton1, RISING);
  30. attachInterrupt(digitalPinToInterrupt(pinButton2), debounceInterruptButton2, RISING);
  31.  
  32. }
  33.  
  34. void loop() {
  35. if(button1){
  36. button1 = false;
  37. button2 = false;
  38. digitalWrite(Relay1, HIGH);
  39. digitalWrite(Relay2, HIGH);
  40. delay(1000);
  41. digitalWrite(Relay1, LOW);
  42. digitalWrite(Relay2, LOW);
  43. }else if(button2){
  44. button1 = false;
  45. button2 = false;
  46. digitalWrite(Relay3, HIGH);
  47. digitalWrite(Relay4, HIGH);
  48. delay(1000);
  49. digitalWrite(Relay3, LOW);
  50. digitalWrite(Relay4, LOW);
  51. }
  52. }
  53.  
  54. void debounceInterruptButton1() {
  55. if((long)(micros() - last_micros) >= debouncing_time * 1000) {
  56. preButton1();
  57. last_micros = micros();
  58. }
  59. }
  60.  
  61. void debounceInterruptButton2() {
  62. if((long)(micros() - last_micros) >= debouncing_time * 1000) {
  63. preButton2();
  64. last_micros = micros();
  65. }
  66. }
  67.  
  68. void preButton1(){
  69. if(!button2)
  70. button1 = true;
  71. }
  72.  
  73. void preButton2(){
  74. if(!button1)
  75. button2 = true;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement