Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 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 == LOW) {
  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 > 0) && (counter < 9)) {
  60. tone(piezo, notes[counter - 1]);
  61. } else {
  62. noTone(8);
  63. }
  64.  
  65. Serial.print("Position: ");
  66. Serial.println(counter);
  67.  
  68. previousStateCLK = currentStateCLK; // Updates the previous state of the inputCLK with the current state
  69. }
  70. }
  71.  
  72. }
  73.  
  74.  
  75.  
  76.  
  77. void randomNumber() {
  78. int i = random(0, 7);
  79. tone(piezo, notes[i]);
  80. delay (1000);
  81. noTone(piezo);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement