Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Ultrasonic Sensor HC-SR04 and Arduino Tutorial
- *
- * by Dejan Nedelkovski,
- * www.HowToMechatronics.com
- *
- */
- #define NOTE_E7 2637
- #define NOTE_C7 2093
- #define NOTE_G7 3136
- #define NOTE_G6 1568
- #define melodyPin 3
- //Mario main theme melody
- int melody[] = {
- NOTE_E7, NOTE_E7, 0, NOTE_E7,
- 0, NOTE_C7, NOTE_E7, 0,
- NOTE_G7, 0, 0, 0,
- NOTE_G6, 0, 0, 0,
- };
- //Mario main them tempo
- int tempo[] = {
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- 12, 12, 12, 12,
- };
- // defines pins numbers
- const int trigPin = 4;
- const int echoPin = 5;
- const int trigPin2 = 9;
- const int echoPin2 = 10;
- const int trigPin3 = 7;
- const int echoPin3 = 6;
- int led = 13; // the PWM pin the LED is attached to
- // defines variables
- long duration;
- long duration2;
- long duration3;
- int distance2;
- int distance3;
- int distance;
- int scores=0;
- int score1=0;
- int score2=0;
- int score3=0;
- int totalscores;
- void setup() {
- pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
- pinMode(echoPin, INPUT); // Sets the echoPin as an Input
- pinMode(trigPin2, OUTPUT); // Sets the trigPin as an Output
- pinMode(echoPin2, INPUT); // Sets the echoPin as an Input
- pinMode(trigPin3, OUTPUT); // Sets the trigPin as an Output
- pinMode(echoPin3, INPUT); // Sets the echoPin as an Input
- Serial.begin(9600); // Starts the serial communication
- pinMode(led, OUTPUT);
- pinMode(3,OUTPUT);
- }
- void loop() {
- sensor();
- sensor2();
- sensor3();
- // Prints the distance on the Serial Monitor
- //Serial.print("Hole 1:");
- //Serial.print(distance);
- //Serial.print(" ");
- //Serial.print("Hole 2:");
- //Serial.print(distance2);
- //Serial.print(" ");
- //Serial.print("Hole 3:");
- //Serial.print(distance3);
- //Serial.print(" ");
- //Serial.println("");
- if(distance>30&&distance<35){
- score1=50;
- sing();
- }
- if(distance2>40&&distance2<45){
- score2=100;
- sing();
- }
- if(distance3>57&&distance3<65){
- score3=150;
- sing();
- }
- totalscores=score1+score2+score3;
- Serial.print("total score is:");
- Serial.print(totalscores);
- Serial.println();
- }
- int song = 0;
- void sensor() {
- // Clears the trigPin
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- // Sets the trigPin on HIGH state for 10 micro seconds
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- distance= duration*0.034/2;
- }
- void sensor2() {
- // Clears the trigPin
- digitalWrite(trigPin2, LOW);
- delayMicroseconds(2);
- // Sets the trigPin on HIGH state for 10 micro seconds
- digitalWrite(trigPin2, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin2, LOW);
- // Reads the echoPin, returns the sound wave travel time in microseconds
- duration2 = pulseIn(echoPin2, HIGH);
- // Calculating the distance
- distance2= duration2*0.034/2;
- }
- void sensor3() {
- // Clears the trigPin
- digitalWrite(trigPin3, LOW);
- delayMicroseconds(2);
- // Sets the trigPin on HIGH state for 10 micro seconds
- digitalWrite(trigPin3, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin3, LOW);
- // Reads the echoPin, returns the sound wave travel time in microseconds
- duration3 = pulseIn(echoPin3, HIGH);
- // Calculating the distance
- distance3= duration3*0.034/2;
- }
- void sing() {
- // iterate over the notes of the melody:
- int size = sizeof(melody) / sizeof(int);
- for (int thisNote = 0; thisNote < size; thisNote++) {
- // to calculate the note duration, take one second
- // divided by the note type.
- //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
- int noteDuration = 1000 / tempo[thisNote];
- buzz(melodyPin, melody[thisNote], noteDuration);
- // to distinguish the notes, set a minimum time between them.
- // the note's duration + 30% seems to work well:
- int pauseBetweenNotes = noteDuration * 1.30;
- delay(pauseBetweenNotes);
- // stop the tone playing:
- buzz(melodyPin, 0, noteDuration);
- }
- }
- void buzz(int targetPin, long frequency, long length) {
- digitalWrite(13, HIGH);
- long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
- //// 1 second's worth of microseconds, divided by the frequency, then split in half since
- //// there are two phases to each cycle
- long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
- //// multiply frequency, which is really cycles per second, by the number of seconds to
- //// get the total number of cycles to produce
- for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
- digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
- delayMicroseconds(delayValue); // wait for the calculated delay value
- digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
- delayMicroseconds(delayValue); // wait again or the calculated delay value
- }
- digitalWrite(13, LOW);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement