Little help needed with measuring Input Pulses in Arduino Pro mini

Hi there. First of all thanks for sparing your time reading my problem. Secondly, please excuse my mistakes as I am just a dumb electrician with no programming skills.

About the project, I want use Arduino pro mini as circuit breaker for a project. I have written the code as much as I could but now I am stuck because i have no skill in programming :frowning: . I messed around examples and came with a code which i have posted below. Even if you are able not to help, I am really thankful to you :slight_smile:

The circuit:

  1. Pin2 is a button input tied to the ground with a 10k resistor.
  2. Pin3 will receive positive HIGH pulses from a magnetic sensor. This pin also tied to the ground with a 10k resistor.
  3. Pin13 is attached to a LED. (which will be later attached to a relay for breaking a circuit)

The project:

  1. When PIN 2 goes HIGH, after a two seconds delay.
  2. If PIN 3 reads no HIGH pulses, then PIN 13 should be HIGH.
  3. If PIN 3 reads HIGH pulses and the interval between HIGH pulses is less than 600milliseconds, PIN 13 should remain LOW.

The code:

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int pulsesPin = 3;      // the number of the pulse receiving pin
const int ledPin =  13;      // the number of the LED pin
unsigned long duration; 

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int pulsesState = 0;          // variable for reading the pulses status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  // intialize the pulses pin as an input:
  pinMode(pulsesPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  // read the state of the pulses value:
  pulsesState = digitalRead(pulsesPin);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // Two seconds delay:
   delay(2000);
    // if pulses are longer than 600 milliseconds intervals:
    
    // F1 F1 F1 (Help, Help, Help):
    // Don't know how to write the code in this line!!!!! :
    
    // turn LED on:
      digitalWrite(ledPin, HIGH);
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

Any help will appreciated. Thanks again for your time.

  1. When PIN 2 goes HIGH, after two seconds, Arduino should start detecting HIGH pulses and the interval between them.

Detecting them where?

You need to read the pulse pin only when the switch pin is HIGH, or you need to look at the state change detection example to set a flag indicating that measuring of pulses (after some interval) is required.

Then, you need to see if that interval has elapsed. Only if reading pulses is needed, and the interval has passed do you need to bother reading the pulse pin.

It seems like you need to detect when the state changes on that pin, too. When it does, record the time. If it is the transition from LOW to HIGH that matters, calculate how long it was from the HIGH to LOW transition. Otherwise, do the opposite.

did you use the 'button state change' sketch?

The way you are using delay(2000) you cannot know if the button was pressed briefly, or held down for the entire 2 second period - does that matter?

To check for pulses at 600 msec intervals you need to save millis() every time the pin is HIGH (which resets your timer). Then calculate millis() - savedMillis. When there are pulses the difference will never go beyond 600 and if it does, you will know that there was a missing pulse.

...R

Thanks Stu.

Thanks Paul, I rephrased my post in an attempt to make it more clear.

Robin2:
The way you are using delay(2000) you cannot know if the button was pressed briefly, or held down for the entire 2 second period - does that matter?

To check for pulses at 600 msec intervals you need to save millis() every time the pin is HIGH (which resets your timer). Then calculate millis() - savedMillis. When there are pulses the difference will never go beyond 600 and if it does, you will know that there was a missing pulse.

...R

Robin thanks for your reply,

I may be wrong with delay () function, but I don't know because this is my first attempt to write a sketch. By the delay function i want to say that when Pin2 will go high (button will remain pressed) and after after two seconds of happening so, Pin3 should be read for HIGH pulses, if High pulses are present with interval of less than 600milliseconds between each HIGH pulse, than Pin13 should be LOW. If after two seconds of Pin2 being HIGH and Pin3 being absent of HIGH pulses or having HIGH pulses with interval of more than 600milliseconds then PIN13 should be HIGH.

The button on pin2 will not be pressed briefly but it will pressed for long time

xtreme_2010:
i want to say that when Pin2 will go high (button will remain pressed)

Then you need something like this pseudo code

void loop() {
   If (button is NOT pressed)
      startMillis = millis()   // resets timer
   else if (millis() - startMillis >= 2000) // else implies that button is pressed
       do stuff as time has expired
}

...R

Thanks Robin, I will look into it. :slight_smile: