Advertisement
Guest User

arduino

a guest
May 23rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     /*
  2.     * Ultrasonic Sensor HC-SR04 and Arduino Tutorial
  3.     *
  4.     * by Dejan Nedelkovski,
  5.     * www.HowToMechatronics.com
  6.     *
  7.     */
  8.  
  9. #define NOTE_E7  2637
  10. #define NOTE_C7  2093
  11. #define NOTE_G7  3136
  12. #define NOTE_G6  1568
  13.  
  14. #define melodyPin 3
  15. //Mario main theme melody
  16. int melody[] = {
  17.   NOTE_E7, NOTE_E7, 0, NOTE_E7,
  18.   0, NOTE_C7, NOTE_E7, 0,
  19.   NOTE_G7, 0, 0,  0,
  20.   NOTE_G6, 0, 0, 0,
  21.  
  22. };
  23. //Mario main them tempo
  24. int tempo[] = {
  25.   12, 12, 12, 12,
  26.   12, 12, 12, 12,
  27.   12, 12, 12, 12,
  28.   12, 12, 12, 12,
  29.  
  30. };
  31.  
  32.     // defines pins numbers
  33.     const int trigPin = 4;
  34.     const int echoPin = 5;
  35.  
  36.     const int trigPin2 = 9;
  37.     const int echoPin2 = 10;
  38.  
  39.     const int trigPin3 = 7;
  40.     const int echoPin3 = 6;
  41.  
  42.     int led = 13;           // the PWM pin the LED is attached to
  43.    
  44.     // defines variables
  45.     long duration;
  46.     long duration2;
  47.     long duration3;
  48.    
  49.     int distance2;
  50.     int distance3;
  51.     int distance;
  52.  
  53.     int scores=0;
  54.     int score1=0;
  55.     int score2=0;
  56.     int score3=0;
  57.  
  58.     int totalscores;
  59.  
  60.     void setup() {
  61.     pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  62.     pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  63.  
  64.     pinMode(trigPin2, OUTPUT); // Sets the trigPin as an Output
  65.     pinMode(echoPin2, INPUT); // Sets the echoPin as an Input
  66.  
  67.     pinMode(trigPin3, OUTPUT); // Sets the trigPin as an Output
  68.     pinMode(echoPin3, INPUT); // Sets the echoPin as an Input
  69.    
  70.     Serial.begin(9600); // Starts the serial communication
  71.    
  72.      pinMode(led, OUTPUT);
  73.      pinMode(3,OUTPUT);
  74.     }
  75.     void loop() {
  76.        sensor();
  77.        sensor2();
  78.        sensor3();
  79.    
  80.    
  81.     // Prints the distance on the Serial Monitor
  82. //Serial.print("Hole 1:");
  83. //Serial.print(distance);
  84. //Serial.print(" ");
  85. //Serial.print("Hole 2:");
  86. //Serial.print(distance2);
  87. //Serial.print(" ");
  88. //Serial.print("Hole 3:");
  89. //Serial.print(distance3);
  90. //Serial.print(" ");
  91. //Serial.println("");
  92.  
  93.  
  94.   if(distance>30&&distance<35){
  95.  
  96.      score1=50;
  97.     sing();
  98.   }
  99.   if(distance2>40&&distance2<45){
  100.  
  101.      score2=100;
  102.     sing();
  103.  
  104.   }
  105.     if(distance3>57&&distance3<65){
  106.      score3=150;
  107.     sing();
  108.   }
  109.  
  110.     totalscores=score1+score2+score3;
  111.  
  112.     Serial.print("total score is:");
  113.     Serial.print(totalscores);
  114.     Serial.println();
  115. }
  116.  
  117.  
  118. int song = 0;
  119.  
  120. void sensor() {
  121.  
  122. // Clears the trigPin
  123.  
  124.     digitalWrite(trigPin, LOW);
  125.     delayMicroseconds(2);
  126.    
  127.     // Sets the trigPin on HIGH state for 10 micro seconds
  128.  
  129.     digitalWrite(trigPin, HIGH);
  130.     delayMicroseconds(10);
  131.        
  132.  
  133.     digitalWrite(trigPin, LOW);
  134.    
  135.  
  136.     duration = pulseIn(echoPin, HIGH);
  137.    
  138.  
  139.     distance= duration*0.034/2;
  140.  
  141.  
  142. }
  143.  
  144. void sensor2() {
  145.  
  146. // Clears the trigPin
  147.     digitalWrite(trigPin2, LOW);
  148.     delayMicroseconds(2);
  149.    
  150.     // Sets the trigPin on HIGH state for 10 micro seconds
  151.     digitalWrite(trigPin2, HIGH);
  152.     delayMicroseconds(10);
  153.        
  154.     digitalWrite(trigPin2, LOW);
  155.  
  156.    
  157.     // Reads the echoPin, returns the sound wave travel time in microseconds
  158.     duration2 = pulseIn(echoPin2, HIGH);
  159.  
  160.    
  161.     // Calculating the distance
  162.     distance2= duration2*0.034/2;
  163.  
  164.  
  165.  
  166. }
  167.  
  168. void sensor3() {
  169.  
  170. // Clears the trigPin
  171.     digitalWrite(trigPin3, LOW);
  172.     delayMicroseconds(2);
  173.    
  174.     // Sets the trigPin on HIGH state for 10 micro seconds
  175.     digitalWrite(trigPin3, HIGH);
  176.  
  177.     delayMicroseconds(10);
  178.        
  179.     digitalWrite(trigPin3, LOW);
  180.  
  181.    
  182.     // Reads the echoPin, returns the sound wave travel time in microseconds
  183.     duration3 = pulseIn(echoPin3, HIGH);
  184.  
  185.    
  186.     // Calculating the distance
  187.     distance3= duration3*0.034/2;
  188.  
  189.  
  190. }
  191.  
  192.  
  193. void sing() {
  194.   // iterate over the notes of the melody:
  195.  
  196.     int size = sizeof(melody) / sizeof(int);
  197.     for (int thisNote = 0; thisNote < size; thisNote++) {
  198.  
  199.       // to calculate the note duration, take one second
  200.       // divided by the note type.
  201.       //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  202.       int noteDuration = 1000 / tempo[thisNote];
  203.  
  204.       buzz(melodyPin, melody[thisNote], noteDuration);
  205.  
  206.       // to distinguish the notes, set a minimum time between them.
  207.       // the note's duration + 30% seems to work well:
  208.       int pauseBetweenNotes = noteDuration * 1.30;
  209.       delay(pauseBetweenNotes);
  210.  
  211.       // stop the tone playing:
  212.       buzz(melodyPin, 0, noteDuration);
  213.  
  214.     }
  215.  
  216. }
  217.  
  218. void buzz(int targetPin, long frequency, long length) {
  219.   digitalWrite(13, HIGH);
  220.   long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
  221.   //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  222.   //// there are two phases to each cycle
  223.   long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
  224.   //// multiply frequency, which is really cycles per second, by the number of seconds to
  225.   //// get the total number of cycles to produce
  226.   for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
  227.     digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
  228.     delayMicroseconds(delayValue); // wait for the calculated delay value
  229.     digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
  230.     delayMicroseconds(delayValue); // wait again or the calculated delay value
  231.   }
  232.   digitalWrite(13, LOW);
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement