Advertisement
gnamp

HC-SR04 reverse engineer code1 Har.Sh

Apr 1st, 2013
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | None | 0 0
  1. #define trigPin 3
  2. #define echoPin 2
  3.  
  4. void setup(){
  5.  Serial.begin(9600);
  6.  pinMode(trigPin, OUTPUT);
  7.  pinMode(echoPin, INPUT);
  8. }
  9.  
  10. void loop(){
  11.  int duration, distance;
  12.  digitalWrite(trigPin, HIGH);
  13.  delayMicroseconds(1000);
  14.  digitalWrite(trigPin, LOW);
  15.  duration = pulseIn(echoPin, HIGH);
  16.  distance = (duration / 2) / 29.1;
  17.  Serial.print(distance);
  18.  Serial.println(" cm");
  19.  
  20. //
  21.  
  22.  int thisPitch = map(distance, 0, 200, 2093, 22);
  23.  int standardPitch = 1500;
  24.  
  25.  // noteList
  26.  
  27. int noteC0 = 262;
  28. int noteC0sharp = 278;
  29. int noteD0 = 294;
  30. int noteD0sharp = 312;
  31. int noteE0 = 330;
  32. int noteF0 = 349;
  33. int noteF0sharp = 370.5;
  34. int noteG0 = 392;
  35. int noteG0sharp = 416;
  36. int noteA1 = 440;
  37. int noteA1sharp = 467;
  38. int noteB1 = 494;
  39. int noteC1 = 523;
  40. int noteC1sharp = 555;
  41. int noteD1 = 587;
  42. int noteD1sharp = 623;
  43. int noteE1 = 659;
  44. int noteF1 = 698;
  45. int noteF1sharp = 741;
  46. int noteG1 = 784;
  47. int noteG1sharp = 832;
  48. int noteA2 = 880;
  49. int noteA2sharp = 934;
  50. int noteB2 = 988;
  51. int noteC2 = 1047;
  52.  
  53. //----------------
  54. //settings here
  55.  int firstPitch = 1500;
  56.  int secondPitch = 1000;
  57.  
  58.  int resetPitch = 500;
  59.  int beepCount = 0;
  60.  
  61.  if (distance > 8 && beepCount <= 3)
  62. {
  63.   //tune here:-
  64.   tone(4, noteE0); //1
  65.   delay(100);
  66.   noTone(4);
  67.   delay(100);
  68.   tone(4, noteF0); //2
  69.   delay(100);
  70.   noTone(4);
  71.   delay(100);
  72.   tone(4, noteF0); //3
  73.   delay(100);
  74.   noTone(4);
  75.   delay(100);
  76.   tone(4, noteF0); //4
  77.   delay(100);
  78.   noTone(4);
  79.   delay(100);
  80.    tone(4, noteF0); //5
  81.   delay(300);
  82.   noTone(4);
  83.   delay(100);
  84.  
  85.   tone(4, noteF0); //6
  86.   delay(100);
  87.   noTone(4);
  88.   delay(100);
  89.   tone(4, noteF0); //7
  90.   delay(100);
  91.   noTone(4);
  92.   delay(100);
  93.   tone(4, noteF0); //8
  94.   delay(100);
  95.   noTone(4);
  96.   delay(100);
  97.   tone(4, noteF0); //9
  98.   delay(150);
  99.   noTone(4);
  100.   delay(50);
  101.   tone(4, noteF0); //10 half note #1
  102.   delay(50);
  103.   noTone(4);
  104.   delay(50);
  105.   tone(4, noteF0); //11 half note #2
  106.   delay(50);
  107.   noTone(4);
  108.   delay(50);
  109.   tone(4, noteF0); //12
  110.   delay(100);
  111.   noTone(4);
  112.   delay(100);
  113.   tone(4, noteF0); //13
  114.   delay(100);
  115.   noTone(4);
  116.   delay(100);
  117.   tone(4, noteD0sharp); //14
  118.   delay(100);
  119.   noTone(4);
  120.   delay(100);
  121.   tone(4, noteE0); //15
  122.   delay(100);
  123.   noTone(4);
  124.   delay(100);
  125.   tone(4, noteF0); //16
  126.   delay(100);
  127.   noTone(4);
  128.   delay(300);
  129.  
  130.   //beepCount = beepCount + 1;
  131. }
  132. /* if (distance < 2 && beepCount <= 3)
  133. {
  134.   tone(4, resetPitch);
  135.   beepCount = 0;
  136. }
  137. */
  138.  
  139.  
  140.  noTone(4);
  141.  delay(1);
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement