Advertisement
Guest User

Untitled

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