Show Posts
Pages: [1] 2 3 ... 33
1  Using Arduino / Programming Questions / Re: Help optimizing a function on: April 22, 2014, 10:38:34 pm
OK...my code won't work then.
2  Using Arduino / Programming Questions / Re: Including a file with arduino on: April 22, 2014, 10:08:41 pm
If they are library files, they should go in the libraries subdirectory, just below where the IDE is stored. If they are not library files, but source and/or header files that you want included in the project, put them in the same directory where you store the sketch code. You can find out where your sketches are stored by looking at the File-Preferences menu sequence in the IDE.
3  Using Arduino / Programming Questions / Re: Help optimizing a function on: April 22, 2014, 10:03:19 pm
I'm probably not seeing something, but if you are calling the functions to get the max or min value in the array and those values are constantly updated, send the most recent value in and have a variable defined in each with the static storage class:

Code:
float get_array_max_value(float val){
   static long tempMax = -100000F;
 
   if (val >= tempMax)
      tempMax = val;
  return tempMax;
}

float get_array_min_value(float val){
   static long tempMin = 100000F;
 
   if (val <= tempMin )
      tempMin = val;
  return tempMin ;
}

If you are only updating the last value in the array, why read the first 4 values when you know which one is the max value from a previous call? What am I missing?
4  Using Arduino / Programming Questions / Re: get a integer from char* ...? on: April 22, 2014, 02:12:50 pm
Actually, you should use the length returned from the call (len) to set the null. Change the null assignment to:


Code:
msg[len] = '\0';
5  Using Arduino / Programming Questions / Re: Libraries Sometimes Not "Seen" By Compiler on: April 22, 2014, 01:42:16 pm
Makes sense. I didn't get that when I read it, but I can see the advantage of that layout.
6  Using Arduino / Programming Questions / Re: get a integer from char* ...? on: April 22, 2014, 01:33:17 pm
I think it does answer your questions. If you need to know which servo, and it is always the 3rd character, then:

Code:
int servoNumber;
int value;
int len;
char msg[7];

servoNumber = msg[2] - '0';   // Servo number
len = udp.read(msg,7);
msg[6] = '\0';
value = atoi(&msg[3]);        // Value from the read
7  Using Arduino / Programming Questions / Re: Libraries Sometimes Not "Seen" By Compiler on: April 22, 2014, 01:17:20 pm
Quote
That is not my understanding.  User contributed libraries belong in the libraries folder in the folder where programs are stored according to http://arduino.cc/en/Guide/Libraries
First, the document states:
Quote
Starting with version 1.0.5, you can install 3rd party libraries in the IDE.
If you read the examples given there, where they are really placing the library is the same place I mentioned after the base directory. Their example:
Quote
 
  My Documents\Arduino\libraries\ArduinoParty\ArduinoParty.cpp
  My Documents\Arduino\libraries\ArduinoParty\ArduinoParty.h
  My Documents\Arduino\libraries\ArduinoParty\examples

Given that I always place the IDE in its own base directory, using Arduino156 instead of tucking it away in My Documents, we have:
  My Documents\Arduino\libraries\ArduinoParty\ArduinoParty.cpp
which becomes
  Arduino156\libraries\ArduinoParty\ArduinoParty.cpp
on my system. I think my setup just makes it a little easier to find things.
8  Using Arduino / Programming Questions / Re: get a integer from char* ...? on: April 22, 2014, 12:54:44 pm
Every time you press a key on your keyboard, a key code gets sent from the keyboard to the computer. These are called ASCII codes. The keycode for a zero is 48, for 1 it's 49, 2 is 50, and so on. So, assuming msg[3] is the character '2', the expression:

Code:
msg[2] - '0'

resolves to:
Code:
msg[2] - '0'
'2' - '0'
50 - 48
2

Therefore, subtracting the ASCII key code for zero from any numeric digit character gives you that character as a numeric.

Finally, give up on the conversions on msg[]. Instead, if you can't assuming the incoming message is a null-terminated character array, try this:

Code:
char msg[7];
int len;

memset(msg, '\0', 7);   // Set all elements to null
len = udp.read(msg,7);
// msg[len] = '\0';           // You could do away with memset() statement if the
                              // read() call does return the message length.
value = atoi(&msg[3]);


You said that the number can never be more than 255, so at most, you need 7 characters: "SR1255", or 6 plus one for the null termination character. The memset() call sets all elements to null, so you know whatever comes in has a null at the end of the array. Get rid of malloc() and free(), they just aren't a real good idea if they can be avoided.
9  Using Arduino / Programming Questions / Re: Libraries Sometimes Not "Seen" By Compiler on: April 22, 2014, 12:23:29 pm
Quite often the compiler doesn't find a library for one of two reasons: 1) the library isn't in the right Arduino subdirectory, or 2) it is in the libraries subdirectory, but the IDE was not restarted after the library was copied.

Problem one is solved by locating the libraries directory off the main directory where you installed the Arduino IDE. So, if you have the IDE installed in a directory named:

Code:
C:\Arduino156

then the proper place to put a new library is in the subdirectory found at:

Code:
C:\Arduino156\libraries

Once you have the new library copied to the libraries subdirectory, you need to reload the IDE. Also note that sometimes the library doesn't expand to the proper library name. That is, you might have a library named SuperHotLibrary100, which unzips to SuperHotLibrary100\SuperhotLibrary100. But when you look in the subdirectories, you find:

   SuperHotLibrary100\SuperHotLibrary100\SuperHotLibrary\
                                                                                                    Examples
                                                                                                    SuperHot.cpp
                                                                                                    SuperHot.h
                                                                                                    keywords

In this case, you want to copy the subdirectory named SuperHotLibrary to the libraries subdirectory of the Arduino IDE, NOT SuperHotLibrary100.
10  Using Arduino / Programming Questions / Re: get a integer from char* ...? on: April 22, 2014, 11:55:17 am
Naw...my comment was meant to be tongue-in-cheek. atoi() expects a null-terminated sequence of characters. So if you want to convert it to a number with an input string of  "SR125" and expect to get 25 back, you would need to use:

Code:
val = atoi(&msg[3]);

which assumes msg is a null-terminated character array. As to my comment, you could use:

Code:
val = ((mess[3] - '0') * 10) + ((mess[4] - '0');

and get the right value, but that seems to be the hard way to do it.
11  Using Arduino / Programming Questions / Re: get a integer from char* ...? on: April 22, 2014, 11:27:58 am
Quote
...2+5+6 as integers...

That works, but you'd need to change it a bit: 2 * 100 + 5 * 10 + 6 * 1 = 256.
12  Using Arduino / Programming Questions / Re: Making an array element a reference to another variable on: April 22, 2014, 10:16:33 am
True enough, the code:

Code:
void setup() {
  Serial.begin(115200);
   int array [12];
   int &x = array [2];

}


does compile, but the code:

Code:
void setup() {
  Serial.begin(115200);
   int array [12];
   int &x = array [2];

   &x = array [2];
}

doesn't.  The syntax works in the assignment statement because you are fixing the allocated memory address at the time of its definition. The last statement won't work because you are trying to change the lvalue of the variable after its been assigned. While your statement works in the assignment, I have difficulty seeing a valid use for that syntax.
13  Using Arduino / Programming Questions / Re: Making an array element a reference to another variable on: April 22, 2014, 09:52:02 am
Some people don't know what lvalue or rvalue means. Think of a diagram with an large inverted "V", with the name of the variable at the point of the V. The leg pointing to the left is the lvalue, which is the memory address of where that variable is located in memory.  The term lvalue probably comes from the assembly language days when a memory address was called its "location value", or lvalue.

The right leg of the V is what is stored at that memory location. This, too, likely comes from assembly language and stood for "register value", or rvalue.

Now take the definition of a variable:

Code:
int x;

and think of it as a bucket. The lvalue tells you where the bucket is located in memory, the rvalue tells you what's inside the bucket, and the data type specifier (e.g., the int keyword) tells you the size of the bucket...big enough to hold two bytes in this case. I developed this into The Bucket Analogy for my books, as it makes it easier to explain casts and pointers. For example:

Code:
int x;
float y = 35000;

x = y;

is a bad assignment because you're taking the contents of a 4 byte bucket and trying to pour it into a 2 byte bucket. You run the risk of overflowing and losing data. Changing the assignment to

Code:
x = (int) y;

using a cast is the correct thing to do even when the compiler doesn't flag it as an error.

Probably an H-Bomb-kill-an-ant post, but perhaps helps to explain what you're seeing.
14  Using Arduino / Programming Questions / Re: Arduino Micro - Move cursor in LCD on: April 22, 2014, 09:30:56 am
You can search this site. One place to start is:

http://arduino.cc/en/Reference/LiquidCrystal

15  Using Arduino / Programming Questions / Re: Compilation error: 'new_8_4' was not declared in this scope on: April 21, 2014, 09:38:51 pm
No, they're not variables, they are missing functions that don't appear to be in the source code. The first compiler error message is on:

new_8_4();

which is a missing function. It, and the others I checked, are not in the source file.
Pages: [1] 2 3 ... 33