You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
1.8KB

  1. /*
  2. *@author James McKenzie
  3. *@lang C89 Based
  4. *@contact lunarised@outlook.com
  5. */
  6. int mode;
  7. void setup() {
  8. pinMode(6, INPUT_PULLUP);
  9. pinMode (13, OUTPUT);
  10. pinMode(8, OUTPUT);
  11. pinMode (7, INPUT_PULLUP);
  12. Serial.begin (9600); //set up a serial connection with the computer
  13. pinMode(11, OUTPUT); //the trigger pin
  14. pinMode(12, INPUT); //echo pin
  15. }
  16. void loop() {
  17. if (digitalRead(6) ==HIGH){
  18. mode = 0;
  19. }else{
  20. mode = 1;
  21. }
  22. float note = 0;
  23. if (digitalRead(7) == LOW){
  24. if (mode == 0){
  25. note = tune(getNote());
  26. }
  27. else{
  28. note = getNote();
  29. }
  30. digitalWrite(13, HIGH);
  31. Serial.print(note); //print units after the distance
  32. Serial.println(" hz"); //print units after the distance
  33. tone(8, note);
  34. }
  35. else{
  36. noTone(8);
  37. }
  38. }
  39. float getNote()
  40. {
  41. float echoTime;
  42. float calculatedDistance;
  43. digitalWrite(11, HIGH);
  44. delayMicroseconds(20);
  45. digitalWrite(11, LOW);
  46. echoTime = pulseIn(12, HIGH);
  47. calculatedDistance = echoTime / 8;
  48. calculatedDistance += 110;
  49. if (calculatedDistance > 1760){ //git
  50. calculatedDistance = 1760;
  51. }
  52. return calculatedDistance;
  53. }
  54. float tune(float toTune){
  55. if (toTune> 523.75){ //C5
  56. return 523.75;
  57. }
  58. if (toTune> 493.88){ //B4
  59. return 493.88;
  60. }
  61. if (toTune> 440){ //A4
  62. return 440;
  63. }
  64. if (toTune> 392){ //G4
  65. return 392;
  66. }
  67. if (toTune> 349.23){ //F4
  68. return 349.23;
  69. }
  70. if (toTune> 329.63){ //E5
  71. return 329.63;
  72. }
  73. if (toTune> 293.66){ //D4
  74. return 293.66;
  75. }
  76. if (toTune> 261.63){ //C4
  77. return 261.63;
  78. }
  79. if (toTune> 246.94){ //B3
  80. return 246.94;
  81. }
  82. if (toTune> 220){ //A3
  83. return 220;
  84. }
  85. if (toTune> 196){ //G3
  86. return 196;
  87. }
  88. if (toTune> 174.61){ //F3
  89. return 174.61;
  90. }
  91. }