Show Posts
Pages: 1 [2] 3 4 ... 143
16  Using Arduino / Programming Questions / Re: multiple if or for loop with if inside on: July 28, 2014, 02:28:33 am
Break is a good solution to your stop variable, however as the code is fairly straight forward, we can do it with out any if statements at all:

Code:
char counterTest = 0;
for(int i = 16 ; i && array[i-1] ; --i, ++counterTest );

If you enable C++11 in IDE 1.5.7, you could also do this:
Code:
for( auto i : array ){
  if( i == LOW ) break;  
  ++counterTest;
}
17  Using Arduino / Programming Questions / Re: Losing variable value when adding function to code on: July 24, 2014, 02:18:13 am
It wouldn't matter if the function parameter was named 'm' or 'p', if its not a reference or pointer, you have a copy and therefore are not referring to the same object inside the function.

As the String is global, you can just access it without an extra parameter, however if the function was in a different CPP file you will need to 'pass' it.

By reference:

Code:
int checkFunctions(String &m){
  m += "Hi";
  return 1;
}

Or by passing in a pointer type.
Code:
int checkFunctions(String *m){
  m->concat( "Hi" );
  return 1;
}

Then any changes to the function variable, will be in reference to the global variable.

Call the first version exactly the same as you had it, the second simply needs the strings pointer.

Code:
String m;
checkFunctions( m ); //Reference
checkFunctions( &m ); //Pointer
18  Community / Website and Forum / Re: How do I Start a Poll? on: July 22, 2014, 01:54:56 am
I remember polls being disabled. Is this a permission only moderators have?

I had a look and I cant see any poll button.
19  Using Arduino / Installation & Troubleshooting / Re: Code is not getting burn in Arduino Uno on: July 22, 2014, 01:52:10 am
Its been a common question, search the forum for the string "Avrdude: skt500_getsync(): not in sync" and you'll probably find a few things.

To start off with, do you have anything plugged into pins 0 and 1, if so disconnect those or use different pins. If you need a second serial interface, you can search for examples using the SoftwareSerial over different pins.
20  Using Arduino / Programming Questions / Re: stopping a sketch once completed on: July 21, 2014, 07:43:04 am
There are a few different methods for stopping a sketch explained here: http://arduino.land/FAQ/content/7/47/en/how-to-stop-an-arduino-sketch.html
21  Using Arduino / Programming Questions / Re: #define, int or const int on: July 20, 2014, 10:52:46 pm
I guess nobody else uses enumerators? How odd. I just luvemup!

I do, was just doing a post:

GoForSmoke is correct, an enum is a great way to define constants, which are still symbols, not copy 'n' paste jobs.
C++11 will allow enums to become one of the better choices, as the enum values are no longer in the global scope, which can allow more creative control.

Whats more is they can have their size controlled, there is no behind the scenes conversions of integer literals. ( notice the : uint8_t )

Currently an enum  can take the form:
Code:
enum SystemPins{
  buttonPin = 2,
  lcdPin0,
  lcdPin1,
  lcdPin2,
  lcdPin3,
  ledWarningPin,
};

You could separate the like pins into different enums, however the point is they are all global names.

When the IDE 1.5.8 is released we can do this:
Code:

enum{ ledWarningPin = 2 }; //Normal enum

enum class HID : uint8_t { //Strongly typed enum
  button0 = 3,
  button1 = 4
};  

enum class LCD : uint8_t {
  data0 = 5,
  data1 = 6,
  data2 = 7,
  data3 = 8,    
}
22  Products / Arduino Yún / Re: IDE 1.5.7 missing SPI library on: July 20, 2014, 10:20:57 pm
Time to post your sketch.
23  Using Arduino / Programming Questions / Re: #define, int or const int on: July 20, 2014, 10:03:22 pm
@Pyro,

I learned it is possible  (prefer the C++11 over the C++98)

the constant   5.0/1024   versus   5/1024   can be incorporated same way I guess

The C++98 version can be incorporated into a typedef:

Code:
typedef Select< USE_FLOAT, float, int >::Result analog_t;

analog_t x = analogRead(A0)* 5/1024;

void foo( analog_t &in ){
  return;
}

You could then use the typedef to sort out the constants you mentioned:
Code:
analog_t result = analog_t( 5.0f ) / analog_t( 1024.0f );

//Or for the example above:
analog_t x = analogRead(A0)* analog_t( 5.0f ) / analog_t( 1024.0f );

This has the advantage of removing all floating point arithmetic when analog_t is int.

In C++11 you can also create the typedef using decltype. ( in C++98 mode GCC allows an extension named typeof, which is equivalent to C++11's decltype )
24  Using Arduino / Programming Questions / Re: A C vs C++ trivia question about integer constants on: July 20, 2014, 09:30:45 pm
In the case of the shift operators, I don't think it matters whether the number of bits being shifted is specified as long or 16-bit int - what matters is the type of the number being shifted.

Pete

Yup, you're onto it.

<< & >> are evaluated left to right, so integral promotion rules won't promote a smaller left hand side to a larger right hand value. Primarily because the left hand side denotes the size of the result.

Here is something from the standard:
Quote
5.8 Shift operators [expr.shift]
1 The shift operators << and >> group left-to-right.
shift-expression:
additive-expression
shift-expression << additive-expression
shift-expression >> additive-expression
The operands shall be of integral or unscoped enumeration type and integral promotions are performed.
The type of the result is that of the promoted left operand. The behavior is undefined if the right operand
is
negative, or greater than or equal to the length in bits of the promoted left operand.
2 The value of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are zero-filled. If E1 has an unsigned
type, the value of the result is E1 × 2
E2, reduced modulo one more than the maximum value representable
in the result type. Otherwise, if E1 has a signed type and non-negative value, and E1 × 2
E2 is representable
in the result type, then that is the resulting value; otherwise, the behavior is undefined.
3 The value of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed
type and a non-negative value, the value of the result is the integral part of the quotient

The promoted left operand refers to a left hand value that would be promoted if it was smaller than int, like a char.
25  Using Arduino / Programming Questions / Re: error: expected unqualified-id before '.' token on: July 20, 2014, 09:19:03 pm
No, I just like to leave it in.

extern only needs to be located where it can be seen by multiple source files. However seeing it in someone else's code instantly tells me that changing the variable declaration in the file scope (.cpp) will break something elsewhere, or simply not compile.
26  Using Arduino / Programming Questions / Re: error: expected unqualified-id before '.' token on: July 20, 2014, 09:00:56 pm
include the file "avr/pgmspace.h" into your code.

Also, only the array definition needs PROGMEM, the declaration does not. As you already specify PROGMEM, prog_xxx isn't needed either. Just use unsigned char or uint8_t.

Also, to keep your code usable with the IDE 1.5.7 and above, add const.
Code:
//Header
extern const unsigned char bitmaps[];

//cpp file
extern const unsigned char bitmaps[] PROGMEM = { /** values **/ };
27  Products / Arduino Yún / Re: IDE 1.5.7 missing SPI library on: July 19, 2014, 08:15:05 pm
Each core has its own version, its not in the root libraries folder.

arduino-1.5.7\hardware\arduino\avr\libraries\SPI
arduino-1.5.7\hardware\arduino\sam\libraries\SPI
28  Using Arduino / Programming Questions / Re: Use Pointer or Reference to Serial or SoftwareSerial in Library on: July 19, 2014, 07:45:31 pm
Prefix the function with the class name like you have on the other functions.

Also instead of using a pointer/reference to the HardwareSerial object, You can use the Stream object. Or if you only send data, the Print object ( Serial inherits both. )

An example of inheriting print: http://forum.arduino.cc/index.php?topic=200975.msg1481633#msg1481633
In the above example, Print can be replaced with Stream.


29  Using Arduino / Programming Questions / Re: #define, int or const int on: July 19, 2014, 07:30:39 pm
there is an important function of  #define  not mentioned yet

#define   can be used for conditional compilation e.g.

you can't use a const <type> for this smiley-wink

Maybe not your second example. But the first one is easy:

Using C++11:
Code:
const bool USE_FLOAT = true;

decltype( USE_FLOAT ? float() : int() ) x = analogRead(A0)* 5/1024;

Using C++98
Code:
const bool USE_FLOAT = true;

template< bool V, typename _T, typename _F > struct Select{ typedef _F Result; };
template< typename _T, typename _F > struct Select< true, _T, _F >{ typedef _T Result; };

Select< USE_FLOAT, float, int >::Result x = analogRead(A0)* 5/1024;
30  Using Arduino / Programming Questions / Re: #define, int or const int on: July 19, 2014, 07:41:18 am
And a quick check of an old draft says:

Quote
16.3 Macro replacement [cpp.replace]
1 Two replacement lists are identical if and only if the preprocessing tokens in both have the same number,
ordering, spelling, and white-space separation, where all white-space separations are considered identical.
...
Pages: 1 [2] 3 4 ... 143