Guest User

Untitled

a guest
Sep 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. int beepPin = 9;
  2.  
  3. int toneValue = 50;
  4.  
  5. int rotaryPinA = 1;
  6. int rotaryPinB = 2;
  7.  
  8. int rotaryReadA;
  9. int rotaryReadB;
  10.  
  11. int rotaryReadAPrev = 0;
  12.  
  13. unsigned long currentTime;
  14. unsigned long loopTime;
  15.  
  16. void setup()
  17. {
  18. Serial.begin(9600);
  19. pinMode(beepPin, OUTPUT);
  20.  
  21. pinMode(rotaryPinA, INPUT);
  22. pinMode(rotaryPinB, INPUT);
  23.  
  24. currentTime = millis();
  25. loopTime = currentTime;
  26. }
  27.  
  28. void loop()
  29. {
  30. currentTime = millis();
  31.  
  32. if (currentTime >= (loopTime + 5))
  33. {
  34.  
  35. rotaryReadA = digitalRead(rotaryPinA);
  36. rotaryReadB = digitalRead(rotaryPinB);
  37.  
  38. Serial.print("A");
  39. Serial.println(rotaryReadA);
  40. Serial.print("B");
  41. Serial.println(rotaryReadB);
  42.  
  43. if ((!rotaryReadA) && (rotaryReadAPrev))
  44. {
  45. if (rotaryReadB)
  46. {
  47. toneValue += 50;
  48. Serial.println("+");
  49. }
  50. else
  51. {
  52. toneValue -= 50;
  53. Serial.println("-");
  54. }
  55. }
  56.  
  57. rotaryReadAPrev = rotaryReadA;
  58. beep(toneValue, 100);
  59. loopTime = currentTime;
  60.  
  61. }
  62. }
  63.  
  64. void beep(int toneValue, int delayTime)
  65. {
  66. analogWrite(beepPin, toneValue);
  67. delay(delayTime);
  68.  
  69. analogWrite(beepPin, 0);
  70. delay(delayTime * 15);
  71. }
Add Comment
Please, Sign In to add comment