Hello:
I've an application where digital inputs are monitored continuously. Application needs to see HIGH on a inputs within certain duration, say for example within 4 seconds. If it does not it sends the error message. If it sees the input within 4 seconds, application should reset and restart the timer to monitor next HIGH state on the input. Digital input will be toggling between high and low. If stays HIGH or LOW continuously, application will set fault, as there is no LOW to HIGH transition within 4 seconds.
Could anyone please let me know how to achieve this using software timers in Arduino.
If the pulses might be very short you can use the Pin Change Interrupt feature found on many pins to call a function whenever a the hardware detects that a pin has changed. Use the function to note the time that the pin changed. In loop() you just check to see if the current time is more than 4 seconds past the last time each pin changed.
gcshwetha:
Could anyone please let me know how to achieve this using software timers in Arduino.
For slow actions you never need anything else than the "millis()" function, which creates a millisecond timer in the Arduino core library (uses internal Timer0).
Timeout code perhaps something like:
#define TIMEOUT 4000 // toggle timeout in milliseconds
const byte togglePin=2; // pin state must toggle within 4 seconds or alarm becomes active
const byte alarmPin=13; // board LED as alarm pin
void setup() {
Serial.begin(9600);
Serial.println("Good night and good luck!");
pinMode(togglePin,INPUT_PULLUP);
pinMode(alarmPin,OUTPUT);
}
boolean timeout=false;
boolean lastState;
unsigned long lastToggle;
void loop() {
boolean newState=digitalRead(togglePin);
if (newState!=lastState)
{
lastToggle=millis();
lastState=newState;
if (timeout)
{
Serial.print(millis()/1000.0,3);
Serial.println(" Alarm OFF");
timeout=false;
digitalWrite(alarmPin,timeout);
}
}
if (!timeout && millis()-lastToggle>=TIMEOUT)
{
Serial.print(millis()/1000.0,3);
Serial.println(" Alarm ON");
timeout=true;
digitalWrite(alarmPin,timeout);
}
}