Arduino Tutorial 3

Arduino ProgrammingIn the last video I asked for requests so in this video I cover those requests. I’ll make both a Proximity Sensor and a Piano using an Arduino. Here is the kit I’m using.

We’ll learn about Ultrasonic Sensors, How Sound is Turned into a Current, Passive Buzzers and much more. All of the code is below.

If you like videos like this, consider donating $1, or simply turn off Ad Blocking software. Either allows me to continue making tutorials.

Code from the Video

// The Ultrasonic Sensor Module measures how far away an
// object is from it from 2 - 400cm. 

// You activate the trigger pin for 10μs it sends a high 
// frequency 8 cycle burst. It then turns on the echo
// receiver that measures distance by how quickly the signal
// bounces back.

// The frequency is sent by converting changes in a current which interacts with a magnet
// to cause the diaphragm, which is a flexible film, to vibrate creating sound waves.

// We then turn the sound waves bouncing back, back into a current we can measure 
// in the opposite way

// Create easy ways to identify what pins are being used for 
int triggerPin = 11;
int echoPin = 10;

long duration, distance;

void setup() {
  Serial.begin(9600);

  // Designate which pins are used for output and input reasons
  pinMode(echoPin, INPUT);
  pinMode(triggerPin, OUTPUT);

  // Prepare LED pins to provide an output voltage
  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop() {

  // As we loop the trigger pin that sends the 8 cycle frequency is turned on and off
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);

  // Get the travel time from playing the sound to receiving
  // it back 
  // pulseIn() turns a pin on and waits times the period from LOW to HIGH
  // and returns the period in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Convert that time to centimeters
  distance = MicrosecondsToCentimeters(duration);
  Serial.print("Distance : ");
  Serial.println(distance);

  // If less then 20 centimeters turn on 1 LED
  if(distance < 20){
    analogWrite(3, 91.5);
  } else {

    // If not true turn off LED
    analogWrite(3, 0);
  }

  // If less then 15 centimeters turn on next LED
  if(distance < 15){
    analogWrite(5, 91.5);
  } else {
    analogWrite(5, 0);
  }

  // If less then 10 centimeters turn on next LED
  if(distance < 10){
    analogWrite(6, 91.5);
  } else {
    analogWrite(6, 0);
  }
  
}


// The speed of sound is .0343 cm/μs
// We receive both the distance from the sensor and
// back again so we must divide by 2
long MicrosecondsToCentimeters(long duration){
  long d = (duration / 2) * .0343;
  return d;
}


// Designate that any place in the code were the variable is used we should instead
// replace with the integer value
// These are all the notes we want to play
#define NOTE_C5  523
#define NOTE_D5  587
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_G5  784
#define NOTE_A5  880
#define NOTE_B5  988
// Constants representing the ports associated with playing certain notes
const int C_5 = 7;
const int D_5 = 8;
const int E_5 = 9;
const int F_5 = 10;
const int G_5 = 11;
const int A_5 = 12;
const int B_5 = 13;

// The port the passive buzzer is attached to
// The passive buzzer receives an AC current and the frequency provided determines
// the tone it plays like we saw with the Ultrasonic sensor
const int Buzzer = 2;
 
void setup() {
  Serial.begin(9600);

  // Configure the pins connected to the switches as inputs
  pinMode(C_5, INPUT);
  pinMode(D_5, INPUT);
  pinMode(E_5, INPUT);
  pinMode(F_5, INPUT);
  pinMode(G_5, INPUT);
  pinMode(A_5, INPUT);
  pinMode(B_5, INPUT);

  // Setup the input pins to receive input
  digitalWrite(C_5, INPUT_PULLUP);
  digitalWrite(D_5, INPUT_PULLUP);
  digitalWrite(E_5, INPUT_PULLUP);
  digitalWrite(F_5, INPUT_PULLUP);
  digitalWrite(G_5, INPUT_PULLUP);
  digitalWrite(A_5, INPUT_PULLUP);
  digitalWrite(B_5, INPUT_PULLUP);
 
}
 
// When a switch is pressed generate the designated tone through the buzzer
// If no switch is pressed stop the generation of square waves
void loop() {  
  while(digitalRead(C_5) == LOW){
    tone(Buzzer, NOTE_C5);
  }
  while(digitalRead(D_5) == LOW){
    tone(Buzzer, NOTE_D5);
  }
  while(digitalRead(E_5) == LOW){
    tone(Buzzer, NOTE_E5);
  }
  while(digitalRead(F_5) == LOW){
    tone(Buzzer, NOTE_F5);
  }
  while(digitalRead(G_5) == LOW){
    tone(Buzzer, NOTE_G5);
  }
  while(digitalRead(A_5) == LOW){
    tone(Buzzer, NOTE_A5);
  }
  while(digitalRead(B_5) == LOW){
    tone(Buzzer, NOTE_B5);
  }

  noTone(Buzzer);
 }

Leave a Reply

Your email address will not be published. Required fields are marked *