0

I want to add a timer to this function so that every time a uid is read it restarts and I can set another function to do a serial.write once a certain time is reached. Let's call this an idle time function. I cannot tie this to the ID read as I have 28 IDs potentially being read. I want to reset my audio player with an ASCII command via serial.write if no iud has been read for say longer than 180 seconds... Suggestions?

if(uid[0] == 0x64 && uid[1] == 0xBF && uid[2] == 0xD8 && uid[3] == 0x51)
{
//pause at beginning
    delay (500);
//Serial.write("for Bässgen MM3210")
    Serial.write("listplay 1 1");
    Serial.write(13);
//pause at end
    delay (3000); 
} 
Automa
  • 1
  • 2

1 Answers1

0

You could assign each uid a value from the millis() call.

It basically counts the milliseconds since the chip turns on. It is stored in a long so it will reset to zero every 80 days or so I think (I never left it that long)

I did this to time a random array that was writing faster or slower based on how many pieces of data were in it. Instead of using delay() and counted loops, using millis() allowed me to jump in at a certain time.

djUniversal
  • 624
  • 5
  • 7
  • millis () overflows after 9 hours according to documentation. Also I need to reset it after each time a uid is read. – Automa Oct 13 '14 at 07:00
  • http://arduino.cc/en/Reference/Millis shows 50 days. Each time a uid is read all you do is update a running variable (ie: long prevMillis) then to measure time at a later point you could just use prevMillis - Millis() – djUniversal Oct 13 '14 at 14:13
  • The upside of using Millis() is that it will count on without holding a thread like delay() does, so you dont require an interrupt if you dont want. The 50 day limitation shouldn't be an issue either since I believe the long will overflow back to the beginning so most comparative equations will still work as long as you are mindful of that in code. – djUniversal Oct 13 '14 at 14:20
  • Thx for the info. I had mine from [Arduino Progr. Notebook](http://playground.arduino.cc/uploads/Main/arduino_notebook_v1-1.pdf)! But your idea is very good anyway. I will try it out and report back with results. – Automa Oct 14 '14 at 09:45
  • I found this in the official Arduino Refernce Manual: "unsigned long millis() Description Returns the number of milliseconds since the Arduino board began running the current program. Parameters None Returns The number of milliseconds since the current program started running, as an unsigned long. This number will overflow (go back to zero), after approximately 9 hours and 32 minutes." Strange that there is inconsistencies in the documentation. Tempted to test which one is right... but then. – Automa Oct 14 '14 at 13:16
  • Strange there is a difference. I know that ~50 days is correct because that is the limit of how far an unsigned long can count in milliseconds. – djUniversal Oct 14 '14 at 13:39
  • The overflow should mean that your code will still work though. – djUniversal Oct 14 '14 at 13:39