ProfHuster
Active member
I am programming an ultrasonic distance sensor and I need a pulse train of exactly eight 40 kHz pulses at 50% duty cycle. I have tried several approaches and can't get what I want. First I used an IntervalTimer that counted down then ended the timer. I got some double pulses.
BUT, here is my latest. The problem is that the timer is not synced with the PWM clock so it shuts off after the right time but most of the time there is a partial pulse at the beginning and as the end, so seven full pulses, and two partial.
What is a good way of just generating eight pulses?
BUT, here is my latest. The problem is that the timer is not synced with the PWM clock so it shuts off after the right time but most of the time there is a partial pulse at the beginning and as the end, so seven full pulses, and two partial.
What is a good way of just generating eight pulses?
Code:
#define PIN 10
IntervalTimer startTrain; // Triggers new pulse train
IntervalTimer stopTrain; // Turns train off
void setup() {
Serial.begin(1000000L);
delay(1000);
Serial.println("TestDAC");
Serial.print("Pin "); Serial.println(PIN);
pinMode(PIN, OUTPUT);
analogWrite(PIN, 0);
analogWriteFrequency(PIN, 40000);
analogWriteResolution(8); // 0 - 255
startTrain.begin(startTr, 100000);
}
void loop() {
delay(1000);
}
#define STOP_US ((int)(1e6 * 8 / 40e3 + 0.5))
void startTr(){
analogWrite(PIN, 127);
stopTrain.begin(stopTr, STOP_US);
}
void stopTr(){
analogWrite(PIN, 0);
stopTrain.end();
}
// EOF