Loading...
  Show Posts
Pages: 1 2 [3] 4 5
31  Using Arduino / Programming Questions / Restarting Arduino 'loop()' on: January 18, 2012, 10:09:17 am
Is the Arduino 'loop()' just a 'while(true)' loop in disguise?

I'd like to be able to abort a series of data inputs and restart the main loop(). If it's just an ordinary C/C++ loop in disguise, then 'continue' aught to do it - right?

I guess I could just give it a try, but I thought I'd ask to make sure I was doing 'the right thing'!

Jim
32  Using Arduino / Programming Questions / Re: Const vs #define on: January 13, 2012, 06:42:23 am
I understand that. But it's why #define can be a trap. You wouldn't have the problem with:

Code:
const int FOO = 5;
const int ITEM_COUNT = FOO + 3;

That's exactly why I'm sold on 'const'.

I'm no programmer so I need all the help I can get from preprocessor, without being caught by any 'gotchas' lying in wait!

Jim
33  Using Arduino / Programming Questions / Re: Const vs #define on: January 12, 2012, 05:00:22 pm
Any potential optimisation is up to the compiler, isn't it?

Yes.  And the linker.

Quote
What does the Arduino IDE actually do with const variables (used or otherwise)?

This...

  static const int MagicNumber = 42;

...and this...

  #define MagicNumber  ((int)(42))

...produce exactly the same code in all expressions.  They are interchangeable.

You can exclude static if the symbol is not defined in any other modules (which is almost always the case).


The use of 'const int' (without the 'static') is what I've been using, and what I interpret is approved by the Arduino language reference. As I mentioned earlier, changing 'magic numbers' from #defines to consts gives exactly the same sized code in my (limited) experience.

Jim
34  Using Arduino / Programming Questions / Re: Const vs #define on: January 12, 2012, 02:34:07 pm
Hmm, P138 of my 'Stroustrup' says with regards to macros:

"The first rule is: Don't use them if you don't have to."

and on P140:

"The const, inline and template mechanisms are intended as alternatives to many traditional uses of preprocessor constructs."

Seems pretty clear to me, which is why I asked why people are so 'gung ho' about #defines!

Jim
35  Using Arduino / Programming Questions / Re: Const vs #define on: January 12, 2012, 02:08:30 pm
Thanks for the replies.

I'm pretty sure that when I changed my code to use 'const ints'  rather than '#defines', the compiled code wasn't any larger.

Jim
36  Using Arduino / Programming Questions / Const vs #define on: January 12, 2012, 12:04:55 pm
Although #define is deprecated over const in the Arduino Reference Manual, and in C++ books is stated that there is little need for it, I note that its use is quite common in Arduino code.  Why is this - particularly as there's no overhead in using const?

I ask because I was a confirmed #define user until I got bitten by a bug in a macro. It was such an unpleasant experience that I now avoid it and always use const..

Jim
37  Using Arduino / Sensors / Re: Hall effect module jitter. on: December 27, 2011, 01:03:55 pm
Thanks for the helpful replies.

Jim
38  Using Arduino / Programming Questions / Re: Splitting code into several files? on: December 27, 2011, 07:51:48 am
Thanks 'Luidr' - it looks like it's just what I need to know! Your blog is duly printed and I'm now off to have a coffee and read it!

BTW, how did you know about splitting the file into different tabs? It isn't in the standard works on C and C++. Is it mentioned in the arduino IDE docs somewhere?

Jim
39  Using Arduino / Programming Questions / Re: Splitting code into several files? on: December 26, 2011, 11:54:00 am
Quote
I didn't have to declare them in the pde file where the function originally 'lived', so why (and how) do I have to do it now, please?
You didn't have to because the IDE did it for you. The missing values are defined in WProgram.h or Arduino.h, depending on which version of the IDE you are using. You need to #include one of these files in your .h file.

I wondered if it was something like that. Where in the arduino documentation does it mention this sort of thing - I think I need to get up to speed with it?

Quote
By the way, the #pragma once directive is not the preferred way on the Arduino.

I though this might be so. I guess 'include guards' are the preferred way.

Thanks for the quick reply.

Jim
40  Using Arduino / Programming Questions / Splitting code into several files? on: December 26, 2011, 11:38:10 am
I want to split my project with the functions in separate 'cpp' and accompanying 'h' files files, to make it tidier, but if I take this trivial function example and put it in a separate file, I get errors:

shutter_release.h:
#pragma once;
extern int shutter release_pin;
void release_shutter();
//EOF

shutter_release.cpp:
#include "shutter_release.h"

void release shutter(){
  digitalWrite(shutter_release_pin, High);
  delay(1000);
  digitalWrite(shutter_release_pin, LOW);
  return;
}
//EOF

The errors I get are that digitalWrite, HIGH, LOW and delay are not declared in this scope.

I didn't have to declare them in the pde file where the function originally 'lived', so why (and how) do I have to do it now, please?

In connection with this, can anyone point me to documentation relating to 'splitting' files with reference to the arduino IDE, please.

Jim
41  Using Arduino / Sensors / Hall effect module jitter. on: December 09, 2011, 02:16:05 pm
I'm using this hall effect module to measure current:

http://www.ebay.co.uk/itm/5A-Range-ACS712-Module-Current-Sensor-Module-Special-Price-Sale-/160681653594?pt=LH_DefaultDomain_0&hash=item25695f755a#ht_3056wt_1061

I'm getting quite a lot of jitter in the readings - 10 digits or more. Is this normal?

Assuming it is normal, what do those more experienced in hall effect devices do about it? I'm considering taking 20 (say) readings, storing them in an array and finding the average to obtain the measurement.

Any ideas, please?

Jim
42  Using Arduino / Programming Questions / Re: Moving Return Stack Pointer? on: November 26, 2011, 09:27:34 am
I have a rule of thumb - if something sounds like it is the wrong thing to do, it probably is the wrong thing to do.

Spot on for my (ex) problem!

As I said earlier, I got locked into the idea of an ISR and it got increasingly ugly from there!

Jim
43  Using Arduino / Programming Questions / Re: Moving Return Stack Pointer? on: November 26, 2011, 09:24:49 am
Post your code so we can see what "devils" you are fighting ...

Thanks, but the devils are exorcised from my code now!

Jim
44  Using Arduino / Programming Questions / Re: Moving Return Stack Pointer? on: November 26, 2011, 09:09:27 am
get the ISR to set a flag
test the flag in the main code

The problem is that it seems that the ISR doesn't return to the main code, but to an arbitrary point in the myStepper.step(steps) method., depending on when the switch is operated.

I've gone for a while loop, checking the switch state after every step and it works fine. It's actually quite an easy task, but my brain because locked into the idea of an interrupt!

Thanks for the ideas.

Jim
45  Using Arduino / Programming Questions / Re: Moving Return Stack Pointer? on: November 26, 2011, 07:51:16 am
Don't muck about with stack pointers.
Restructure your code

Thanks for the reply.

Hmm, not the quick and easy answer I was hoping for!

I'm winding a stepper motor driven carriage back to a start position. When it gets past the start position it operates a microswitch which triggers an interrupt. The interrupt service routine then backs the carriage off the microswitch to the final start position, and the code hopefully returning to the main loop.

As I mentioned in my OP, the interrupt service routine returns to the myStepper.step(steps) method, then hangs somewhere in it.

I'm not sure where to start restructuring my code. I thought just losing the return address of the myStepper.step(steps) method would get me back to the main loop just after it was called. A quick, albeit dirty solution!

Jim
Pages: 1 2 [3] 4 5