1/100th of second momentary contact timer

Hi, I'd like to measure the amount of time a contact remains closed with no "bounce".

I have two powerful magnets, (conductors) that I want to measure the instant they make contact with each other, until the first loss of contact. No actual switch involved, just using the touch of the two magnet conductors as the start and stop triggers.

If they touch again, I do not want the timer to continue or reset. Reset would be done by an additional standard momentary contact switch.

For a visual... imagine having two magnets with a wire coming off each and attempting to press the repelling poles together to start the timer -- until such time that pressure cannot be maintained any longer and contact is broken.

I'd like to have the Arduino set to the highest time resolution... hundreds or thousands of a second to display the length of time of continuous contact. Seems doable, but outside of my expertise.

The magnets are Neodymium and conduct nicely.

Ideas?
Code?

Thanks in advance!!!
Robert

Welcome to the tribe. Do you want to some Arduino homework ?

:thinking:

  • Took AI 10ms to come up with your answer.

:scream:

// Pins
const int magnetPin = 2;    // Magnet contact pin
const int resetPin = 3;     // Manual reset button

// Variables
unsigned long startTime = 0;
unsigned long endTime = 0;
float finalTime = 0;

enum State { IDLE, TIMING, FINISHED };
State currentState = IDLE;

void setup()
{
  Serial.begin(115200); // High baud rate for fast data transfer
  pinMode(magnetPin, INPUT_PULLUP);
  pinMode(resetPin, INPUT_PULLUP);
  Serial.println("System Ready. \n\nPress magnets to start timing.");
}

void loop()
{
  int magnetStatus = digitalRead(magnetPin);
  int resetStatus = digitalRead(resetPin);

  switch (currentState)
  {
    case IDLE:
      // Start timing when magnets first touch (Pin goes LOW)
      if (magnetStatus == LOW)
      {
        startTime = micros();
        currentState = TIMING;
      }
      break;

    case TIMING:
      // Stop timing the instant contact is lost (Pin goes HIGH)
      if (magnetStatus == HIGH)
      {
        endTime = micros();
        finalTime = (endTime - startTime) / 1000.0; // Convert to milliseconds

        Serial.print("Contact Duration: ");
        Serial.print(finalTime, 3); // Display with 3 decimal places
        Serial.println(" ms");

        currentState = FINISHED;
      }
      break;

    case FINISHED:
      // Wait for manual reset button to be pressed
      if (resetStatus == LOW)
      {
        Serial.println("\nResetting... Ready for next test.");
        delay(500); // Debounce delay for the reset button
        currentState = IDLE;
      }
      break;
  }
}

Say more. Contacts bounce, typically an interval is selected to variously ignore the phenomenon.

If you ignore contact bounce, you would measure a very tiny interval of contact, possibly smaller than you can accurately measure with a simple program.

On the other hand, if you consider contact bounce, the selected interval may be in excess of what you expect to measure: typical pushbuttons use times like 10 or 20 or 50 milliseconds.

Or is this all moog, and you did not say this contact is somehow bounceless?

a7

It occurs to me that you may get good results without involving a microprocessor at all.

Use a digital logic analyzer and record a number of tests. Examine the results and you shoukd get a very good idea of the interval.

You don't have a logic analyzer?

https://www.jameco.com/z/LA-USB-Hiletgo-24MHz-8-Channel-USB-Logic-Analyzer_2319143.html

This inexpensive unit is a true surprise, does what it says. You can pay twice as much or half as much, they all seem to come out of the same factory somewhere.

Software for analysis is here

and whike I wouldn't call it intuitive, it is manageable.

The two cheap logic analyzers I have also function well with a version of Saleae's software. I didn't notice any reason to pick one over the other, my uses of the logic analyzer are on the simple side and PulseView is sufficient.

a7

How much time resolution do you think an UNO or NANO classic does NOT have?

A while back for fun I made a super simple code once that simply incremented a count by 1 on every loop cycle. Every 10 seconds I serial printed the average number of loop cycles per second. It showed around 289,000 loops per second. By default the Atmega 328 can measure time in micro seconds (.000001 seconds, or a millionth of a second). That seems like plenty of chances to record time.

Hi LarryD,

Well that was SLICK... thanks for running my question through AI.

If you would... what AI did you use? Claude, Grok, ChatGPT?

And for fun, did you just copy paste my description or did you have to enhance the prompt?

I have some Arduino CPU and a Dev module in a storage locker... now I'll surely dig that box out and play.

Much Appreciated!
Robert

Hey A7,

Thanks, didn't know about Digital Logic Analyzers being so inexpensive... too bad I'm a Mac guy and they are windows.

Bounceless -- I get it -- there is probably going to be some bounce... so a 100ms even 250ms parameter will work for what I'm attempting.

Much Appreciated!

Robert

That was a fun question... I guess UNO and Classic are far faster than I thought they would be...

Thanks for pointing that out!

I'll dig out my vintage collection and try these great suggestions.

All the best,
Robert

Whoops,
Just getting used to this Forum's tools... I should have posted using the Reply button I just spotted.

Thanks for the assist... great group you have here!

Robert

AI is a super handy tool. But it only regurgitates what it finds in a search. That isn’t always good and sometimes it is downright wrong. I use it because it can search and return results far faster than I can. I don’t ask it to show me code or coding techniques much beyond my skill level or much beyond my level of understanding. It has helped me learn by showing and explaining things at just slightly above my skill level.

As a code develops and gets longer, AI can definitely write it faster than I can. BUT as the code grows AI starts to mess up quite a bit. I think it is super important that you need to be able to read and interpret the code AI provides you so you can diagnose problems or ask it more direct questions.

In other words, folks need to know how to code before asking AI to do it for them. Without some knowledge of coding or the project, you won’t know where to begin when it does not work.

If you would... what AI did you use? Claude, Grok, ChatGPT?
Gemini
And for fun, did you just copy paste my description or did you have to enhance the prompt?
Yes, copied and pasted.

Do you want to see a real world display of contact bounce? Check this out.

WRONG LINK, here is the right
LINK

Yep Scott,

I can read and interpret the code that AI came up with...
Creating in an unfamiliar language... not so much, fixing it -- for me, easy.

Completely agree!
Best,
R

Thanks Larry,
When I get to my storage locker and gather my collection, I'll get back to you.
VERY much appreciated!
Robert

Hey Sonofcy,

I got the EXACT same results from my first Arduino project when I put my tongue on the board.

Great video...
**

;-)

**

Check this out LINK

Also this LINK

I built @LarryD / AI's sketch.

When the test button is pressed, an instant value is printed, ranging from 28 to 80 us, consistent with measuring the first contact bounce, +/-.

Applying a bounce free input correctly measures the down time of a signal.

If you add a debounce interval, you won't be able to measure anything smaller in a meaningful way.

The title is for 1/100th of a second. I don't see how a simple program can hack this.

If you meant measure the interval in units of 0.01 seconds, that is straightforward. You would have to say how the bouncing shoukd enter into the timing - first contact to last? The stable period between the closing bounces and the opening bounces? Like that.

I have few other ideas but am currently out of the things one runs out of. Maybe I'll ask an AI for 10 ms of its thinking. :expressionless:


In the meantime,

PukseView and the Saleae software both run on the Mac…

a7

This can't be bad :)

I use PulseView

EDIT: ooops did not see that @sonofcy had already shared links

I did and got a completely different answer that did not include code but did consider bounce.