1

I'm working on a project in which I need a series of very accurate pulses containing 2 shots in every pulse. The pulse should have 100µsec on time followed by 300ms off time. But when I'm programming the Arduino Due I'm getting random pulses of non-uniform width.But when I'm trying the same code in microsecond range I'm getting perfect wave. Here is the code that I'm using

#define LED 8 // Pin designated to give out the pulse
int cycles = 2;  // No. of times you want it to give pulse
int high = 100; // High time in microseconds
int low = 300;  // Low time in milliseconds
int i = 0;
int univ_cycle = 10;

void setup() {
  // put your setup code here, to run once:
 pinMode(LED, OUTPUT); 
 Serial.begin(9600);

}
void loop(){
  for (i ; i < univ_cycle; i ++){
  pulse();
  delay(100);
  }
}
void pulse() {

  // put your main code here, to run repeatedly:
  for(int counter = 0;counter < cycles;counter++){
  digitalWrite(LED, HIGH);
  delayMicroseconds(high);
  Serial.println("ontime");
  digitalWrite(13, LOW);
  Serial.println("offtime");
  delay(low);
  Serial.println(counter);
  }

}

Here is the image of the waveform in microsecond setting enter image description here

Here is the wavefrom that we got in millisecond setting enter image description here

I would like to know how can I get the similar wave in microsecond also.

  • 1
    I would suggest as a start removing those Serial.println calls. They will make complete nonsense of any timing you attempt to do. – Majenko Apr 6 '18 at 12:52
  • Look into using one of the 16bit timers. What do you mean with “two shots in every pulse”? – Gerben Apr 6 '18 at 13:04
  • @Gerben I assume he means ___-__-______-__-______-__-_____ – Majenko Apr 6 '18 at 14:27
  • @Majenko silly me. I should have looked at the scope output. – Gerben Apr 6 '18 at 15:10
  • @Gerben Can you please suggest me how to use 16-bit register? – Siddhant Bansal Apr 7 '18 at 4:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.