Primitive variable frequency PWM/synth proof of concept,
sort of a dds synthesizer.
Hello all. I wanted to prove that you could use floating
point math in an interrupt service routine. It works, but
sounds raw. It might be useful for people who want faster
frequency pulse width modulation. 10k sampling rate.
I am very busy the schoolwork, so I'm posting this in hopes
that someone can get some fun out of it.
Use at your own risk !!
#include <WProgram.h>
#include <FrequencyTimer2.h>
//Frequency pot:
//1k from +5V to ground, pot wiper to analog 0
//Pulse width pot:
//1k from +5V to ground, pot wiper to analog 0
//Speaker connection:
//pin 11 to 100ohm to speaker to ground
float n[10];
static long int y=0,x=0,on_=0;
static long int tmp=0;
static long l,f,c=0;
unsigned long int time;
void setup() {
pinMode(11,OUTPUT);
f=0;
Serial.begin(19200);
delay(20);
Serial.print("Ready");
FrequencyTimer2::setOnOverflow( Burp);
FrequencyTimer2::setPeriod(200);
FrequencyTimer2::disable();
}
//extern "C"
void Burp(void) {
n[0] = n[0] + 0.0055 * pow(2.0,(x>>4)/12.0);
if (n[0]>=1.0) n[0] -= 1.0;
if(n[0] >= y/1023.0) //pulse width 0.0 to 1.0
{
if (l!=1)
{
digitalWrite(11, HIGH);
l=1 ;
}
}
else
{
if (l!=0)
{
digitalWrite(11, LOW);
l=0 ;
}
}
on_ = on_ +1;
if (on_==10000) //1 sec mark
{
Serial.print(millis()) ;
on_ =0;
f= 1;
}
}
void loop() {
setup();
time=millis() ;
Serial.println(FrequencyTimer2::getPeriod());
for(;;)
{
//Serial.println(time);
while( time + 1000 > millis() )
{
x = analogRead(0);
y = analogRead(1);
c++;
}
f=0;
//Serial.print((int)(10000.0*0.0055 * pow(2.0,(x>>4)/12.0))) ;
//Serial.print("\t");
//Serial.print(c);
//Serial.print("\t");
//Serial.println(x>>4) ;
c =0;
//time+=1000;
time=millis();
}
}