0

I have switch is a line connected to Digital pins 1,2,3,4,5 . When pins 1 connects, I need a timer to start. as the rest connect i need it to mark the time in the console output of each one and End on the timer on connection to pin 5. ex

START:
0.00
1.43
2.12
2.94
3.89
END

Also is there a way to Save the data to a log (.txt or whatever) If not its fine but I need to record results.

(This is for a school project) THANKS!

  • 2
    Why two pins per switch? You only need one. – Majenko Feb 29 '16 at 18:14
  • my bad, Thats what I meant. Sorry. – Display Word Feb 29 '16 at 18:15
  • 4
    @DisplayWord Please update the question too to reflect what pins are used and how the buttons actually are connected. – Avamander Feb 29 '16 at 18:19
  • @Avamander Done – Display Word Feb 29 '16 at 19:02
2

I will not give you code, only clues on how to write the code yourself.

The way to tackle this is to (at first) remove the Arduino from the equation. Replace it, instead, with you.

Imagine you are doing the timing yourself. How would you go about doing it?

This is how I would set about the task:

  1. When the first button gets pressed I would look at the clock and jot down the current time.
  2. When the second button gets pressed I would again look at the clock, and subtract the first time from the current time to find out how much time has elapsed. I would jot that down.
  3. When the third button is pressed I would do the same again - look at the clock, subtract the start time, and jot down the difference.
  4. And I would keep going until I had run out of buttons.

So now you need to find the right code to read what buttons are doing, get the current time, and finally send the times somewhere useful.

For all those there are many many many examples on the web.

  • The point of the experiment is to get the most accurate results as possible (Im using photocells and lasers). I have no Idea how to code the arduino. I was give one with the software by my teacher. – Display Word Feb 29 '16 at 19:51
  • But i will keep this in mind – Display Word Feb 29 '16 at 19:51
  • +1 for the way of explaining the replacement of a person and the logic – RSM Mar 1 '16 at 13:30
  • Yes, Did help my understanding. Thank You! – Display Word Mar 2 '16 at 14:53
2

Are you familiar with interrupts? The best/most efficient way to do this is with pin interrupts on each input pin (and make sure your switches are wired correctly and debounced).

Unfortunately, within the Arduino libraries, there are only two pin interrupts (0 and 1), which correspond to pins 2 and 3.

You have two options:

Use a polling solution. Use one pin with an interrupt to start the timer (save the value of millis()) and then poll the other switches in a loop, capturing the millis() value at each switch closure.

Alternatively, the ATmega can fire an interrupt when any pin in a port changes, but IIRC this isn't built into the Arduino libraries and requires direct register manipulation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.