Measuring time between two inputs

Hello Arduino Lovers,
I am working on a project using Arduino Mega 2560.
I want to read 2 digital inputs (for Ex. pin31 and pin33).
Can I generate these inputs in the arduino itself(for Ex. pin35 and pin37), And give it to pin31 and 33.

Of course, yes. It can be done with a simple line of code:

digitalWrite(OUTPUT_PIN, digitalRead(INPUT_PIN));

Thank you for quick reply.

I dont want to do it internally.
I want to connect using external wires.

[code]
int event1;
int event2;
int interval;
int SV1 = 31;
int SV2 = 33;


void setup() {
  pinMode(31, INPUT);
  pinMode(33, INPUT);
  pinMode(35, OUTPUT);
  pinMode(37, OUTPUT);

}
void loop() {
  digitalWrite(35, HIGH);
  digitalWrite(37, HIGH);

  if (digitalRead(31) == HIGH) {
    event1 = millis();
    Serial.print(event1);
  }
  if (digitalRead(33) == HIGH) {
    event2 = millis();
    Serial.print(event2);
  }
  interval = event2 - event1; // this will give you the interval in milliseconds
  Serial.print(interval);
}

[/code]

I want to record time between 2 inputs.

bonyl:
Thank you for quick reply.

I dont want to do it internally.
I want to connect using external wires.

That code requires that you connect the 2 pins with wires. They are not connected internally

As to your code, you should consider detecting when the inputs become HIGH or LOW rather than when the are HIGH or LOW. See the StateChangeDetction example in the IDE

I am using Arduino Mega2560. I want to measure time between two 5vdc inputs.
Range of time to be measured is 500ms to 3 seconds.
Accuracy required is in milli-seconds.
Slight measuring error is acceptable.
Is the following code the way to go about?

int event1;
int event2;
int interval;
int SV1 = 31;
int SV2 = 33;


void setup() {
  pinMode(31, INPUT);
  pinMode(33, INPUT);
  pinMode(35, OUTPUT);
  pinMode(37, OUTPUT);

}
void loop() {
  digitalWrite(35, HIGH);
  digitalWrite(37, HIGH);

  if (digitalRead(31) == HIGH) {
    event1 = millis();
    Serial.print(event1);
  }
  if (digitalRead(33) == HIGH) {
    event2 = millis();
    Serial.print(event2);
  }
  interval = event2 - event1; // this will give you the interval in milliseconds
  Serial.print(interval);
}

int event1;
int event2;
int interval;
int SV1 = 31;
int SV2 = 33;

void setup() {
pinMode(31, INPUT);
pinMode(33, INPUT);
pinMode(35, OUTPUT);
pinMode(37, OUTPUT);

}
void loop() {
digitalWrite(35, HIGH);
digitalWrite(37, HIGH);

if (digitalRead(31) == HIGH) {
event1 = millis();
Serial.print(event1);
}
if (digitalRead(33) == HIGH) {
event2 = millis();
Serial.print(event2);
}
interval = event2 - event1; // this will give you the interval in milliseconds
Serial.print(interval);
}

Try using millis function to measure both dc inputs in milliseconds

Merged topics to avoid wasting time answering in 2 places

See my comment re detecting changes of state

Connecting two Arduino pins with wires is perfectly normal. Just make sure that at least one of the pins is set as pinMode(pin, INPUT).

If they are both set to OUTPUT you could get a short circuit that would damage the electronics behind the pins.

...R

As you said, I used the button change code.
I am using output pin35 to give input to pin31.
But even though the wire is connected continuously, the state changes from on to off continuously.

/*
// this constant won't change:
const int  buttonPin = 31;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  pinMode(35, OUTPUT);
  digitalWrite(35, HIGH);
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(2000);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}

What keeps the button pin LOW when not pressed ?
Consider using INPUT_PULLUP in its pinMode() to activate the built in pullup resistor and change the wiring and code to match

int V1;
int V2;

long int time1;
long int time2;
long int interval;

void setup() {
  pinMode(31, INPUT_PULLUP);
  pinMode(33, INPUT_PULLUP);
  pinMode(35, OUTPUT);
  pinMode(37, OUTPUT);
  digitalWrite(35, LOW);
  digitalWrite(37, LOW);

  Serial.begin(9600);
}

void loop() {
  V1 = digitalRead(31);
  V2 = digitalRead(33);

  if (V1 == LOW & V2 == HIGH) {
    E1();
  }
  if (V2 == LOW & V1 == HIGH) {
    E2();
  }
  if (V1 == LOW & V2 == LOW) {
    interval = time2 - time1;
    Serial.println(interval);
  }
  delay(1000);
}
void E1()
{
  Serial.println("Time1");
  time1 = millis();
  Serial.println(time1);
}
void E2()
{
  Serial.println("Time2");
  time2 = millis();
  Serial.println(time2);
}

It keeps displaying the values.
How do i make it display only once?

Something like this (untested)

  if (V1 == LOW & V2 == LOW)
  {
    static boolean donePrint = false;
    interval = time2 - time1;
    if (!donePrint)
    {
      Serial.println(interval);
      donePrint = true;
    }
  }
int V1;
int V2;

int a = 0;
int b = 0;
int c = 0;

long int time1;
long int time2;
long int interval;

void setup() {
  pinMode(31, INPUT);
  pinMode(33, INPUT);
  pinMode(35, OUTPUT);
  pinMode(37, OUTPUT);
  digitalWrite(35, HIGH);
  digitalWrite(37, HIGH);

  Serial.begin(9600);
}

void loop() {
  V1 = digitalRead(31);
  V2 = digitalRead(33);

  if (a < 1 & V1 == HIGH & V2 == LOW) {
    Serial.println("Time1");
    time1 = millis();
    Serial.println(time1);
    a++;
  }
  if (b < 1  & V2 == HIGH & V1 == LOW) {
    Serial.println("Time2");
    time2 = millis();
    Serial.println(time2);
    interval = time2 - time1;
    Serial.println(interval);
    b++;
  }

I used this.
One question, do i need to use INPUT_PULLUP?

do i need to use INPUT_PULLUP?

If you do not have any pullup or pulldown resistors in place to keep the inputs in a known state at all time them you would be wise to do so. The most convenient way to do it is to use INPUT_PULLUP in pinMode(), but your wiring and code logic must match. ie, closing a switch needs to take the input pin LOW and the code logic needs to interpret a LOW state on the input pin as the switch being closed.

Okay.
Thank you so much Bob.