#include <Arduino.h>
const int pulsePin = 8; // Arduino pin 8 for the pulse output
#include <Arduino.h>
void delay_100_micro() {
// 1 ms delay function using inline assembly
asm volatile (
"ldi r20, 198\n"
"L1: dec r20\n"
"nop\n"
"nop\n"
"nop\n"
"nop\n"
"nop\n"
"brne L1\n"
);
}
void delay_micro(float micro_s) {
int c = (int)(micro_s / 100);
for (uint16_t i = 0; i < (micro_s / 100); i++) {
delay_100_micro();
}
}
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(pulsePin, OUTPUT); // Set pulsePin as output
Serial.println("Enter high time in milliseconds (1 to 2 ms):");
}
void loop() {
while (Serial.available() == 0) {
delay(10); // Wait for serial input
}
float highTime = Serial.parseFloat(); // Read float value from serial input
Serial.read(); // Clear the newline character from serial buffer
// Check if the value is within the valid range (1 to 2 ms)
if (highTime < 1.0 || highTime > 2.0) {
Serial.println("Invalid input. Please enter a value between 1 and 2 milliseconds.");
return;
}
// Calculate low time
float lowTime = 20.0 - highTime;
unsigned long t1 = micros();
delay_100_micro();
unsigned long t2 = micros();
Serial.print("Time taken by the task: ");
Serial.print(t2 - t1);
Serial.println(" microseconds");
// Generate the pulse
for (int i = 0; i < 20; i++) {
digitalWrite(pulsePin, HIGH); // Set pulsePin high
delay_micro(highTime * 1000); // Delay for the high time
digitalWrite(pulsePin, LOW); // Set pulsePin low
delay_micro(lowTime * 1000); // Delay for the low time
}
}