Show Posts
Pages: 1 2 [3] 4 5 ... 32
31  Using Arduino / Programming Questions / Re: function pointer & memory selection on: April 08, 2014, 08:52:56 am
void pointers pose certain problems and are usually used to get around the compiler complaining about a missing type specifier for a pointer. If you want to write a "generic" function that returns a pointer, you can do something like this:

Code:
void setup() {
 //Initialize serial and wait for port to open:
  Serial.begin(115200);

}

void *myGeneric() {
   // Do something
}

void loop() {
  int *ptr;
  long *lPtr;
 
  ptr = (int *) myGeneric();
  lPtr = (long *) myGeneric();

}

The casts on the return value is required so that the scalar associated with the data type of the pointer is known.
32  Using Arduino / Programming Questions / Re: Running out of Ram ? on: April 08, 2014, 08:41:33 am
You might sprinkle calls to freeMemory() at different point in loop() to see if that's your problem. Check out:

http://playground.arduino.cc/Code/AvailableMemory

33  Using Arduino / Programming Questions / Re: function pointer & memory selection on: April 08, 2014, 07:03:49 am
I'm not sure exactly what the code is trying to do, but I changed the line:

Code:
        pMemory = &memory_System.bSlidingSwitch_Left;

to

Code:
       
       pMemory = (word *) &memory_System.bSlidingSwitch_Left;

and the code compiles. The error is caused because, without the cast, there is a type mismatch between the two data types. Many compilers don't complain on a "silent cast", because you're attempting to place a smaller data type (boolean) into a larger data type (word). That is, you're pouring one byte of information into a two-byte bucket. Any reasonably good compiler will complain on assignment in the other direction (pour two bytes of information into a one-byte bucket, since that runs the risk of losing information).
34  Using Arduino / Programming Questions / Re: Array Basics (sizeof) on: April 07, 2014, 11:44:14 pm
My guess is that we are talking to cross purposes. If an array name is used as the argument in a function call, you're saying its a pointer to the first element. I agree because that is also the definition of the lvalue of the array. That is, array and &array[0] are the same thing. Once I know that, I can use the array in the function body since I know where it is stored in memory.  You can see that using the following code fragment:

Code:
  int array[10];
  Serial.print("array = ");
  Serial.print((int)array);
  Serial.print("   &array[0] = ");
  Serial.println((int)&array[0]);

That's all I was trying to say: Given the proper type specifier for an array passed to the function, you get the memory address of the array (lvalue), which equates to the name of the array,  which allows you to reference the array in the function.
35  Using Arduino / Programming Questions / Re: Array Basics (sizeof) on: April 07, 2014, 09:42:43 pm
I was referring to the statement that ...it is not a reference to the array or a pointer to it..  We were talking about array names being passed to a function.
36  Using Arduino / Programming Questions / Re: Array Basics (sizeof) on: April 07, 2014, 09:19:21 pm
I disagree. You state:

...it is not a reference to the array or a pointer to it. Passing the array name without subscript operators decays the array type to a pointer of its first element, nothing to do with the original array.

Even your own quote says it's a pointer. If it has nothing to do with the original array, then why is the type of the pointer required as part of the parameter being passed?  The reason is because pointer operations are scaled to the data type being pointed to. Further, if it has nothing to do with the array, then why is the value passed the lvalue of the array? Indeed, it has everything to do with the array, otherwise the function wouldn't have a memory address to allow any operation to be performed on it. Indeed, if I pass in an array name and pass additional parameters for its length and rank, I can reference the array in a way that matches its original definition.

37  Using Arduino / Programming Questions / Re: Array Basics (sizeof) on: April 07, 2014, 05:53:32 pm
sizeof() is not a function, it's an operator. In fact, you don't need to use the parentheses.

There is no way a function can know the size of the array because arrays are passed to a function by reference. That is, the function receives the memory address (lvalue) of where the array resides in memory. The only possible exception is a null terminated character array that's treated as a string.
38  Using Arduino / Programming Questions / Re: Data type comparison on: April 05, 2014, 02:02:00 pm
My guess is that the Check_SMS() function fills in the sms_rx[] character array. If that's the case, you need to compare strings, which won't work using sms_rx == ON. You'd have to do something like:

Code:
if (strcmp(sms_rx, "ON") == 0) 
{
digitalWrite(13, HIGH);
} else {
    if (strcmp(sms_rx, "OFF") == 0)
   {
digitalWrite(13, LOW);
   }
}

strcmp() returns 0 when there is a match, -1 if the match fails and the letter of the mismatch is lexicographically less, or 1 if greater. For example, an ASCII 'A' is 65 and ASCII 'B' is 66, so strcmp("A", "B") would return -1 while strcmp("B", "A") would return 1, and strcmp("A", "A") or strcmp("B", "B") would return 0.
39  Using Arduino / Programming Questions / Re: Problem with serial.print(F("A lot of times anything")) on: April 05, 2014, 10:51:01 am
Try looking at  calls to freeMemory() in loop() to see if you're hitting its max:

Code:
int freeMemory() {
  int free_memory;

  if ((int)__brkval == 0) {
    free_memory = ((int)&free_memory) - ((int)&__heap_start);
  } else {
    free_memory = ((int)&free_memory) - ((int)__brkval);
    free_memory += freeListSize();
  }
  return free_memory;
}
40  Using Arduino / Programming Questions / Re: Increasing a Loop value on: April 04, 2014, 11:02:22 pm
I didn't test this, but it should give you an idea...

Code:
int yTarget ;      // Assume 18 passes in Y direction
int x;
// setup()

void loop()
{
   while (yTarget < 90) {
      for (x = 0;x < 180; x++) {
         myservo1.write(yTarget);     // tell servo to go to position in variable 'pos'
         delay(150);
         myservo2.write(++x);
      }
      yTarget += 5;
   }
}
41  Using Arduino / Programming Questions / Re: Need guidance on programming technique on: April 04, 2014, 03:15:14 pm
Often new students know what they want to do, but don't know where to start...the old How Do You Eat An Elephant problem. I find it useful to think in terms of 5 program steps:
   1) Initialization -- what has to be done before the user (system) sees anything. e.g., read configuration data from
         EEPROM,open DB connections, initialize ports, etc. (setup())
   2) Data input -- do whatever needs to be done to get and verify the data
   3) Process -- almost all programs take data in one form and change it to some other form
   4) Output -- could be a display, but may be writing data to an SD card, altering a heater, etc.
   5) Termination -- clean up after yourself, often undoing what was done in Step 1. Not always needed in
         system programming.
Once that's done, I do a sideways refinement, which is a lot like what was mentioned in terms of pseudo code. If you do this first, the program almost writes itself. It is possible to get too carried away and fall into the old BDUF (Big Design Up Front) issues where you spend six month writing a design which lives for about 2 days, hence Agile technologies. Still, a little forethought goes a long way towards writing better code, faster.
42  Using Arduino / Programming Questions / Re: A theoretical question on: April 04, 2014, 06:36:15 am
Quote
...doesn't it imply...

May well be true, but basing code on implication is almost never a good idea. Explicitly using statements, if nothing else, documents what you want to do.
43  Using Arduino / Programming Questions / Re: Why does my wave form generator using interrupts max out at 3.8KHz. on: April 03, 2014, 09:03:32 pm
@Peter: Each waveform takes 128 bytes, processing it's a fairly long process and therein lies the rub. I think I'll have to live with the cutoff.

 Thanks, all, for your help.
44  Using Arduino / Programming Questions / Re: Serial Echo code breakdown [beginner] on: April 03, 2014, 02:35:23 pm
The expression:
Code:
void process_data (const char * data)
is what is called a function signature and is used by the compiler to identify a function. (If it had a semicolon at the end of the line, it would be called a function prototype.) The word void is called the function type specifier. Every function has the ability to return a value to whatever invoked that function. The word void for this type specifier simply means that this function does not return a value.

Next, process_date is simply the name of the function.

Next, the const keyword means that whatever follows is to be etched in stone and cannot be changed. When used in parentheses following a function name, it means that the data item passed into this function cannot be changed by the function.

Next, the char * data is the actual data being passed into the function. Using Purdum's RIght-Left Rule, you can verbalize this data item as: "data is a pointer to char".  Pointers are often difficult for new programmers to understand, but worth knowing. A valid pointer can have only two possible values: 1) the memory address of a data type, or 2) null (binary 0, or '\0'). What we are saying here is that, if it is a valid, non-null pointer, it points to a memory address where a character named data lives.

Hope that helps.
45  Using Arduino / Programming Questions / Re: Why does my wave form generator using interrupts max out at 3.8KHz. on: April 03, 2014, 09:59:16 am
Mornin' Peter: I'm away from my system and I don't know how long a read takes from program memory.  I'll investigate that as soon as I can. It may well be that I simply don't have the horsepower to do what I'm trying to do. Thanks...
Pages: 1 2 [3] 4 5 ... 32