I made a relay with a transistor and 1k resistor and have the tone I like using a quick delay, with the following sketch.
/*Example 13.0Drive a piezoelectric buzzer with Arduino
http://tronixstuff.wordpress.com/tutorials > Chapter 13*/
void setup(){
pinMode(4, OUTPUT); // sets the pin as output
}
void loop(){
digitalWrite(4,HIGH);
delay(1);
digitalWrite(4, LOW);
delay(1);
}
I want to use this tone as an alarm. What would the code be to break this tone: on for 9/10's of a second and off for 1/10 of a second. So it will kind of resemble alarms in movies (the coundown before the explosion)
use delayMicroseconds for finer timing resolution if needed.
Read up on the blink tutorials and PWM as this would apply to your pizeo.
Have a look on alarm manufacturers websites and see if they say the alarm frequency, then replicate it using PWM
this way you can even produce a LFO tone giving the effect of emergency services sirens.
I gave the analog pwm a try and couldn't get it to pulse every second like an alarm. This is the best I could do to get it to pulse every second. Without using digitalWrite I also noticed I can"t change the frequency to a higher non-clicking sound. If I ever get this working then I have to figure out how to syncronise it to the last ten seconds of my countdown.
/*
Fade
This example shows how to fade an LED on pin 12
using the analogWrite() function.
This example code is in the public domain.
*/
int brightness = 0; // how bright the LED is
int fadeAmount = 10; // how many points to fade the LED by
void setup() {
// declare pin 12 to be an output:
pinMode(12, OUTPUT);
}
void loop() {
// set the brightness of pin 12:
analogWrite(12, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 1 milliseconds to hear the dimming effect
delayMicroseconds(1);
}
If you can help me some more and get this to work using the digitalWrite that would go a long way for my planned new years "estes rocket" launch.
I found another pwm digital led example over at instructables and messed with it until I got it to work the way I wanted. Next is getting this bit of code to work in the last ten seconds of the countdown in the main sketch. Any suggested reading as above to achive this would be great. This code probably can be cleaned up by someone with more knowledge.
//originally by PKM
int duty = 1;
int steps = 2;//steps within the tone fequency
int sunrisespeed = 200;//the timming of the pulse
int i;
int j;
int pulsepin = 12;//pin number
int lookup[1] = {255};//the original had 64 numbers for an array of dimming
// there are freq steps in the phasing
void setup()
{
pinMode(pulsepin, OUTPUT);//pin 12 assigned to pulsepin output
}
void loop()
{
for (i=0; i<steps; i++)
{
duty = lookup[i] * 1;
for (j=0; j<sunrisespeed; j++)
{
// one pulse of PWM
digitalWrite(pulsepin, HIGH);
delayMicroseconds(duty);
digitalWrite(pulsepin, LOW);
delayMicroseconds(2500-duty); //Frequency of the tone
}
}
}
After I get the buzzer to work with the countdown I will only have one step left. Send a 9v pulse for 5 seconds at the end of the countdown using another pin and a relay (I'm thinking) to the estes rocket for ignition^