SoundDemo Arduino Sketch

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

Full Source Code

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// 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);
}

Table Of Contents

Previous topic

ServoSweep Arduino Sketch

Next topic

EventLoopDemo Arduino Sketch