I will try and explain this the best way I can think off, I'm working on a project where I have to pro-mini's.
The first one uses this code below where it generates some pulses, 2 short pulses (about 250ms arpart) and one about 750ms long, The first one is only for testing the other pro-mini as the unit this will be connected to is the unit that generates the pulses. The idea is that the 2 short pulses will turn a relay on then when the longer pulse is detected it turns the relay off. The testing pro-mini generates the pulses ok, the part I'm not sure about is how to detect how long the 2 short pulses are compared to the longer pulse is.
The pulse code.
//Send out 2 short pulse's to swtich on and longer singal pulse to trun off
const int ledPin = 7; // Turn on Signal
int ledState = LOW; // ledState used to set the LED
int Alarm_on = 8; //Alram on switch signal
int Alarm_off = 9; //Alram off switch signal
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long currentMillis ;
const long interval = 250; // interval at which to blink (milliseconds)
int time_flag = 0; //Timer flag
unsigned long ActivateTime;
long AlramTime = 750UL; //sound alram for 10 seconds
void setup()
{
digitalWrite(Alarm_on, HIGH);
pinMode(Alarm_on, INPUT);
digitalWrite(Alarm_off, HIGH);
pinMode(Alarm_off, INPUT);
pinMode(ledPin, OUTPUT);
delay(100);
}
void loop() {
// Checks alram on swtich then goto alrarm on function to send out 2 short pulses (tell other unit to turn on same input pin)
if (digitalRead(Alarm_on) == LOW) { // check alram on swtich
time_flag = 1; //set the down falg to 1
ActivateTime = millis();
Turn_on();
}
// Checks alram of swtich then goto alrarm of function to send out 1 longer pulses (tell other unit to turn off same input pin)
if (digitalRead(Alarm_off) == LOW) {
time_flag = 1; //set the down falg to 1
ActivateTime = millis();
Turn_off();
}
}
void Turn_on() { // trun on function
while (time_flag) {
unsigned long Tunonmills = millis();
if (Tunonmills - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = Tunonmills;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
if ((time_flag == 1) && (millis() - ActivateTime >= AlramTime)) {
if (time_flag == 1)
delay(50);
time_flag = 0;
ledState = LOW;
digitalWrite(ledPin, ledState);
return;
}
}
}
void Turn_off() { //turn off function
while (time_flag) {
unsigned long Tunoffmills = millis();
if (Tunoffmills - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = Tunoffmills;
ledState = HIGH;
digitalWrite(ledPin, ledState);
}
if ((time_flag == 1) && (millis() - ActivateTime >= AlramTime)) {
if (time_flag == 1)
delay(50);
time_flag = 0;
ledState = LOW;
digitalWrite(ledPin, ledState);
return;
}
}
}
I've looked at the puslein and been playing around with I seem to capture the first pulse of the 2 short ones which the first one is reading 12397 on the LCD not sure about the second pulse and the longer pulse is showing 39636 on the LCD. the pulse code below
// Include libraries
#include <LiquidCrystal_I2C.h> // I2C LCD display
#include <Wire.h>
int pin = 7;
unsigned long duration;
unsigned long previousMillis = 0; // will store last time LED was updated
LiquidCrystal_I2C lcd(0x027, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
// constants won't change :
const long interval = 1000; // interval at which to blink (milliseconds)
void setup()
{
lcd.begin (16, 2); // for 20 X 4 LCD module
lcd.setBacklightPin(3, POSITIVE); //set back light pin
lcd.setBacklight(HIGH); //Turn the back light on the LCD
Serial.begin(9600);
pinMode(pin, INPUT);
///LCD DISPLAY for debugging only removed one sorted
lcd.setCursor(0, 0);
lcd.print("====================");
lcd.setCursor(0, 1);
lcd.print("pulse test");
delay(1000);
lcd.clear();
}
void loop()
{
duration = pulseIn(pin, HIGH);
// unsigned long currentMillis = millis();
//if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
// previousMillis = currentMillis;
lcd.setCursor(0, 0);
lcd.print("duration = ");
lcd.print(duration);
lcd.print(" ");
// }
}
This is only the testing for recording the pulse as I'm not 100% sure how long they actually are yet until I can capture the 2 short pulses and the longer pulse, hopefully I should be able to work how to switch the relay on/off afterwards but just need to capture the true pulse.
Is there a better method or correct method to capture the pulse's ?