Help with arrays and debouncing
-
I am not by a computer now but :
define NUMCHILDREN sizeof(children)
children = 8 chars.. :) you cant define that like so. The compiler will try to do a size of before code is compiled.
The correct way would be uint8_t numchildren = sizeof(children); as your first line of code.
and note, dont have a habit of using INT as standard (2 bytes, -32,768 to 32,767) -
I am not by a computer now but :
define NUMCHILDREN sizeof(children)
children = 8 chars.. :) you cant define that like so. The compiler will try to do a size of before code is compiled.
The correct way would be uint8_t numchildren = sizeof(children); as your first line of code.
and note, dont have a habit of using INT as standard (2 bytes, -32,768 to 32,767)@Damme no, that's not true....
Children is an array of int. Sizeof(int) = 2, sizeof(children) = size of the whole structure = 8*sizeof(int) = 14I usually define a macro to get the number of elements in an array:
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))Then use as:
for(int I = 0; I < ARRAY_SIZE(children); ++I) .... -
@Damme no, that's not true....
Children is an array of int. Sizeof(int) = 2, sizeof(children) = size of the whole structure = 8*sizeof(int) = 14I usually define a macro to get the number of elements in an array:
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))Then use as:
for(int I = 0; I < ARRAY_SIZE(children); ++I) ....@Yveaux Hmm, you're right. I didsomething just the other day and realized that it was sizing the variable as a string, not the content of variable itself! and it was somthing like in this example.Hmm, too bad I didn't remenber correcly.. :)
-
@Damme no, that's not true....
Children is an array of int. Sizeof(int) = 2, sizeof(children) = size of the whole structure = 8*sizeof(int) = 14I usually define a macro to get the number of elements in an array:
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))Then use as:
for(int I = 0; I < ARRAY_SIZE(children); ++I) ....@Yveaux said:
I usually define a macro to get the number of elements in an array:
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))Great, thanks. Just so I'm clear, I would just paste in the line of code above as is? I don't need to substitute x for anything else?
I was also just thinking... Is there any problem with having multiple Bounce objects in the array like I did?
Thanks again. I'll try this tonight when I get home.
-
I am not by a computer now but :
define NUMCHILDREN sizeof(children)
children = 8 chars.. :) you cant define that like so. The compiler will try to do a size of before code is compiled.
The correct way would be uint8_t numchildren = sizeof(children); as your first line of code.
and note, dont have a habit of using INT as standard (2 bytes, -32,768 to 32,767) -
@Damme said:
and note, dont have a habit of using INT as standard (2 bytes, -32,768 to 32,767)Sorry forgot to respond to this before. What would you recommend I use instead, byte?
Thanks.
@petewill said:
What would you recommend I use instead, byte?
uint8_t, which is effectively an unsigned char or byte.
Bit more typing, but I prefer making things explicit.
Also no discussion on the size if you ever decide to port your code to a different platform (regular int on ATMega is 8bit, Arduino defines it as 16bit, on 32bit intel x86 or e.g. Raspberry Pi it is 32bit and on 64bit intel x64 it is 64bit !!) -
Ok, making progress. I think I have more problems than just my code. I am using a Mega so I can get more inputs and apparently I don't have something wired quite right. My sensors are sending 10 times but then stop because they are not receiving an Ack. That will then cause my node to time out. It seems I don't have the radio wired correctly. Any idea which pin is wrong? The sends are working fine when going to Vera (at least until the timeout). Here is what I'm getting in the serial monitor:
Relaying message back to gateway. Tx: fr=12,to=0,la=12,ne=0,ci=21,mt=1,ty=16,cr=235: 0 Ack: receive timeout Natalie's Door: 0 Relaying message back to gateway. Tx: fr=12,to=0,la=12,ne=0,ci=21,mt=1,ty=16,cr=140: 1 Ack: receive timeout Natalie's Door: 1 Relaying message back to gateway. Tx: fr=12,to=0,la=12,ne=0,ci=21,mt=1,ty=16,cr=235: 0 Ack: receive timeout Natalie's Door: 0 Relaying message back to gateway. Tx: fr=12,to=0,la=12,ne=0,ci=21,mt=1,ty=16,cr=140: 1 Ack: receive timeout Open ping reading pipe: 12 Tx: fr=12,to=255,la=12,ne=255,ci=255,mt=4,ty=9,cr=85: No relay nodes was found. Trying again in 10 seconds. Tx: fr=12,to=255,la=12,ne=255,ci=255,mt=4,ty=9,cr=85: No relay nodes was found. Trying again in 10 seconds. Tx: fr=12,to=255,la=12,ne=255,ci=255,mt=4,ty=9,cr=85: No relay nodes was found. Trying again in 10 seconds.Thanks!
-
Probably should have done some more testing before posting. Just anxious to get this working before I have to go to bed...
I figured out the problem. I had to put a capacitor across my radio 3.3v and ground pins. I haven't had to do this with any of my pro mini sensors so I forgot that may be an issue. Anyway, after doing that I got an Ack: received OK message!
Hopefully my mistakes can help someone else avoid them...

-
Ok, looks like I was wrong again. Turns out was a bad wire (or wires) connecting the radio to the Mega. Either that or the Mega has some bad connections. Anyway, I switched out the wires and all is working again (for now).
@petewill said:
Ok, looks like I was wrong again. Turns out was a bad wire (or wires) connecting the radio to the Mega. Either that or the Mega has some bad connections. Anyway, I switched out the wires and all is working again (for now).
Loose wires are a bear to find at times - you do something (like adding a cap) and you think you fixed it. Then for no good reason the problem comes back. Been there.