Camberley, Surrey, UK
Offline
Newbie
Karma: 0
Posts: 7
Arduino rocks
|
 |
« on: April 26, 2012, 06:38:44 pm » |
I'm reading a string of characters from serial but I'm only interested in one byte of data. I thought the most efficient way to do this was to use an array of 3 chars (char c[2];) and look for my byte based on the bytes either side... while (Serial.available() > 0) { c[0] = Serial.read(); if (c[2] == 'P' && c[0] == 'V') { if (bitRead(c[1],0) == 0) { // do some stuff here } } c[2] = c[1]; c[1] = c[0]; } My code doesn't work. The last two lines are breaking it but I can't see what I'm doing wrong. I'm sure it's something basic. Please help. Thank you.
|
|
|
|
« Last Edit: April 26, 2012, 06:45:53 pm by Dogsbody »
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 230
Arduino rocks
|
 |
« Reply #1 on: April 26, 2012, 06:44:20 pm » |
you're missing a } at the very end
|
|
|
|
|
Logged
|
|
|
|
|
Camberley, Surrey, UK
Offline
Newbie
Karma: 0
Posts: 7
Arduino rocks
|
 |
« Reply #2 on: April 26, 2012, 06:48:29 pm » |
Sorry, no, that was just be copy/pasting the bit of code out the bigger program. I've added it back into the top post, thanks. There is a whole load of debug lines in my code now as I've been trying to work out what's happening 
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35550
Seattle, WA USA
|
 |
« Reply #3 on: April 26, 2012, 06:51:06 pm » |
I thought the most efficient way to do this was to use an array of 3 chars (char c[2]  and look for my byte based on the bytes either side... An array of 2 characters, one of which will be the NULL, you mean. The value in the brackets is the count, not the upper index. You need to allow room for the trailing NULL, too.
|
|
|
|
|
Logged
|
|
|
|
|
Camberley, Surrey, UK
Offline
Newbie
Karma: 0
Posts: 7
Arduino rocks
|
 |
« Reply #4 on: April 26, 2012, 07:03:19 pm » |
You absolute star! Thank you :-)
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 31
Posts: 2947
I only know some basic electricity....
|
 |
« Reply #5 on: April 26, 2012, 07:37:57 pm » |
Why should there have to be a NULL, Paul? I see no string or print commands.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #6 on: April 26, 2012, 07:55:27 pm » |
... use an array of 3 chars (char c[2]  and look for my byte based on the bytes either side... if (c[2] == 'P' && c[0] == 'V') { } Regardless of whether there is a null or not, an array of 2 characters has indexes 0 and 1. So you can't check index 2 for a P.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #7 on: April 26, 2012, 07:56:26 pm » |
.. to use an array of 3 chars (char c[2] In fact, even that should give you pause. An array of 3 chars you say? char c [3];
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
God Member
Karma: 0
Posts: 596
Arduino rocks
|
 |
« Reply #8 on: April 27, 2012, 01:24:23 am » |
Isn't an initialization step missing ? Something like "if numCharsReceived >=3 <code here> else <do nothing>".
Or maybe the fact that c[] has random contents is enough to cover this case ?
|
|
|
|
|
Logged
|
|
|
|
|
Camberley, Surrey, UK
Offline
Newbie
Karma: 0
Posts: 7
Arduino rocks
|
 |
« Reply #9 on: April 27, 2012, 04:15:40 am » |
I've changed this to the following now... char c[3] = {0,0,0}; ... is that what you mean?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #10 on: April 27, 2012, 04:58:34 am » |
If your array "c" is a global or a static, the compiler will helpfully set all elements to zero, unless you tell it to set them to some other value. Difficult to tell from snippets though (hint)
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
God Member
Karma: 0
Posts: 596
Arduino rocks
|
 |
« Reply #11 on: April 27, 2012, 05:04:44 am » |
Well, I guess explicitly initializing the array is a simpler way to solve the issue.
|
|
|
|
|
Logged
|
|
|
|
|
|