Show Posts
Pages: 1 2 3 [4] 5 6 ... 152
46  Using Arduino / Programming Questions / Re: error: multiple definition of `func1()' on: September 12, 2014, 04:18:59 am
Place the include inside a namespace, and call that version.

Code: (sketch.ino)
namespace Foo{
  #include "test.cpp"
};

void setup() {
  Serial.begin( 9600 );
  Foo::func();
}

void loop() {}

Code: (test.cpp)
#include <Arduino.h>
void func(){ Serial.print( "HI" ); }

Or use ifdef guards to prevent showing the definitions when not included.
47  Using Arduino / Programming Questions / Re: Selective Random Pins..not 1-10, but only 1,4,7,9 anyone know how to limit that on: September 12, 2014, 02:57:53 am
I have updated my example to actually control the pins.

It shows how to do it without using delay().

Code:
#define PIN_ON_TIME  1000
#define PIN_OFF_TIME 1000

struct PinFlash{
  PinFlash( const char pin ) : start( millis() ), pin( pin ) { digitalWrite( pin, HIGH ); }
  ~PinFlash() {
    while( millis() - start < PIN_ON_TIME );
    digitalWrite( pin, LOW );
  }
  const char pin;
  const unsigned long start;
};

void setup() {
  for( char i = 1 ; i < 20 ; ++i ){
    switch( i ){ case 5 ... 15: continue; }
    pinMode( i, OUTPUT );
  }
  Serial.begin( 115200 );
}

void loop() {
  static unsigned long lastRun = 0;
  if( millis() - lastRun < PIN_OFF_TIME ) return;
 
  switch( char pin = random( 1, 20 ) ){
    case 1 ... 4:
    case 16 ... 19: {
        PinFlash p( pin );
        Serial.print( "Flashing LED: " );
        Serial.println( pin, 10 );
      }
      lastRun = millis();
  };
}
48  Using Arduino / Programming Questions / Re: Writing to the wrong elements of an array on: September 12, 2014, 02:21:32 am
If you stick a Serial.print(mtxRow); before each write to the array, you will see that mtxRow does increase to 8, but is reset to 0 before it is called again.

Not talking about that variable  smiley-wink

Quote
buffer[mtxRow][mtxWidth+ col] = 1;

...

Quote
const byte mtxWidth = 8;
49  Using Arduino / Programming Questions / Re: Selective Random Pins..not 1-10, but only 1,4,7,9 anyone know how to limit that on: September 12, 2014, 02:08:21 am
this is a simon like game, it means that each led should light up with same delay. with that code, if taking random from inexitent pins, would delay infinite.
sorry but it's not efficiente, neither acceptable. it's a mistake. sorry.

Fixed the random typo, but do you understand how it works?

The code is ready to go, upload it to a board and see. The random function may return values outside of the needed ranges, however these are ignored.

50  Using Arduino / Programming Questions / Re: Selective Random Pins..not 1-10, but only 1,4,7,9 anyone know how to limit that on: September 12, 2014, 01:41:10 am
As your data is in a neat set of ranges, there is no need to store a lookup table.

Code:
void setup() {
  Serial.begin( 9600 );
}

void loop() {
  switch( char pin = random( 1, 20 ) ){
    case 1 ... 4:
    case 16 ... 19:
      Serial.print( "Got new pin: " );
      Serial.println( pin, 10 );
      delay( 300 );
  };
}
51  Using Arduino / Programming Questions / Re: Can Arduino IDE compile files in sub directories? on: September 12, 2014, 01:28:00 am
If you create a library you can have a sub-directory called 'utility', but its seems not for a sketch, you'll have to keep all the sketch files together.
52  Using Arduino / Programming Questions / Re: Writing to the wrong elements of an array on: September 12, 2014, 01:18:11 am

'buffer's second dimension is set to 8 ( mtxHeight ) so the range of valid indices are [ 0 to 7 ]

Code:
const byte mtxWidth = 8; //matrix width
const byte mtxHeight = 8;

byte buffer[mtxWidth + charWidth][mtxHeight];

You access it at index 8 and above:
Code:
buffer[mtxRow][mtxWidth+ col] = 1;

The multiple 1's may be a result of runtime corruption. Fix your indexing errors then check again.
53  Using Arduino / Programming Questions / Re: calling variable from other tab on: September 11, 2014, 04:22:09 am
If both files are .ino, extern is not needed. The IDE will join all .ino files together, starting with the one named the same as the sketch ( so the compiler sees it as one big .cpp file. ).

If you place the float variable above the functions it is used in ( maybe the top of the main .ino. ) then you should have some luck hopefully. extern is only needed to share variables between c++ .cpp source files.

Your setup is only  a guess however, post the code from both tabs.
54  Using Arduino / Programming Questions / Re: cannot declare variable to be of abstract type on: September 11, 2014, 04:10:44 am
The message is kind of clear.

Quote
rosserial_arduino::Acc_righthand Acc_righthand_msg;

Quote
Acc_Recieverhand:15: error: cannot declare variable ‘Acc_righthand_msg’ to be of abstract type ‘rosserial_arduino::Acc_righthand
/home/sp/arduino-1.0.1/libraries/ros_lib/rosserial_arduino/Acc_righthand.h:13: note:   because the following virtual functions are pure within ‘rosserial_arduino::Acc_righthand’:


You have a function with '=0' after it ( or an error in your class that looks like one ). Using a class with pure virtual functions is a clear indication of error, as only a derived type will provide an implementation for the pure members. ( i.e a class with pure virtual functions can also be described as 'incomplete' or... abstract ).

It thinks you have these members marked as pure, do you?:

Code:
virtual int ros::Msg::serialize(unsigned char*) const
virtual const char* ros::Msg::getMD5()
55  Using Arduino / Programming Questions / Re: large array compiler error - Trying to load image into Flash on: September 10, 2014, 06:09:30 am
I've placed around 50K chunks of data in flash and was able to sequentially access all of them (which it seems I should not be able to if the pointer is SIGNED).

I used static const uint8_t bitmap[] PROGMEM = { ....~50 k of data .... };

Not using Arduino and GCC you didn't. It has a maximum of 32768 bytes, not elements per addressable object regardless of what those AVRFreaks think, try and declare these arrays bigger:

Code:
char a[ 32767 ];
short b[ 16383 ];
int c[ 16383 ];
long d[ 8191 ];
long long e[ 4095 ];
float f[ 8191 ];
double g[ 8191 ];

http://arduino.land/FAQ/content/6/7/en/what-is-the-maximum-number-of-elements-an-array-can-have.html
56  Using Arduino / Project Guidance / Re: Yes Or No, will this work or shall I go elsewhere? on: September 09, 2014, 07:31:38 pm
The PTZ is controlled by the software, the camera must be sent the PTZ data from the main box. If you can intercept the data for sure you can use an Arduino to interpret it and control some servos.

Will the camera tell the device weather it supports PTZ ( options will probably be disabled in the software if not available )?

3g is irrelevant isn't it? only the PC and main device use it to communicate ( and Arduino will not be able to intercept a 3g transmission due to security and lack of hardware ).

Why not create your own app. Even Google chrome can use the com port ( website with JavaScript... easy ). then you can independently control the servos regardless of what the DVR or its software is doing.

You are jumping in the deep end and are going to smother yourself in ideas you might not understand. Baby steps... learn how to control a servo using Arduino, then modify it to user the serial comms to control it a bit more. and so on...

Yeah you might not have time to tinker, but if that's the case, you might as well keep this as a pipe dream. A month or two of learning and you'll probably surprise yourself.

Why not get a cheapo wifi IP camera. They are full colour and some models have PTZ built in. Then everything is already done for you.
57  Development / Other Software Development / Re: Some new core functions on: September 09, 2014, 09:51:11 am
Quote
How would I do that with this, do I just use #if defined(__AVR__) and just mod the code to use dynamic memory for non AVR machines?

There is no need, my lack of sleep made me blind to the fact that N is a constant, and is legal in any C++.

For clarification, if N was a parameter to the function ( and therefore not a compile time constant in C++98 ) rather than a template parameter, things would be different.

The AVR macro is one way, however other platforms use GCC (Due) and it seems there is a __GNUC__ macro.
https://gcc.gnu.org/onlinedocs/gcc-4.8.1/gcc/C-Extensions.html
And the extension in question: https://gcc.gnu.org/onlinedocs/gcc-4.8.1/gcc/Variable-Length.html#Variable-Length

Code:
#ifdef __GNUC__
  T _data[N];
#elif
  T* _data = new T[N];
#endif

//...

#ifndef __GNUC__
  delete [] _data;
#endif


Quote
When will C++11 be usable on the arduino, its usefulness will be great appreciated.

You can enable it in 1.5.7, and it'll hopefully be default in 1.5.8 ( https://github.com/arduino/Arduino/pull/2175 )
58  Development / Other Software Development / Re: Some new core functions on: September 09, 2014, 08:17:26 am
A big thing that will keep your code out of the core is dynamic memory usage.

GCC supports stack based allocation, and on an AVR, you should definitely use it over short lived dynamic allocations.

Quote
template<typename T, size_t N> T median(T (&Arr)[N])
{
  //T* _data = new T[N];
  T _data[N];
  sort(Arr);
  for (int i = 0; i < N; i++)
  {
    _data[i] = Arr[i];
  }
 
  T Med = 0;
  if ((N % 2) == 0)
    Med = (T)(_data[N/2] + _data[(N/2) - 1])/2;
  else
    Med = _data[N/2];

  //delete [] _data;
  return Med;
}

It is an extension, however the Arduino core uses gnu extensions as the AVR core has parts of its code which require it ( atomic block macros for example ), so its worth using. Also a simple #ifdef can be used to replace the dynamic handling for non AVR machines.
59  Development / Other Software Development / Re: Some new core functions on: September 09, 2014, 07:49:12 am
@Pyro
I never did look into C++11, :/ Looks like I got homework.

I'm still learning it myself, infact I didn't realize at first, but the LargerType and Select classes in my code can be replaced with the new decltype keyword:

Code:
template< typename T, typename U, typename V, typename X, typename Y >
   auto map( const T &x, U &&imin, V &&imax, X &&omin, Y &&omax ) -> T
    {
      typedef decltype( T() * U() * V() * X() * Y() ) cll;
      return (cll)(x - imin) * (omax - omin) / (imax - imin) + omin;
    }

As integer promotion will occur with each multiplication operator, the largest type can be found.

Also a new function declaration syntax allows using the same decltype as the return type of the function; and then its sized to the largest type which will prevent overflow if the data input is of a different type to the range inputs.

Code:
template< typename T, typename U, typename V, typename X, typename Y >
   auto map( const T &x, U &&imin, V &&imax, X &&omin, Y &&omax ) -> decltype( T() * U() * V() * X() * Y() )
    {
      typedef decltype( T() * U() * V() * X() * Y() ) cll;
      return (cll)(x - imin) * (omax - omin) / (imax - imin) + omin;
    }

Another feature is ranged loops, your mean function could look like this:
Code:
template<class T, size_t N>
  T mean(T(&x)[N]){
    T tmp = 0;
    for( auto i : x )  tmp += i;
    return tmp / N;
}
60  Using Arduino / Programming Questions / Re: Somebody can explain to me the meaning of this part of code on: September 09, 2014, 06:51:50 am
Template functions are not supposed to always go into a header file. I've never heard anything like that, nor read in the C++ language specifications that it was mandatory.

Actually, not firing very local declarations into interface files and just letting them in the required scope can be considered as a good practice.

Anyway, the so-called "badly-writting" (which is just a matter of point of view) is not enough to explain nor justify the particular behaviour of the Arduino compiler.

You are completely right, just take into account the article is aimed at noobs, who have most probably put a class definition in a separate .cpp to the declaration in a header; And only included the header.

As a side note, this can be solved by including the .cpp, rather than the .h.

Whean dealing with the IDE auto prototype feature, you can suppy your own prototype like Nick said, however you can also use something called an exception specification to prevent a function having its prototype generated at all, which can save time if your code is changing as it grows, it is explained in detail at the bottom of this article, also this one explains the steps the IDE takes before compiling .
Pages: 1 2 3 [4] 5 6 ... 152