Show Posts
Pages: [1] 2 3 ... 142
1  Using Arduino / Programming Questions / Re: Do I need to reset variables to 0 ? on: Today at 11:46:25 am
Variables obtained by assignement are automatically reset when a new assignement is done ? Can you confirm?

loose the notion of reset, a String can be assigned an empty string, and an int can be assigned zero, if this is what you need. If you need it to have real data, then just assign that to it.

if I well understand, only variables obtained by += have to be cleared for new usage ?

The += operator is one example. I used it as the String library overloads it. If you have control of the code, just change the '+=' to '='. Do you understand the differences between the two?

Quote
I need to simplify my sketch because I have to care about the size of it because it is a very big sketch and the Uno memory is limited.

Then you should probably stick to character arrays instead of the String library. To a novice C++ programmer the memory usage can seem quite unpredictable when using Strings.
2  Using Arduino / Programming Questions / Re: Do I need to reset variables to 0 ? on: Today at 09:42:46 am
No, you just need to set it to something useful before its reused. Which you do.
Code:
semicolunPosition = trimIncomingString.indexOf(';');

We can't see where trimIncomingString is populated, so it might if it is populated using '+=' and you do not want the old data. If there is only a simple assignment, then it is replaced.

Why do you ask, some debugging problem?
3  Using Arduino / Programming Questions / Re: Array Difficulties on: Today at 09:34:22 am
There are 10 different segments of LEDs that can all be different colors, so I'd think each LED segment world require a color right?

For sure, but if the 10 values for each set are the same, you should only need to store one value for each set which you can just repeat.

AWOL pointed out you could simply shift 0xF00000 right ( 4 * x ) bits where x is the index of the segment. There is no real need for an array.

What is your motivation for a first dimension of 1? It is the same as not having the extra dimension at all.

Also the repeating variable names requires repeated code to use, arrays are extremely useful for this situation ( 1 array with an extra dimension ):
Code:
const unsigned long color_1[10];
const unsigned long color_2[10];
const unsigned long color_3[10];
//...
//Or

const unsigned long color[ 3 ][ 10 ];

4  Using Arduino / Programming Questions / Re: multiple if or for loop with if inside on: Today at 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;
}
5  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
6  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.
7  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.
8  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
9  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,    
}
10  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.
11  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 )
12  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.
13  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.
14  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 **/ };
15  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
Pages: [1] 2 3 ... 142