Advertisement
Guest User

Untitled

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