Show Posts
Pages: 1 2 [3] 4 5 ... 48
31  Using Arduino / Programming Questions / Re: #define - compiler-linker precedence on: July 25, 2014, 09:02:33 am
...Pascal, FORTRANand COBOL, well now we're talking.

Yep...that's what we need: A good COBOL compiler for the Arduino!
32  Using Arduino / Programming Questions / Re: Why My Program Does Not Exit from If statements on: July 25, 2014, 08:05:36 am
@omersiar: Just a couple of thoughts: 1) once you get the code to compile, place the cursor in the Source Code Window and press Ctrl-T to reformat it. It will make it easier to read. 2) Use the menu sequence Edit --> Decrease Indent to adjust the number of spaces used when indenting a line. My preference is 2 spaces, which is enough to see the indent, but not so much that I'm continually using the horizontal scroll bar.
33  Using Arduino / Project Guidance / Re: stp16cp05 on: July 24, 2014, 06:32:54 am
Hi, and welcome. Make sure you read the first two posts on this Forum written by Nick Gammon. It will give you a good idea of how to use this Forum effectively.

Second, your topic's tag line should be as descriptive as possible, so we can see what your problem is just by reading that. Your tag line for this post was not descriptive enough to draw peoples' attention to it.

Third, you're not going to get too much response if it is obvious that you haven't tried to solve the problem yourself. For example, it took me about a minute to find this resource:

which may be helpful. Always at least do a Google search before you post a question here. There are a lot of very talented people who read this Forum on a regular basis and they will bend over backwards to help someone who at least has demonstrated they have tried to find the answer themselves. On the other hand, if they feel that you haven't tried to answer the question yourself, your request may well go unanswered.
34  Using Arduino / Programming Questions / Re: Comparing whole arrays? on: July 23, 2014, 03:33:44 pm
@jimlee: Why make numItems and i a long when the native size is 16 bits?
35  Using Arduino / Programming Questions / Re: Comparing whole arrays? on: July 23, 2014, 03:31:14 pm
That only compares their SIZE not cells.  I want to see if one array is a copy of another array.

As robtillaart points out, it does a memory compare and can only come back with 0 on a perfect match. As for the other alternatives that have been offered, they essentially do the same thing, only they control the loop that walks through the elements. When I had my compiler company, all of the mem*() functions were hand-tweaked assembler and I doubt that a compiler would improve on them either in terms of code size or speed (although they were optimized for speed). As a general rule, it makes no sense to reinvent the wheel so memcmp() should do the trick. While some others rely on your fixed array sizes, my code doesn't make that assumption and that's often not a bad idea in the long run. Coding for fixed sizes has a way of coming back to haunt you later on.
36  Using Arduino / Programming Questions / Re: Comparing whole arrays? on: July 23, 2014, 02:21:42 pm
Another way is to use memcmp(). First, test to see if the sizes are the same:

int disarmCode[4] = {1,2,3,4};
int tempArray[4] = {1,2,3,4};

int oneElementSize = sizeof(disarmCode[0]);

if (sizeof(tempArray[0] == oneElementSize) {        // Are the sizes of the elements the same?
   if (sizeof(tempArray) == sizeof(disarmCode)) {   // Are the number of elements the same?
      if (memcmp(tempArray, disarmCode, sizeof(tempArray) * oneElementSize) == 0)
        Serial.println("They match");

I haven't tested this code, but I think it would work.
37  Using Arduino / Programming Questions / Re: Converting hex expressed as string into integers on: July 23, 2014, 01:54:53 pm
I thought of the hexToDecimal() code after I had already written the decimalToHex() code and was too lazy to rewrite it.
38  Using Arduino / Programming Questions / Re: Converting hex expressed as string into integers on: July 23, 2014, 01:33:41 pm
Give this a try:

void setup() {
  int val = 140;
  char buffer[20];

  decimalToHex(val, buffer);

  Serial.print("decimal 140 in hex is: ");
  Serial.print("First hex digit value: ");
  Serial.print("    Second hex digit value: ");

void loop() {

  Function that converts decimal value to a hex string
  Argument list:
    int n;        the decimal value to convert
    char hex[]    the buffer to hold the conversion
  Return value:
  Caution: This function assumes the buffer is large enough to hold
  the output.


void decimalToHex(int n, char hex[])
  int i = 0, rem;
  while (n > 0)
    rem = n % 16;
    switch (rem)
      case 10:
        hex[i] = 'A';
      case 11:
        hex[i] = 'B';
      case 12:
        hex[i] = 'C';
      case 13:
        hex[i] = 'D';
      case 14:
        hex[i] = 'E';
      case 15:
        hex[i] = 'F';
        hex[i] = rem + '0';
    n /= 16;
  hex[i] = '\0';
  strrev(hex);   /* Reverse string */

  Function that converts hex digit to a decimal value
  Argument list:
    char hex       the hex digit to convert
  Return value:
int hexToDecimal(char hex)   /* Function to convert hexadecimal to decimal. */
  hex = toupper(hex);
  int temp = hex - '0';
  if (temp < 10)
    return temp;
    return  hex - 'A' + 10;
39  Using Arduino / Programming Questions / Re: Using an array in a switch case on: July 23, 2014, 01:00:47 pm
The expression controlling a switch must resolve to an integral value. The code you posted wouldn't work because you are using string data which do not resolve to an integral data type. You could use:

  char states[4][6] = {"Dud", "Win", "Lose", "Accel"};
  int index;
  // some code that determines the proper index to use...

  char val = states[index][0];

  switch (val) {
    case 'D':
      //do nothing
    case 'W':
      // user wins
    case 'L':
      //user loses
    case 'A':
      //timer accelerates
      Serial.println("I shouldn't be here.");

I also find it useful to always have a default case in a switch expression.
40  Using Arduino / Programming Questions / Re: const int / int on: July 23, 2014, 12:50:07 pm
A string constant has a type of "array of characters" and so decays to a pointer to its first element when used as in AWOL's example.
Perhaps a difference in point of view. First, AWOL's example doesn't use any arrays, so I fail to see how it is decaying into a pointer. Second, you are equating a memory address with a pointer. They are not the same. A memory address is just that...a memory address and its size is fixed by the host machine: 16 bits on an Arduino but 32 bits on other controllers. A pointer, however, always has a scalar as part of its attribute list. A pointer to int has a 2-byte scalar while a pointer to long has a 4-byte scalar. The scalar attribute is necessary to perform proper pointer arithmetic.

@PeterH: I don't view it that way. In the statement:


int k = 10;

what is the rvalue of b? The lvalue tells me where to find b in the memory map, while the rvalue tells me the value of what the bytes allocated to b hold at that address. In the second expression, k still has an lvalue where it is located in memory, but it also has an rvalue even though it's on the left side of the assignment expression. Until the value 10 is moved to k's lvalue, the rvalue of k is whatever random pattern of bits happens to exist at those two bytes of memory. All defined data items have an lvalue and an rvalue regardless of there they appear in an expression.
41  Using Arduino / Programming Questions / Re: Converting hex expressed as string into integers on: July 23, 2014, 09:23:57 am
Given that you said that the 8-bit int named rotaryvalue may be equal to or less than 128

dimmerlevel = 128 + rotaryvalue;

what happens when rotaryvalue is 128? This is a bug waiting in the weeds to bite you in the butt down the road.

Also, while scanf() (and its friends) makes life easy, it is often an H-bomb-to-kill-an-ant approach. If memory's not an issue, it's great. However, if you're banging up against a memory limit, writing a simple function to do the conversion you're looking for might be an alternative approach.
42  Using Arduino / Programming Questions / Re: const int / int on: July 23, 2014, 09:12:24 am
@KeithRB: AWOL's example is not really using a pointer. A true data item has an lvalue (an address where it lives in memory) and an rvalue (what is contained at that address). Simple assignment statements:

int a = 10;
int b;

b = a;

are rvalue to rvalue exchanges. The same is true in a function call:


AWOL's example simply places the address of operator (&) in front of the variable to tell the compiler not to use the rvalue of the data item, but rather the lvalue in the call to the Serial object. It has nothing to do with pointers.

It also should be pointed out that a #define is typeless whereas a const is always tied by its definition to a data type.  While a #define does give up the safety of type checking, there are some cases where this can make a data structure more flexible.

Finally, to the OP, most programmers will use all cap letters with both #define and const as a tip-off that the data item is "different" than other variables.
43  Using Arduino / Programming Questions / Re: How can I send and recive multiple values over serial? on: July 22, 2014, 09:08:10 am
If you are using Visual Basic to send the serial data, type in the data packet into a textbox and then append a newline character ('\n') to the data before sending it to the Arduino. Then use the Serial.readBytesUntil() method to read the data into your input[] array until the newline is read. Then break it apart as you wish.
44  Using Arduino / Programming Questions / Re: arduino serial erratic behaviour missing characters on: July 22, 2014, 06:33:39 am
I commented out the calls to the Adafruit library and ran the code just fine. However, if I included the angle brackets ("<", ">") as part of the string, the parseint method fails. I don't know if you're doing that or not, but your post says you are. Remove the brackets and see what happens.
45  Using Arduino / Programming Questions / Re: reset static variable on: July 20, 2014, 09:26:54 pm
Yet another example of why posters need to post all of their code at the outset.
Pages: 1 2 [3] 4 5 ... 48