Show Posts
Pages: [1] 2 3 ... 1308
1  Using Arduino / Programming Questions / Re: const int and memory space on: Today at 02:20:08 am
Code:
void setup ()
  {
  Serial.begin (115200);
  Serial.println ();

  Serial.print ("using literal:   ");
  Serial.println (5ul);
  }  // end of setup

void loop () { }

2264 bytes. smiley

Code:
  Serial.println (5u);

Gives 2278 bytes.

But who writes code like that?

Quote
How many kids do you have?

Response:

Quote
5 (unsigned).
2  Using Arduino / Programming Questions / Re: const int and memory space on: Today at 01:13:32 am
If there are no indirect references to the "const" then the compiler is free to optimise them away...

What's an indirect reference to a const? Can you give an example?
3  Using Arduino / Programming Questions / Re: Push button timer and LCD readout sketch needed on: Today at 01:06:17 am
skaru= as on south park when Eric Carlton says screw you.  he says it in a ska ru yu fashion.

How to use this forum

Quote
Be polite
4  Using Arduino / Programming Questions / Re: const int and memory space on: September 01, 2014, 03:53:56 pm
From the other thread:

True, side effect bugs can be difficult to track down, but I still want to have "typeless" macros from time-to-time. For example:

Code:
#define ARRAYELEMENTCOUNT(x) (sizeof(x) / sizeof(x[0]))

can determine the size of any defined array regardless of type. As to the side effects on this macro, it would be pretty unusual to want to modify a constant lvalue.

There are always templates:

Code:
// number of items in an array
template< typename T, size_t N > size_t ArraySize (T (&) [N]){ return N; }

Example:

Code:
int foo [] = { 42, 56, 78, 34, 11 };

// number of items in an array
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
template< typename T, size_t N > size_t ArraySize (T (&) [N]){ return N; }

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();
  Serial.print ("using #define:  ");
  Serial.println (ARRAY_SIZE (foo));
  Serial.print ("using template: ");
  Serial.println (ArraySize (foo));
   }  // end of setup
void loop () { }

Output:

Code:
using #define:  5
using template: 5

If you comment-out one and then the other method the generated code size is the same. (2278 bytes on the Uno).



Rather interestingly though, this uses more program memory (2380 bytes):

Code:
int foo [] = { 42, 56, 78, 34, 11 };

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

  Serial.print ("using literal:   ");
  Serial.println (5);
  }  // end of setup

void loop () { }

To get back to 2278 bytes I had to change the println line to read:

Code:
  Serial.println ((size_t) 5);

Now most people wouldn't think to do that. The conclusion is that salting literal values through your code not only makes it harder to read and debug, but may even use more space rather than less.
5  Using Arduino / Programming Questions / MOVED: Audio (yes, I know) on: September 01, 2014, 01:32:26 am
This topic has been moved to Arduino Due.

http://forum.arduino.cc/index.php?topic=264266.0
6  Products / Arduino Due / Re: Audio (yes, I know) on: September 01, 2014, 01:32:11 am
Ah, in that case we have a Due section of the forum.
7  Products / Arduino Due / Re: Audio (yes, I know) on: September 01, 2014, 01:17:20 am
I'm guessing, but you haven't said, that you have a Mega, judging by the pin numbers.

I'm guessing, but you haven't said, that it is hanging in some way.

Code:
int timclk = 8000;
      outgoingAudio = "";
     
      while (timclk > 1) {
 ...
      outgoingAudio += String(incomingAudio);
      outgoingAudio += ",";
      timclk = timclk - 1;
      delayMicroseconds(25);
      }

The Mega has 8K of RAM so there is no way you will fit 8000 samples into RAM, even if String stores things efficiently, which it doesn't. For one thing you are putting 8000 commas there. smiley-wink
8  Using Arduino / Programming Questions / Re: Global Variable Not Initializing? on: September 01, 2014, 12:32:26 am
Code:
    // User selected option "a" - Measure Temperature
    if(incomingByte == 97)

It's much easier to read the code if you put the literal characters there:

Code:
    // User selected option "a" - Measure Temperature
    if(incomingByte == 'a')

Ditto for elsewhere.
9  Products / Arduino Due / Re: Audio (yes, I know) on: September 01, 2014, 12:29:06 am
Post all of your code please, in code tags. Not seeing the data types makes it hard to comment. Plus "not achieve anywhere near my desired effect" is very vague. In what way?

How to use this forum

If the code is long you can add an attachment. (see link above)
10  Using Arduino / Programming Questions / Re: const int and memory space on: September 01, 2014, 12:25:47 am
In other post, a user suggest that doing this:
Code:
const byte ROWS = 4;
const byte COLS = 4;  
char keys[ROWS][COLS] =
...

wastes data memory space, and so, is better do:
Code:
char keys[4][4] =

Whoever that other user is, is completely wrong. Using const like that is the same as using the actual number and is much better documentation. If you hard-code "magic" numbers like [4] [4] and later decide to change one of them to 5, it is easy to overlook other parts of the code which rely on it being 4. And particularly in this case, if you change it to [5] [4] then some of the "4" in the rest of the code will need changing and some not.
11  Using Arduino / Programming Questions / Re: After set a watchdog,the mcu died.I can't reset it though the Reset pin on: August 31, 2014, 02:40:56 am
What does that do? To find the reset reason you need to save MCUCR before you do virtually anything else.
12  Using Arduino / Programming Questions / Re: Thankyou on: August 30, 2014, 07:43:25 pm
I know there isn't the "best" way to use the braches but the latter saves a line for each block of code

Are lines expensive?
13  Using Arduino / Programming Questions / Re: do while in an interrupt on: August 30, 2014, 07:35:28 pm
I'm just curious about why adjusting the timer value can't work. Whether a constant or something you work out at runtime, you should be able to tweak the timer value.

Theory: Timer 1 is a 16 bit timer, so it can count up to 65536 (technically 0 to 65535).

The interval depends on the prescaler, from which you can choose: 1, 8, 64, 256, 1024.

Assuming you are running the processor at 16 MHz, that translates to a precision of:

Code:
Prescaler  Period (µS)
    1        0.0625
    8        0.5
   64        4.0
  256       16.0
 1024       64.0     

Given that you are trying to time 1/5 of a second (200 mS) and the timer can count up to 65536 then you would need a prescaler of 64. That is:

Code:
Count to 50000 * 4 µS = 200000 µS (200 mS)

With a prescaler of 64 you can then tweak that to +/- 4 µS at a time. So for example, if it is 4 µS too slow you count up to 50001.

Now your original post gave the impression you were dealing with much larger amounts (you quoted 500 µS) so it would seem reasonable that you could get it to within a couple of µS accuracy just by changing the count.


14  Using Arduino / Programming Questions / Re: do while in an interrupt on: August 30, 2014, 05:46:30 pm
But the clock on the Arduino is not perfectly accurate (+/- 0.2%). To compensate, I want to measure exactly how far off the clock is, and then in the timer interrupt routine, wait the appropriate number of microseconds before setting the digital output high.

I should also point out that there is jitter with interrupts. As explained on my page ( http://www.gammon.com.au/interrupts ) an interrupt may be delayed a bit (especially if another interrupt is currently running). So for example if a Timer 0 interrupt is being serviced, and say that takes a few microseconds, attempting to compensate for Timer 1 by a few microseconds is probably not going to help.

If you stick to letting the timer itself generate the signal (which you can do) then no interrupts are necessary and it will be accurate as it can be, bearing in mind the resolution of the interrupt and the processor clock accuracy.

http://www.gammon.com.au/forum/?id=11504
15  Using Arduino / Programming Questions / Re: After set a watchdog,the mcu died.I can't reset it though the Reset pin on: August 30, 2014, 05:31:15 pm
As explained here: How to use this forum

<sigh>

Scroll down to:

Quote
8. You can add attachments
Pages: [1] 2 3 ... 1308