Hi all!
I am new to Arduino and new to electronics in general. I am (web) developer by trade, so I find the programming bit easier than the electronics.
As my first project I am building a dub siren. A dub siren is a small box which generates sounds like wailing siren, alarm, various waveforms with lfo. I managed to create 2 functions which are the wailing siren and the alarm, the alarm has a potmeter which is used to adjust the frequency of the tone.
I run in to the following problems (so far!! ;))
-
The only waveform or tone I can produce via the Arduino library is a squarewave. I have googled and found various posts from a couple of years back who use various other electronic parts to generate a sine and triangle wave. Is the only way to achieve this via electronic parts and not with code? Can someone maybe point me in a (noobfriendly) direction where I can learn how to achieve this? I want to try to achieve this with the parts I have at home (Arduino Uno starters kit + a lot of resistors, capacitators)
-
The sound output via a small 8ohm speaker is good, when I use a cable to hook up the prototype to my mixing desk the volumeoutput is extremely low. I believe I need to amplify the signal, is this correct? Can someone point me to a good tutorial using Arduino to achieve this? I've never done anything like that in my life and as we say in Holland. I can't see the forest because of all the trees
here is my code so far (warning, this is very very very very ugly prototyping code ;))
/**
const float startHz = 100.0;
const float endHz = 2000.0;
const float maxPotential = 1023.0;
const int button0 = A0;
const int wailingStartHz = 300;
const int wailingEndHz = 3200;
int wailingStart = wailingStartHz;
int wailingEnd = wailingEndHz;
boolean wailingUp = true;
const int button1 = A1;
const int speakerPin = 7;
const int ledPin = 9;
const int potPin = A5;
void setup() {
Serial.begin(9600);
// pinMode(speakerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(button0, INPUT);
pinMode(button1, INPUT);
}
void loop() {
boolean buttonState0 = digitalRead(button0);
boolean buttonState1 = digitalRead(button1);
if ( buttonState1 == 0 ) {
laser(buttonState1);
}
if ( buttonState0 == 0 ) {
if ( wailingUp ) {
wailingSirenUp();
} else {
wailingSirenDown();
}
}
}
void wailingSirenUp() {
for (wailingStart < wailingEnd; wailingStart++;) {
// break out of the for when the button is let go
if ( digitalRead(button0) == 1 ) break;
// Check if we need to reverse the sound
if ( wailingStart > wailingEnd ) {
wailingUp = false;
wailingStart = wailingEndHz;
wailingEnd = wailingStartHz;
delay(10);
break;
}
// Turn on the led
analogWrite(ledPin, 255);
// Generate tone on frequency == wailingStart
tone(speakerPin, wailingStart, 5);
delay(1);
}
analogWrite(ledPin, 0);
}
void wailingSirenDown() {
for (wailingStart > wailingEnd; wailingStart--;) {
// break out of the for when the button is let go
if ( digitalRead(button0) == 1 ) break;
// Check if we need to reverse the sound
if ( wailingStart < wailingEnd ) {
wailingUp = true;
wailingStart = wailingStartHz;
wailingEnd = wailingEndHz;
delay(10);
break;
}
// Turn on the led
analogWrite(ledPin, 255);
// Generate tone on frequency == wailingStart
tone(speakerPin, wailingStart, 5);
delay(1);
}
analogWrite(ledPin, 0);
}
void laser(int buttonState1) {
/**
( (potValue - minimumPotValue) / (maxPotValue - minimumPotValue) ) * (endHz - startHz) + startHz
**/
int time = 100;
if (buttonState1 == 0) {
float analogIn = analogRead(potPin);
float freq = ( (analogIn - 0) / (maxPotential - 0) ) * (endHz - startHz) + startHz;
analogWrite(ledPin, 255);
tone(speakerPin, freq, time);
delay(time);
}
if ( buttonState1 == 0 ) {
float analogIn = analogRead(potPin);
float freq = ( (analogIn - 0) / (maxPotential - 0) ) * (endHz - startHz) + startHz;
tone(speakerPin, freq*1.5, time);
delay(time);
}
analogWrite(ledPin, 0);
}
Thanks!
Erik
//edit
I found this tutorial, but it states that it for the Arduino Due. Can someone please explain me why this can't work on Uno?