Advertisement
Guest User

Untitled

a guest
May 17th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2.  
  3. #define vol A1 // INPUT / Reads Voltage to determine Volumne.
  4.  
  5. #define r1 A2 // OUTPUT / mode 1 indicator light
  6. #define r2 A3 // OUTPUT / mode 2 indicator light
  7. #define r3 A4 // OUTPUT / mode 3 indicator light
  8. #define r4 A5 // OUTPUT / mode 4 indicator light
  9.  
  10. const int delta = 4000; // change of range by mode.
  11. const int delta_min = 100; // change of interval inside mode oscillation.
  12.  
  13. const int buzzer = 3; // OUTPUT / voltage to speaker and volumne determination.
  14. const int SW_pin = 2; // INPUT / used for mute button
  15.  
  16. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  17.  
  18. enum keyPadButton{
  19. RIGHT = 0,
  20. LEFT = 505,
  21. UP = 144,
  22. DOWN = 329,
  23. SELECT = 742,
  24. NONE = 1023
  25. };
  26.  
  27. enum joystick{
  28. BUTTON_DOWN,
  29. BUTTON_UP,
  30. };
  31.  
  32. joystick state = BUTTON_UP;
  33.  
  34. void setup() {
  35. joySetup();
  36. keypadSetup();
  37. }
  38.  
  39. int tempo = 1000 / 4; // 4 notes a second
  40. int mode = 2; // starts in mode 2
  41. int c = 0; // concurrent counter of change in delta min
  42. int d = 1; // direction of counter
  43. int muted = 1; // boolean if muted
  44.  
  45. void loop() {
  46. noTone(buzzer); // turns off the tone at buzzer
  47.  
  48. processJoystick();
  49. processKeyPress(getKeyPressed());
  50. manageLights();
  51.  
  52. int lroof = delta * (mode + 1); // calculates modes roof value
  53. int lfloor = delta * mode; // calculates modes floor value
  54.  
  55. if (muted == 0) {
  56. tone(buzzer, lfloor + c); /
  57.  
  58. if (d == 1) {
  59. c += delta_min;
  60. if (c > (lroof - lfloor)) {
  61. d = -1;
  62. }
  63. } else {
  64. c -= delta_min;
  65. if (c < 0) {
  66. d = 1;
  67. }
  68. }
  69. } else {
  70. lcd.setCursor(4, 0);
  71. lcd.print("[MUTED]");
  72. }
  73.  
  74. lcd.setCursor(0, 1);
  75. lcd.print("FREQ=");
  76. lcd.print(lfloor + c);
  77. lcd.print(" HZ");
  78.  
  79. delay(tempo);
  80. lcd.clear();
  81. }
  82.  
  83. void manageLights() {
  84. analogWrite(r1, 0);
  85. analogWrite(r2, 0);
  86. analogWrite(r3, 0);
  87. analogWrite(r4, 0);
  88.  
  89. if (muted == 0) {
  90. if (mode == 1) {
  91. analogWrite(r1, 255);
  92. } else if (mode == 2) {
  93. analogWrite(r2, 255);
  94. } else if (mode == 3) {
  95. analogWrite(r3, 255);
  96. } else if (mode == 4) {
  97. analogWrite(r4, 255);
  98. }
  99. }
  100. }
  101.  
  102. void keypadSetup() {
  103. pinMode(r1, OUTPUT);
  104. pinMode(r2, OUTPUT);
  105. pinMode(r3, OUTPUT);
  106. pinMode(r4, OUTPUT);
  107. lcd.begin(16, 2);
  108. }
  109.  
  110. int prevB = 0; // Previous Button State
  111. joystick psw = BUTTON_UP; // Previous Stick State
  112.  
  113. void processKeyPress(int keyPressed) {
  114.  
  115. digitalWrite(buzzer, HIGH);
  116. lcd.setCursor(0, 0);
  117. lcd.print("Vol=");
  118. int v = analogRead(vol);
  119. v = (v / 10.24);
  120. lcd.print(v);
  121. lcd.print("%");
  122.  
  123. lcd.setCursor(0, 1);
  124.  
  125. if (psw == BUTTON_UP && state == BUTTON_DOWN) {
  126. if (muted == 1) {
  127. muted = 0;
  128. } else {
  129. muted = 1;
  130. }
  131. }else if (keyPressed > NONE-2 && keyPressed < NONE+2 ){
  132. } else if (keyPressed > SELECT-2 && keyPressed < SELECT+2 ) {
  133. } else if (keyPressed > RIGHT-2 && keyPressed < RIGHT+2 ) {
  134. mode = 4;
  135. } else if (keyPressed > LEFT-2 && keyPressed < LEFT+2 ) {
  136. mode = 2;
  137. } else if (keyPressed > UP-2 && keyPressed < UP+2 ) {
  138. mode = 3;
  139. } else if (keyPressed > DOWN-2 && keyPressed < DOWN+2 ) {
  140. mode = 1;
  141. }
  142.  
  143. prevB = keyPressed;
  144. psw = state;
  145. }
  146.  
  147. int getKeyPressed() {
  148. return analogRead(0);
  149. }
  150.  
  151. void joySetup() {
  152. pinMode(vol, INPUT);
  153. pinMode(SW_pin, INPUT);
  154. digitalWrite(SW_pin, HIGH);
  155. }
  156.  
  157. int getJoystickState() {
  158. int s = digitalRead(SW_pin);
  159. return s;
  160. }
  161.  
  162. void processJoystickState(int joyState) {
  163. if (joyState == 0) {
  164. state = BUTTON_DOWN;
  165. } else {
  166. state = BUTTON_UP;
  167. }
  168. }
  169.  
  170. void processJoystick() {
  171. processJoystickState(getJoystickState());
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement