Pages: [1]   Go Down
Author Topic: Moving array variables  (Read 1201 times)
0 Members and 1 Guest are viewing this topic.
Camberley, Surrey, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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...

Code:
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 Offline
Sr. Member
****
Karma: 4
Posts: 329
KC4MM
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you're missing a } at the very end
Logged

Camberley, Surrey, UK
Offline Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-confuse
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I thought the most efficient way to do this was to use an array of 3 chars (char c[2]smiley-wink 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 Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You absolute star!  Thank you :-)
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4836
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why should there have to be a NULL, Paul? I see no string or print commands.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19340
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

... use an array of 3 chars (char c[2]smiley-wink and look for my byte based on the bytes either side...

Code:
  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

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19340
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

.. to use an array of 3 chars (char c[2]smiley-wink

In fact, even that should give you pause. An array of 3 chars you say?

Code:
char c [3];
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

0
Offline Offline
God Member
*****
Karma: 2
Posts: 596
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 0
Posts: 7
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've changed this to the following now...
Code:
char c[3] = {0,0,0};
... is that what you mean?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26627
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
God Member
*****
Karma: 2
Posts: 596
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, I guess explicitly initializing the array is a simpler way to solve the issue.
Logged

Pages: [1]   Go Up
Jump to: