Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. #include<LiquidCrystal.h>
  2. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  3.  
  4. const int ButtonPin1 = 10;
  5. const int ButtonPin2 = 9;
  6.  
  7. int i = 0;
  8.  
  9. char Result;
  10.  
  11. #define inputCLK 6
  12. #define inputDT 7
  13.  
  14. int piezo = 8;
  15.  
  16. int notes[] = {261, 294, 329, 349, 392, 440, 493, 523};
  17. //order: c d e f g a b C
  18.  
  19. int counter = 0;
  20. int currentStateCLK;
  21. int previousStateCLK;
  22.  
  23. int B1State = 0;
  24. int B2State = 0;
  25.  
  26.  
  27. void setup() {
  28. pinMode (inputCLK,INPUT);
  29. pinMode (inputDT,INPUT);
  30. pinMode(ButtonPin1, INPUT);
  31. pinMode(ButtonPin2, INPUT);
  32.  
  33. Serial.begin (9600);
  34.  
  35. lcd.begin(16,2);
  36. lcd.print("position:" );
  37. }
  38. void loop() {
  39.  
  40. B1State = digitalRead(ButtonPin1);
  41. B2State = digitalRead(ButtonPin2);
  42.  
  43. if (B1State == HIGH) {
  44. randomNumber();
  45. }
  46. else {
  47. rotary();
  48. }
  49. if (B2State == HIGH) {
  50. check();
  51. }
  52. else {
  53. rotary();
  54. }
  55. }
  56.  
  57.  
  58.  
  59. void randomNumber() {
  60. i = random(0,7);
  61. tone(piezo, notes[i]);
  62. delay (1000);
  63. noTone(piezo);
  64. }
  65.  
  66.  
  67.  
  68. void check(){
  69. if (i = counter){
  70. Serial.println("Winner");
  71. }
  72. else {
  73. Serial.println("Loser");
  74. }
  75. }
  76.  
  77.  
  78. void rotary(){
  79. previousStateCLK = digitalRead(inputCLK);
  80. currentStateCLK = digitalRead(inputCLK); // Reads the "current" state of the inputCLK
  81. if (currentStateCLK != previousStateCLK){
  82. if (digitalRead(inputDT) != currentStateCLK) {
  83. counter ++;
  84.  
  85. } else {
  86. counter --;
  87.  
  88. }
  89. if (counter<0){
  90. counter=0;
  91. }
  92. else if (counter>9){
  93. counter=9;
  94. }
  95.  
  96.  
  97. if ((counter > 0) && (counter < 9)) {
  98. tone(piezo, notes[counter]);
  99. delay (1000);
  100. noTone(8);
  101. } else {
  102. noTone(8);
  103. }
  104.  
  105. // Serial.println(counter);
  106. previousStateCLK = currentStateCLK; // Updates the previous state of the inputCLK with the current state
  107. }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement