Show Posts
Pages: [1] 2 3 ... 48
1  Using Arduino / Programming Questions / Re: Help needed for strtok function on: July 30, 2014, 02:05:18 pm
From your questions it appears that you would benefit from a good introductory programming book. My totally biased suggestion is Beginning C for Arduino, available on Amazon.com. There are tons of other books available, so read the reviews on Amazon and make your decision. After you read that book, I think a lot of your questions will become answered. A little investment in time studying now will pay you huge benefits down the road.
2  Using Arduino / Programming Questions / Re: Help for Keyboard inputs on: July 30, 2014, 01:56:21 pm
As an alternative using the Serial object:

Code:
#define MAXIN 50
char input[MAXIN];

void setup() {
  Serial.begin(115200);
}

void loop() {
  float val;
  if (Serial.available() > 0) {
    Serial.readBytesUntil('\n', input, MAXIN);
    Serial.print("input = ");
    Serial.print(input);
    val = atof(input);
    Serial.print("   val = ");
    Serial.println(val);
  }
}

Note that the Serial.print() method rounds the data to 2 decimal places, which may not be good enough for you.
3  Using Arduino / Programming Questions / Re: Help needed for strtok function on: July 30, 2014, 09:30:05 am
This may help explain what AWOL was trying to tell you:

Code:
char message[] = "Qs323=976;456;890;1000;24;876;0;432;0;1000;1000;989;745;0;876;5";
const byte MAX_TOKENS = 20;
const char* delimiters = ",;="; // whatever characters delimit your input string
char* tokens [MAX_TOKENS + 1];
void setup() {
  // put your setup code here; to run once:
    int tokenIndex = 0;
    Serial.begin(115200);
    Serial.println (message);
    tokens [tokenIndex] = strtok (message, delimiters);
    while ((tokenIndex < MAX_TOKENS - 1) && tokens [tokenIndex])
    {
      tokens [++tokenIndex] = strtok (NULL, delimiters);
    }
    for (int i = 0; i < tokenIndex; i++)
    Serial.println(tokens[i]);
}

void loop() {
  // put your main code here, to run repeatedly:

}
4  Using Arduino / Programming Questions / Re: Non linear list and sensor values (that are not Integers) on: July 30, 2014, 09:03:49 am
I agree with GoForSmoke; use an int data type instead of a float and code it with a fixed-point notation. That is, if you need to store the temperature of 98.6, store it as 986 and divide by 10 when you need it. That will save you two bytes for every temperature.
5  Using Arduino / Programming Questions / Re: float switch loop code help on: July 29, 2014, 12:29:51 pm
One of the things that will hurt you here on the Forum is not reading Nick Gammon's first two notes on the proper way to post on this Forum. The regulars here forgive the first one or two posts, but 13 is too many for not having read the rules. Read those sticky notes, come back to this post (readers don't like double-posts), and resubmit your question with the proper format.
6  Using Arduino / Programming Questions / Re: how to cast int from int[] to const char* on: July 29, 2014, 08:28:56 am
I agree with AWOL. Also, even if it did work, it seems like you're trying to print the ASCII value for 0 and 1, which are non-printing. Maybe this will help you see what's going on:

Code:
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  int msg[10] ;
  msg[0] = 65;      // ASCII for 'A'
  msg[1] = 66;      // 'B'

const char* a = (char *) &msg[0];     
const char* b = (char *) &msg[1];

Serial.println(a);
Serial.println(b);
}

void loop() {
  // put your main code here, to run repeatedly:

}

Also, the statement

Code:
int[10] msg;

is not a proper definition in C.
7  Using Arduino / Programming Questions / Re: Serial LCD, sending special commands on: July 29, 2014, 08:06:13 am
I don't know exactly how your sensors work, but reading the code is not as clear as it could be. My guess is that the following code would NOT work, but it may be easier to read. I'm offering it as an alternative to getting your code to do what youo are trying to do.

Code:
void loop()
{
  unsigned int keyCommand;

  while (Serial.available() > 0) {
    inKey = Serial.read();
    keyCommand = inKey & 0xFF;
  }
  // Check for special LCD command
  if (keyCommand == SPECIAL_COMMAND) {
    SpecialCommands();
    // Backlight control
  } else {                            // Other commands...
    while (Serial.available() == 0)
      ;
    inKey = Serial.read();
    keyCommand = inKey & 0xFF;
    switch (keyCommand) {
      case BACKLIGHT_COMMAND:
        setBacklight(Serial.read());
        break;

      case BAUD_COMMAND:
        setBaudRate(Serial.read());
        break;

      default:
        Serial.println("Should be here");
        break;
    }

    switch (inKey) {
      case 8:          // Backspace
        Cursor--;
        LCDDisplay(0x20);
        Cursor--;
        break;

      case 9:          // Horizontal tab
        Cursor += 5;
        break;

      case 10:         // Linefeed
        Cursor += columns - Cursor % columns;
        break;

      case 13:         // Carriage return
        Cursor += columns;
        break;

      default:         // Regular character
        LCDDisplay(inKey);
        break;
    }
  }
}
8  Using Arduino / Programming Questions / Re: How to find number of "rows" in an array of strings? on: July 28, 2014, 11:53:02 pm
@GoForSmoke: For the Arduino platform, I don't know why one would risk using the String class in lieu of char arrays, other than for convenience
9  Using Arduino / Programming Questions / Re: New to Arduino - help converting float on: July 28, 2014, 11:16:51 pm
I'd have to look at the code for the Serial object's println() method, but it appears that it rounds to the nearest digit in a default field of 2 decimal places for floating point numbers.
10  Using Arduino / Programming Questions / Re: New to Arduino - help converting float on: July 28, 2014, 11:01:52 pm
When I pass 3.13 to dtostrf() in my sample program, it comes back as 3.130000. I would suggest that you print out the value of both sensorValue and voltage to see what their values are.
11  Using Arduino / Programming Questions / Re: How to find number of "rows" in an array of strings? on: July 28, 2014, 10:55:06 pm
@michinyon. Writing the code as:

Code:
sizeof( myString ) / 2

is not good style. First, it uses a magic number, 2. Someone reading the code might wonder why the 2 is there. Second, it's not portable. Take the expression to a 32 bit processor it fails. Writing the expression as:

Code:
sizeof(myString) / sizeof(char *)

is preferred because: 1) it doesn't use a magic number, 2) it's self-documenting as to the data type being used for the second operand, and 3) it's portable across platforms with different pointer sizes.
12  Using Arduino / Programming Questions / Re: programming for LCD Module 1602, I see this error. on: July 28, 2014, 10:47:15 pm
First, the C preprocessor isn't going to like having a space between the '#' and the keyword include. It should be #include with no spaces. Second, you've also put in spaces between the function name and its opening parentheses, as in lcd (0x27, 16,2). You did this with the other function calls, too (e.g., setup () and loop ()). Get rid of the spaces. You also added an extra space between the method name and its opening parentheses, as in all the lcd calls (e.g., lcd.init ()). Get rid of the extra space in front of the opening parentheses. Finally, you have an extra space between the comment characters: //. get rid of them.

Now try and compile it.
13  Using Arduino / Programming Questions / Re: New to Arduino - help converting float on: July 28, 2014, 10:27:26 pm
@joelzyla: floating point numbers have a number of limitations. First, a float for the Arduino IDE is a 4 byte value, so its accuracy only extends to the first 6 or 7 digits even though it can "represent" a number with 38 digits. Alas, the first 6-7 are probably spot on and the remaining 32 are the computer's best approximation of the actual value.

Second, some numbers cannot be represented accurately in binary, such as 1/3. If you don't want to exceed a float's precision in your answer, you can specify what you want with the width and precision parameters that are passed to dtostrf(). Unfortunately, the choice of terms is a little misleading for the function. In this case, width refers to the minimum field width of the number being represented while precision is the number of places after the decimal point.
14  Using Arduino / Programming Questions / Re: How to find number of "rows" in an array of strings? on: July 28, 2014, 07:18:53 pm
Quote
p.s. you know you write a lot of code when you type using a semicolon as a period.

Opps...
15  Using Arduino / Programming Questions / Re: How to find number of "rows" in an array of strings? on: July 28, 2014, 06:29:17 pm
No, sizeof() returns the number of bytes allocated to the object. For your calculation to tell how many elements there are, you need to use:

Code:
number  = sizeof(myString) / sizeof(char *);

Each element of the array is a pointer, not a string. Therefore the first sizeof() expression returns the total number of bytes allocates for all of the pointers (e.g., 8 bytes). To get the element count, you need to divide by the size of one element, which is the size of a single pointer (e.g., 2 bytes);
Pages: [1] 2 3 ... 48