Show Posts
Pages: [1] 2 3 ... 58
1  Using Arduino / Programming Questions / Re: Reading strings from Serial on: September 19, 2014, 08:00:08 am
The other posters have pointed out some errors/improvements. In addition, get rid of the String class. Note that if you use the keyword String in your code, the compiler is forced to call in the String class, which can bloat your code unnecessarily. Replace the String with a simple char array. Compiling your code required 3922 bytes while the alternative below uses 2228 bytes. Most of the difference is the bloat from the String class.

Code:
 
char inMessage[25];          // Assume max chars expected is 24, with room for null terminator

void setup() {
  Serial.begin(115200);      // open the serial port at 115200 bps: 
  Serial.println("Power On"); 
}

void loop()
{
  int bytesRead;
 
  inMessage[0] = '\0';                                      // Clear the array
  while (Serial.available() > 0) {
    bytesRead = Serial.readBytesUntil('\n', inMessage, 24); // Read until a newline...
    inMessage[bytesRead] = '\0';                            // Add string terminator
  }
  if (inMessage[0] != '\0') {                               // If something sent, show it...
    Serial.print("received: ");
    Serial.println(inMessage);
  }
}

Also, using the Serial method readBytesUntil() simplifies the input code.
2  Using Arduino / Programming Questions / Re: Converting a SubString to Word on: September 19, 2014, 07:35:30 am
Quote
...but there aren't lots of if/else statements here. In fact, testing sline[12] requires only two of them.
If I'm sure the switch/case block will never expand and there are only two if blocks, I'd probably write the block the way you prefer. If I see that I'm nudging memory limits and I'm using a switch/case, I'd also see if the cascading if block saves the day. That said, after teaching university-level programming courses for almost three decades, I can tell you that a switch/case block results in fewer bugs and, if there is a bug, switch/case is easier to read, making it is easier to isolate and correct than if the bug is in a cascading if block. Given that 80% of software development costs are absorbed in testing and debugging, and assuming that switch/case blocks are a viable alternative, I always prefer switch/case.
3  Using Arduino / Programming Questions / Re: Converting a SubString to Word on: September 18, 2014, 09:11:53 am
Quote
Is switch/case/break "more readable" than few if/else enough to be worth increasing the length of the code ? I don't think so

True, it's a matter of choice. Your code is shorter (1380 bytes) than mine (1396), but I always find a switch easier to read.  I wouldn't expect everyone to agree.
4  Using Arduino / Programming Questions / Re: Converting a SubString to Word on: September 18, 2014, 08:13:53 am
Quote
What happens if the character in position 12 is upper case? What is in test if that is the case?

Opps. And there are still some other potential errors that could happen (e.g., what it the string is only 11 characters long). Correcting those is left "as an exercise for the reader". I think my real reason for the post is to show that it is easier to read a switch than a cascading if block when possible.

Code:
    if (sline[12] >= 'a')                    // Is it lower case?
      test = sline[12] - 32;                 // Convert to upper case
    else
      test = sline[12];
5  Using Arduino / Programming Questions / Re: Converting a SubString to Word on: September 18, 2014, 07:56:28 am
Building on __pepe__ post, it might be easier to read this:
Code:
long val;
char test;

if ( strcmp("setnozzels:", sline)  == 0) {   // We have a match
  if (sline[11]=='0') {                      // The char is a 0 digit character
    if (sline[12] >= 'a')                    // Is it lower case?
      test = sline[12] - 32;                 // Convert to upper case
    switch (test) {
      case 'X':                                 // Hex
         val = strtol(&sline[13], NULL, 16);
         break;
      case 'B':                                 // Binary
         val = strtol(&sline[13], NULL, 2);  
         break;
      case 'O':                                 // Octal
         val = strtol(&sline[12], NULL, 8);
         break;
      default:                                 // Decimal
         val = strtol(&sline[11], NULL, 10);
         break;
    }
}
6  Using Arduino / Programming Questions / Re: Problems with SoftwareSerial and Strings on: September 18, 2014, 07:37:32 am
Along with what Paul and Robin said, check out the code size differences using their suggestions versus using the String class for processing strings.
7  Using Arduino / Programming Questions / Re: serialEvent() string reconstruction and interpreting issue. on: September 17, 2014, 02:08:14 pm
If the message is to be terminated with the newline character, why not use something like

Code:
#define MESSAGESIZE  20   // Whatever the size needs to be
char message[MESSAGESIZE  + 1];   // Room for terminating null

// setup() and loop() code...

// Code in loop()
int charsRead;

while (Serial.available() > 0) {
    charsRead = Serial.readBytesUntil('\n', message, MESSAGESIZE);
    message[charsRead] = '\0';   // Make it a string...
    commandComplete = true;
}
// rest of code
8  Using Arduino / Programming Questions / Re: serialEvent() string reconstruction and interpreting issue. on: September 17, 2014, 12:57:13 pm
Also, we don't know if you're using the String class (often unnecessary) or a character array for strings, but this statement:

Code:
if (Command == ("someAction") {

is not the way to compare strings. Check out strcmp(). Post all of you code so we can see the data types and other structures you are using.
9  Using Arduino / Programming Questions / Re: Help,no matching function for call to 'Adafruit_NFCShield_I2C::readPassiveTarget on: September 16, 2014, 09:43:48 pm
Code:
if(memcmp(uid, goodCard, sizeof(goodCard))){

If uid[] has 7 elements and goodCard[] has 4 elements, the chance that the memcmp() returns 0 is pure serendipity. While I don't have the library, it appears that the compiler is expecting 3 arguments on the call to readPassiveTargetID(), but your code only supplies two. Are you sure you have the right method?


[my edit]
Nope...I reversed the element counts in my head. The memcmp() is okay, but only examines the first 4 bytes of the uid[] array.
10  Using Arduino / Programming Questions / Re: Irregular time on: September 16, 2014, 03:36:03 pm
Please read the first two posts on this Forum, which tell you the proper way to pose a question to the group. You will get more responses that way. Also:

Quote
I want for each of the values in brackets to decrease every time the code loops.

I don't see any brackets. I see values in parentheses passed to functions. Is that what you mean? Also, using color to highlight areas of interest isn't necessary. You could use:

Code:
// ===== Following lines need help =========

/* the lines of code here... */

// ========== End lines of code needing help ========

Use code tags (explained in the first two posts at the top of this Forum) for posting code.
11  Using Arduino / Programming Questions / Re: Using the contents of a string (char array) as a variable ? on: September 16, 2014, 10:12:28 am
You can read more on complex data definition and Purdum's Right-Left Rule at:

http://jdurrett.ba.ttu.edu/3345/handouts/RL-rule.html

An array of pointers to functions needs to be written as Paul suggested. He's a short demo of how the syntax it works.

Code:

void (*funcPtr[])() = {func1, func2, func3};   // Define an array of pointers to function all of which return void

void func1() {
  Serial.println("Function 1");
}

void func2() {
  Serial.println("Function 2");
}
void func3() {
  Serial.println("Function 3");
}

void setup() {
  Serial.begin(115200);
  for (int i = 0; i < 3; i++)
    (*funcPtr[i])();
}

void loop() {
}
12  Using Arduino / Programming Questions / Re: memcmp() on: September 15, 2014, 03:37:43 pm

Quote
...which is why more correct language uses the more generic "array of characters".

What is a "more correct language"?

This is an Arduino Forum and the underlying compiler is the GNU compiler, so that is my frame of reference. I learned a long time ago that, when someone tries to bend the fundamental premise of the discussion with a "That-may-be-but-I-don't-believe-it" argument, the best thing to do is let them believe whatever they want. Think what you will, I'll stick with the GNU interpretation upon which the Arduino compiler is built.
13  Using Arduino / Programming Questions / Re: memcmp() on: September 15, 2014, 02:33:05 pm
From the GNU C Library:
Quote
Function: int memcmp (const void *a1, const void *a2, size_t size)
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

The function memcmp compares the size bytes of memory beginning at a1 against the size bytes of memory beginning at a2. The value returned has the same sign as the difference between the first differing pair of bytes (interpreted as unsigned char objects, then promoted to int).

If the contents of the two blocks are equal, memcmp returns 0.

It is only the difference between the two memory blocks that is treated as an unsigned char, which is then promoted to an int. I'd agree with your misinterpretation of K&R and the others you cite, but then we'd both be wrong.
14  Using Arduino / Programming Questions / Re: memcmp() on: September 15, 2014, 10:18:19 am
Quote
...after all, the sizeof(char) is defined to be 1.

Really? What about Unicode characters? I still don't think the memcmp() function refers to arrays of chars. It refers to blocks of memory expressed in bytes. (Both the C++ and Linux docs refer to memcmp() as comparing bytes of memory.) Also, since a char is signed quantity, does that mean the memcmp() strips off the high bit and ignores it? I think it is much more descriptive to refer to the blocks as bytes, since those are unsigned entities.
15  Using Arduino / Programming Questions / Re: program error on write to lcd on: September 14, 2014, 09:12:33 pm
@luisilva: As I said, I did not have the library and had no info on it. Personally, I absolutely abhor header files that contain data definitions for class objects. I would also be surprised if the call to the LCDA.CLEAR() method worked since it is defined in the header file in lower case letters.
Pages: [1] 2 3 ... 58