For my project I sent a sequence of frequencies to a speaker indefinitely with a for loop. Every time the loop executes again the speed at which the frequencies change increases. I was going for a “frequency modulation” type of sound, similar to if you connected two oscillators on a synthesizer together, where one was sending control voltage to the other.
I started the setup and code from the first sound demo in the technical exercises, and altered them to accomplish my goal.
// 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 = 1; i >= 0; i++) {
int polyr = (1000 / i);
tone(SPEAKER_PIN, 440);
delay(polyr);
tone(SPEAKER_PIN, 500);
delay(polyr);
tone(SPEAKER_PIN, 550);
delay(polyr);
tone(SPEAKER_PIN, 600);
delay(polyr);
}
}
Leave a Reply
You must be logged in to post a comment.