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?
1 Answer 1
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– fistameeny2015年10月24日 16:19:26 +00:00Commented Oct 24, 2015 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– Igor Stoppa2015年10月24日 18:56:41 +00:00Commented Oct 24, 2015 at 18:56
dequeue()
pulls from the front andenqueue()
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.tempItem
is always the original stamp - are you definitely usingdequeue()
in your code, or is if just used here as an example?