Buongiorno a tutti.
Dovrei generare un treno di 50 impulsi con frequenza 20Hz e Ton di 2.1ms sul pin 11 PWM. Quindi frequenza 20Hz T=50ms. Il duty cycle è quindi Ton/T = 0.042.
A questo punto il codice per generare quanto sopra vi torna?
int pulseNumber = 50; // Number of pulses in pulse train
double frequency = 20; //frequency in Hz
double dutyCycle = .042; //duty cycle
unsigned long delay_Charge = 10; //delay between pulse sets
int on;
int off;
double period;
#define fastWrite(_pin_, _state_) ( _pin_ < 8 ? (_state_ ? PORTD |= 1 << _pin_ : PORTD &= ~(1 << _pin_ )) : (_state_ ? PORTB |= 1 << (_pin_ -8) : PORTB &= ~(1 << (_pin_ -8) )))
// the macro sets or clears the appropriate bit in port D if the pin is less than 8 or port B if between 8 and 13
void setup() {
pinMode(11, OUTPUT); // set outPin pin as output
}
void loop() {
period = (1 / frequency) * 1000000;
on = dutyCycle * period;
off = period * (1-dutyCycle);
// call Pulse function for n = pulseNumber
for (int i=1; i<=pulseNumber; i++) {
Pulse(on, off);
}
delay(delay_Charge); // delay between pulse sets
}
void Pulse(int on, int off) {
fastWrite(11, HIGH); // set Pin high
delayMicroseconds(on); // waits "on" microseconds
fastWrite(11, LOW); // set pin low
delayMicroseconds(off); //wait "off" microseconds
}