/*
Arduino Forum
Topics: Triggering 5 millisecond pulses part #1
Category: Using Arduino
SubCategory: Programming Questions
Link: https://forum.arduino.cc/t/triggering-5-millisecond-pulses-part-1/1135999/85
*/
#define ainPin A0
#define compareVoltage 2.0f
#define pulseTime 50ul
#define hiAmplitude 6
#define loAmplitude 1
float input_voltage;
uint32_t pulseStartAtMs;
bool inPulse;
uint8_t Millis ;
void setup() {
Serial.begin(115200);
input_voltage = 0.0f;
inPulse = false;
}
void loop() {
int analog_value = analogRead(ainPin);
float prevInputVoltage = input_voltage;
input_voltage = (analog_value * 5.0) / 1023.0f;
if (prevInputVoltage < compareVoltage && input_voltage >= compareVoltage) {
Millis = hiAmplitude;
inPulse = true ;
pulseStartAtMs = millis() ;
}
if (inPulse && millis() - pulseStartAtMs >= pulseTime) {
inPulse = false ;
Millis = loAmplitude;
}
Serial.println(
"Input:= " + String(input_voltage, 2)
+ ", Millis:= " + String(Millis)
+ ", Compare Voltage:= " + String(compareVoltage, 2)
);
}