Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.13 KB | None | 0 0
  1. const int pot = A5;
  2. const int piezo = 13;
  3. int led, val, value, tono;
  4. const int gap = 100;
  5. unsigned long previous = 0;
  6. bool flag = 0;
  7.  
  8. // TONES: note, period & frequency.
  9. int c = 3830; // 261 Hz
  10. int d = 3400; // 294 Hz
  11. int e = 3038; // 329 Hz
  12. int f = 2864; // 349 Hz
  13. int g = 2550; // 392 Hz
  14. int A = 2272; // 440 Hz
  15. int B = 2028; // 493 Hz
  16. int C = 1912; // 523 Hz
  17. int D = 1704; // 587 Hz
  18. int E = 1517; // 659 Hz
  19. int F = 1432; // 698 Hz
  20. int G = 1275; // 784 Hz
  21.  
  22. int note[] = {c,d,e,f,g,A,B,C,D,E,F,G};
  23.  
  24. void setup() {
  25.   for (led = 2; led <= 12; led++)
  26.     pinMode(led, OUTPUT);
  27.     pinMode(pot, INPUT);
  28.   Serial.begin(9600);
  29. }
  30.  
  31. void loop() {
  32.   val = analogRead(piezo);
  33.   value = map(val, 0, 1023, 1, 12);
  34.  
  35.     for (led = 2; led <= 12; led++) {
  36.       if ((led > value-2) && (led < value+2))
  37.         digitalWrite(led, HIGH);
  38.       else
  39.         digitalWrite(led, LOW);
  40.     }
  41.  
  42.   tono = map(val, 0, 1023, 11, 0);
  43.  
  44.   if (millis() - previous >= gap) {
  45.     previous = millis();
  46.     if (flag == 0) {
  47.       flag = 1;
  48.       tone(piezo, note[tono]);
  49.     }
  50.     else {
  51.       flag = 0;
  52.       noTone(piezo);
  53.     }
  54.   }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement