Pulse train frequency problem


hello all,  

greetings!  this first time programming arduino, , having fun!  the code supposed generate sequence of pulses have variable frequency, duty cycle, , total pulse number.  i modified servo controller code found online produce following results.  

with frequency = 2000 hz , dutycycle = .25, out (scoped pin 7) 1926hz , 25.35% duty cycle.  as shown in first picture it's pretty close!  

the problem arises when try increase frequency , duty cycle 20khz, , 0.95 duty cycle.  as shown in second picture, way off, 14.47khz, , 78.3%!!  

the error steadily increases increase frequency.  is there problem in arithmetic, or data type used define variables?  am doing stupid?  any appreciated.  thanks!  


pictures coming on next post...



hi everyone, here code previous post.  sorry split this.  



code: [select]
//adapted http://todbot.com/
//generates pulse train of specified pulse number, frequency, , duty cycle
//pin 7 output

int pulsenumber = 2500;          // number of pulses in pulse train                
double frequency = 2000;            //frequency in hz                              
float dutycycle = .25;                //duty cycle                                    
unsigned long seconds = 1;        //delay between pulse sets                        

float on;
float off;                      
float period;


void setup() {
 pinmode(7, output);          // set outpin pin output
}

void loop() {
period = (1 / frequency) * 1000000;
on = dutycycle * period;
off = period * (1-dutycycle);

 // call pulse function n = pulsenumber
 for (int i=1; i<=pulsenumber; i++) {  
   pulse(on, off);
 }
 
 delay(seconds * 1000ul);  // delay between pulse sets
}

void pulse(double on, double off) {
 digitalwrite(7, high);       // set pin high
 delaymicroseconds(on);      // waits "on" microseconds
 digitalwrite(7, low);        // set pin low
 delaymicroseconds(off);      //wait "off" microseconds
}


picture 1


picture 2





Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Pulse train frequency problem


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums