Arduino Sketch Sound Demo

This sketch is used by Exercise: Multichannel Bipolar Transistor Driver.

Full Source Code

The full code is all in one file SoundDemo.ino.

// SoundDemo - generate sound using a speaker and pager motor
//
// Copyright (c) 2016, Garth Zeglin.  All rights reserved. Licensed under the
// terms of the BSD 3-clause license as included in LICENSE.
//
// This program assumes that:
//
//  1. A speaker is connected to pin 5 via a suitable driver.
//  2. A vibratory motor is connected to pin 6 via a suitable driver.
//  3. The serial console on the Arduino IDE is set to 9600 baud communications speed.

// ================================================================================
// Define constant values.

// The wiring assignment.
const int SPEAKER_PIN = 5;
const int MOTOR_PIN = 6;

// ================================================================================
// Configure the hardware once after booting up.  This runs once after pressing
// reset or powering up the board.

void setup()
{
  // Initialize the serial UART at 9600 bits per second.
  Serial.begin(9600);

  // Initialize the outputs.
  pinMode(SPEAKER_PIN, OUTPUT);
  digitalWrite(SPEAKER_PIN, LOW);
  pinMode(MOTOR_PIN, OUTPUT);
  digitalWrite(MOTOR_PIN, LOW);
}
// ================================================================================
// Run one iteration of the main event loop.  The Arduino system will call this
// function over and over forever.
void loop()
{
  // play a siren tone
  for(int i = 0; i < 4; i++) {
    tone(SPEAKER_PIN, 622);
    delay(500);
    tone(SPEAKER_PIN, 440);
    delay(500);
  }
  noTone(SPEAKER_PIN);
  
  // buzz a simple rhythm
  for(int i = 0; i < 4; i++) {
    digitalWrite(MOTOR_PIN, HIGH);
    delay(450);
    digitalWrite(MOTOR_PIN, LOW);
    delay(50);
    digitalWrite(MOTOR_PIN, HIGH);
    delay(450);
    digitalWrite(MOTOR_PIN, LOW);
    delay(50);
    digitalWrite(MOTOR_PIN, HIGH);
    delay(200);
    digitalWrite(MOTOR_PIN, LOW);
    delay(50);
    digitalWrite(MOTOR_PIN, HIGH);
    delay(200);
    digitalWrite(MOTOR_PIN, LOW);
    delay(50);
  }

  // Play a simple melody, using a pitch table for more concise code.
  // The following line declares an immutable table of integers.
  const int pitches[] = { 262, 330, 392, 523, 494, 440, 392, 349, 330, 294, 262, -1 };

  // Loop through the table.  The break statement will exit this loop, so the
  // continuation test is empty, which will be treated as 'true'.
  for(int i = 0; /* note: empty */ ; i++) {
    int pitch = pitches[i];

    // Once the end marker is seen (an invalid pitch), exit the loop.
    if (pitch < 0) break;

    // Otherwise, play the tone
    tone(SPEAKER_PIN, pitch);
    delay(250);
  }
  noTone(SPEAKER_PIN);
  
  // blissful silence
  delay(2000);
}

Source: Arduino Sketch Sound Demo

About The Author

Scroll to Top
Read previous post:
Professional-Weather-Station-Using-ESP8266-and-ESP32-DIY-Please-Check-the-V2.0-Improved-Version-Link
Professional Weather Station Using ESP8266 and ESP32 DIY (Please Check the V2.0 Improved Version Link!)

PLEASE CHECK THE NEW IMPROVED WEATHER STATION LINEAMETEOSTAZIONE v2.0! LINK VERSION 2.0 LINEAMETEOSTAZIONE HERE LineaMeteoStazione is a complete weather station...

Close