// pwm generator box //
//Libraries
#include <TimerOne.h>
//Pins
int pwmPin = 9;
int freqPin = A0;
int dutyPin = A5;
//Variables
long period;
long pulse;
//Operation
void genPWM (int FREQPIN,int DUTYPIN)
{
unsigned int periodVal=analogRead(FREQPIN);
delay(2);
//---------------------------------------------------------------------------------------------------------------------------
if ( periodVal >991 && periodVal <=1024 )
{
period = 20000 ;
}
else
if ( periodVal > 901 && periodVal <=990 )
{
period = 10000 ;
}
else
if ( periodVal > 801 && periodVal <= 900)
{
period = 4000 ;
}
else
if ( periodVal > 701 && periodVal <= 800 )
{
period = 2000 ;
}
else
if ( periodVal > 601 && periodVal <= 700)
{
period = 1000 ;
}
else
if ( periodVal > 501 && periodVal <=600 )
{
period = 400 ;
}
else
if ( periodVal > 401 && periodVal <= 500)
{
period = 200 ;
}
else
if ( periodVal > 301 && periodVal <= 400 )
{
period = 100 ;
}
else
if ( periodVal > 201 && periodVal <= 300)
{
period = 40 ;
}
else
if ( periodVal > 101 && periodVal <=200 )
{
period = 20 ;
}
else
if ( periodVal > 0 && periodVal <= 100)
{
period = 10 ;
}
else
if ( periodVal == 0 )
{
period = 2 ;
}
//--------------------------------------------------------------------------------------------------------------------------
unsigned int pulseVal=analogRead(DUTYPIN);
delay(2);
// ANALOG TO 10 BIT DIGITAL SCALING (0-1023 points for 0-5 volts input.)
pulse=map(pulseVal,0,1023,0,100);
Timer1.pwm(pwmPin,pulseVal,period);
}
//Setup
void setup()
{
analogRead (A0);
Timer1.initialize(period);
pinMode(freqPin, INPUT);
pinMode(dutyPin,INPUT);
pinMode(pwmPin, OUTPUT);
}
void loop()
{
genPWM (freqPin,dutyPin);
}