Advertisement
CaptainSpaceCat

PotStep Tone Control

Mar 28th, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #define BUT 0
  2. #define POT A0
  3. #define BUZZ 10
  4.  
  5. #define UP 5
  6. #define DOWN 4
  7.  
  8. void setup() {
  9.   pinMode(BUT, INPUT_PULLUP);
  10.   pinMode(UP, INPUT_PULLUP);
  11.   pinMode(DOWN, INPUT_PULLUP);
  12.   pinMode(POT, INPUT);
  13.   pinMode(BUZZ, OUTPUT);
  14.   //Serial.begin(9600);
  15. }
  16.  
  17. boolean state = true;
  18. boolean uState = true;
  19. boolean dState = true;
  20.  
  21. boolean bTone = false;
  22.  
  23. int steps = 0;
  24.  
  25. void loop() {
  26.   detectButtons();
  27.   steps = limit(steps, 0, 20);  
  28.   int hz = map(analogRead(POT), 0, 1023, 400, 800);
  29.   hz *= pow(2, steps/12.0);
  30.  
  31.   if (bTone) {
  32.     tone(BUZZ, hz);
  33.   } else {
  34.     noTone(BUZZ);
  35.   }
  36.  
  37.   //Serial.println(steps);
  38. }
  39.  
  40. void detectButtons() {
  41.   if ((digitalRead(BUT) == LOW) && state) {
  42.     bTone = !bTone;
  43.     state = false;
  44.   } else if (digitalRead(BUT) == HIGH) {
  45.     state = true;
  46.   }
  47.  
  48.   if ((digitalRead(UP) == LOW) && uState) {
  49.     steps++;
  50.     uState = false;
  51.   } else if (digitalRead(UP) == HIGH) {
  52.     uState = true;
  53.   }
  54.  
  55.   if ((digitalRead(DOWN) == LOW) && dState) {
  56.     steps--;
  57.     dState = false;
  58.   } else if (digitalRead(DOWN) == HIGH) {
  59.     dState = true;
  60.   }
  61. }
  62.  
  63. int limit(int a, int _min, int _max) {
  64.   a = max(_min, a);
  65.   a = min(a, _max);
  66.   return a;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement