2

I'm writing a sketch to run on a Mega that builds up an array of timestamps (using the QueueArray library and DS3231 library). I'm having a problem where a struct I'm defining and then queueing is always referring to the first in the list. The relevant bits of the code looks like this:

#include <QueueArray.h>
#include <DS3231.h>
struct payload_t
{
   unsigned long timestamp;
}

QueueArray <payload_t> dataQueue;
DS3231 clock;
RTCDateTime dt;

void setup() {
   clock.begin();
   clock.setDateTime(__DATE__, __TIME__);
}

void loop() {
    payload_t dataPayload;
    dataPayload.timestamp = getTimestamp();
    dataQueue.enqueue(dataPayload);

    payload_t tempItem = dataQueue.dequeue();

    /**
     * At this stage, the dataPayload.timestamp is correct, 
     * but tempItem.timestamp will always show the value from 
     * the first item in the queue
     */

    sleep(1000);
}

unsigned long getTimestamp() 
{
    RTCDateTime dt = clock.getDateTime();
    return dt.unixtime;
}

As per the comment in the main() loop, the first time a packet is added to the queue, the timestamp matches. For subsequent items, the value retrieved from the queue always gives the timestamp from the first item in the queue.

I feel this is my C knowledge letting me down with something basic, but can anyone point me in the right direction?

  • Have you got FIFO and LIFO round the wrong way? dequeue() pulls from the front and enqueue() pushes on the back. Also, in your example code, the queue never contains more than one entry; you add a payload, then remove it on the very next line. – CharlieHanson Oct 24 '15 at 18:55
  • I think you have hit the nail on the head. I'm adding to the end of the queue, then grabbing the first item for the comparison check. The first loop iteration will work, but subsequent ones will show a difference. Obvious when you realise! Thanks for the pointer. – fistameeny Oct 26 '15 at 17:06
  • I'm confused by the fact that tempItem is always the original stamp - are you definitely using dequeue() in your code, or is if just used here as an example? – CharlieHanson Oct 26 '15 at 17:10
2

It's not your C knowlede letting you down, it's just that you haven't gotten used yet to the weirdness of the arduino ide/compiler.

You do not need to create a main() function.

You need to create a loop() function.

main() is already defined and calls loop() in a loop.

Check any other sketch and you'll notice that it usually defines setup() and loop(), almost never main().

You must put in the setup function all the code that needs to be executed once (settings and initializations) and in the loop() function whatever needs to be executed iteratively.

Also, leave declarations out of loop() unless you want such variables to have scope (and life) limited to only one round of loop()

  • My mistake - I'd been reading up on C, so wrote main() instead of loop() in the example code. It's definitely loop() in the actual sketch though... – fistameeny Oct 24 '15 at 16:19
  • Even with that fix (btw, why didn't you past exactly the code you wrote?) I don't see what is strange with what you describe: you initialize an empty queue, put in one element, then extract that one element right away. But you do not show how you are reading it. I suggest that you show exactly the code you are using, rather than some modified version of it. It might well be that the problem lies in how you check that value, rather than in the way it's generated. – Igor Stoppa Oct 24 '15 at 18:56

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.