Show Posts
Pages: 1 ... 72 73 [74] 75 76 ... 146
1096  Using Arduino / Programming Questions / Re: Must Arduino libraries be object-oriented? on: April 18, 2013, 08:47:41 pm
There is no need, here is a post I made the other day: http://arduino.cc/forum/index.php/topic,104447.msg1205867.html#msg1205867
There are other posts there that might be useful too.
 
1097  Using Arduino / Programming Questions / Re: enum and switch cases on: April 18, 2013, 08:02:27 pm
Quote
cannot convert "char*" to "clr" in initialization

Is that really what it said?
Seems more appropriate to say the actual type mismatch:
Quote
cannot convert "char*" to "Color" in initialization

If arg was an int or char, I would suggest casting the value, but char* is pretty incompatible.

Code:
Color clr = ( Color )( unsigned int ) arg;
1098  Using Arduino / Programming Questions / Re: newby help on: April 18, 2013, 08:30:38 am
Static variables inside a function hold their value between calls to that function.
They can also be given a starting value.

Code:
void loop()
{
static char lastState = !switch1;

if( lastState != switch1 ){

  lastState = switch1;

  if (switch1 == HIGH){
    Serial.println("switch open");
  }else{
    Serial.println("switch closed");
  }
}

}
1099  Using Arduino / Programming Questions / Re: can i include .ino or pde files? on: April 18, 2013, 12:48:24 am
Arduino.h would only be required if you use any of the standard arduino core stuff like 'byte' and digitalRead.
1100  Using Arduino / Programming Questions / Re: use of main() in arduino IDE on: April 17, 2013, 11:16:45 pm
Quote
I'm going to go look up that init() you mentioned.  maybe that is what is configuring my timers differently ?

Yeah it does do some things, it is in wiring.c

Here is the timer1 stuff:
Code:
// timers 1 and 2 are used for phase-correct hardware pwm
// this is better for motors as it ensures an even waveform
// note, however, that fast pwm mode can achieve a frequency of up
// 8 MHz (with a 16 MHz clock) at 50% duty cycle

#if defined(TCCR1B) && defined(CS11) && defined(CS10)
TCCR1B = 0;

// set timer 1 prescale factor to 64
sbi(TCCR1B, CS11);
#if F_CPU >= 8000000L
sbi(TCCR1B, CS10);
#endif
#elif defined(TCCR1) && defined(CS11) && defined(CS10)
sbi(TCCR1, CS11);
#if F_CPU >= 8000000L
sbi(TCCR1, CS10);
#endif
#endif
// put timer 1 in 8-bit phase correct pwm mode
#if defined(TCCR1A) && defined(WGM10)
sbi(TCCR1A, WGM10);
#elif defined(TCCR1)
#warning this needs to be finished
#endif
1101  Using Arduino / Programming Questions / Re: use of main() in arduino IDE on: April 17, 2013, 10:52:52 pm
using main overrides the internal main.

It also calls a function named init() that also sets up timers and PWM.

So your 1st and 2nd codes override it and setup/loop aren't even called in the 2nd code.

the 3rd uses the default main, sets up the timers, then you make further changes using mainx.

EDIT: main is the program entry point, the Arduino default calls init(), then setup(), then calls loop() in a never ending loop.
1102  Using Arduino / Programming Questions / Re: can i include .ino or pde files? on: April 17, 2013, 09:56:46 pm
I too would like to be able to take say a dozen (maybe or maybe not unrelated) short C functions I've created and somehow turn them into a simple library that I could include into a sketch when desired. So if someone does have some simple examples to accomplish that I be most interested in seeing it.

Lefty

You can use namespaces for this job, here is a mockup FFT library:

FFT.h
Code:
#include "complex.h"

namespace FFT{
//   FORWARD FOURIER TRANSFORM
bool Run(const complex *const Input, complex *const Output, const unsigned int N);
bool Run(complex *const Data, const unsigned int N); //INPLACE VERSION

//   INVERSE FOURIER TRANSFORM
bool Inverse(const complex *const Input, complex *const Output, const unsigned int N, const bool Scale = true);
bool Inverse(complex *const Data, const unsigned int N, const bool Scale = true); //INPLACE VERSION
};

FFT.cpp
Code:
#include "FFT.h"

namespace FFT{

//   FORWARD FOURIER TRANSFORM
bool Run(const complex *const Input, complex *const Output, const unsigned int N)
{
return true;
}

bool Run(complex *const Data, const unsigned int N) //INPLACE VERSION
{
return true;
}
//Left out Inverse functions.
};

Client Code
Code:
#include <FFT.h>

void loop(){

  Complex c_Data[ 512 ];
  //... Fill with data.

  //Use library
  FFT::Run( c_Data, 512 );

}
1103  Using Arduino / Programming Questions / Re: enum member of a class on: April 17, 2013, 09:13:17 pm
For non-static members of a class, this is not initialising, its assigning.
Code:
valve::valve()
{
    _state = opening;
}

This is initialising:
Code:
valve::valve() : _state( opening )
{ return; }
1104  Using Arduino / Programming Questions / Re: length of multidimensional array? on: April 17, 2013, 12:19:20 am
Never needed to know that value, but here is how I would do it in C++:
Code:
template< typename T, size_t A, size_t N >
  inline const size_t sizeofMArray( T (&)[A][N], const char item ) { return item ? A : N; }

Tested with this code:
Code:
    Serial.begin( 9600 );
    Serial.println( sizeofMArray( zone1, 0 ) );
    Serial.println( sizeofMArray( zone1, 1 ) );
    Serial.println( sizeofMArray( zone2, 0 ) );
    Serial.println( sizeofMArray( zone2, 1 ) );
    Serial.println( sizeofMArray( zone3, 0 ) );
    Serial.println( sizeofMArray( zone3, 1 ) );

Gives this output.
Quote
3
6
3
7
3
4

Do what WizenedEE said and use the global variable, then this code in your function.
1105  Using Arduino / Programming Questions / Re: can you see what I am doing wrong? on: April 16, 2013, 07:40:31 pm
What pin are you setting high, http://arduino.cc/forum/index.php/topic,160932.msg1204291.html#msg1204291

There is a difference between pin numbers and pin values You are comparing a pin number with HIGH, completely illogical,
just remove the 'if' dang nabbit, the pin is high cos you set it high, there is no need to assume it has changed unless you typed the code below inside an interrupt.

Code:
digitalWrite(b1yel1, LOW);

1106  Using Arduino / Programming Questions / Re: can you see what I am doing wrong? on: April 16, 2013, 07:04:25 pm
It appears b1yel1 is a pin number not a LOW/HIGH value, so the if statement will fail on any pin number that isn't 1 and skip line 7

Code:
5      digitalWrite(b1yel1, HIGH);   //  I have put this line in to prove b1yel1 is high
6      if (b1yel1 == HIGH)  
7        digitalWrite(b1yel2, HIGH);

You could check the pin is high, but you just set it high, so only an interrupt could change it, which you probably aren't using.
1107  Using Arduino / Programming Questions / Re: Undefined reference compilation error on: April 16, 2013, 04:14:08 am
The sketch is compiled as C++, your other.c is C.
rename the file to .cpp or look at this: http://stackoverflow.com/questions/1041866/in-c-source-what-is-the-effect-of-extern-c
1108  Using Arduino / Programming Questions / Re: Issue with variable declaration on: April 16, 2013, 01:06:31 am
Ah, I see whats happening, not used scanf at all really.
I just had a look at the documentation for scanf:

Quote
format specifier for scanf follows this prototype:

%(*)[width][length]specifier

Seeing as you use 'u', which is integer ( 2 bytes ), do you need to specify a width/length of 1 for one byte. like '%1u' instead of '%u'
1109  Using Arduino / Programming Questions / Re: Issue with variable declaration on: April 15, 2013, 08:42:44 pm
Drop the '&' character.

If 'mydns' is an array:
Code:
byte mydns[4] = {1,1,1,1};
//or any other type
int mydns[4] = {1,1,1,1};

You access its elements like:
Code:
mydns[ index ]

Not:

Code:
&mydns[ index ]

But IPAddress supports using '[]' operators so this code below is fine:

Code:
IPAddress mydns;

//No '&' infront of element.
mydns[0] = 192;
mydns[1] = 168;
mydns[2] = 0;
mydns[3] = 1;
1110  Using Arduino / Programming Questions / Re: EEPROM read and write on: April 15, 2013, 10:48:11 am
That library is fairly easy to use.

Code:
unsigned long int hiscore[8];

//Write
for( int index = 0 ; index < 8 ; ++index ){
  EEPROM_writeAnything( index * sizeof( unsigned long int ), hiscore[ index ] );
}

//Read
for( int index = 0 ; index < 8 ; ++index ){
  EEPROM_readAnything( index * sizeof( unsigned long int ), hiscore[ index ] );
}

Edit: you could extend the life of your eeprom when writing by using the method below:
Code:
for( int index = 0 ; index < 8 ; ++index ){

  unsigned long int temp;
  EEPROM_readAnything( index * sizeof( unsigned long int ), temp );

  if( temp != hiscore[ index ] ){

    EEPROM_writeAnything( index * sizeof( unsigned long int ), hiscore[ index ] );
  }
}
Pages: 1 ... 72 73 [74] 75 76 ... 146