Show Posts
Pages: 1 ... 48 49 [50] 51 52 ... 121
736  Using Arduino / Programming Questions / Re: Bitwise operations, again... on: April 08, 2013, 05:26:34 pm
Quote
Code:
const uint8_t   pins[]  = { AD, AC, AB, AA };
uint8_t         digit   = /* 0 - 255 */;

for ( int mask = 0b00001000, i = 0; mask; mask >>= 1, i++ )
{
    digitalWrite(pins[--i], digit & mask ? HIGH : LOW);
}

Is 'i' not set right? it starts at zero, is decremented to -1 before use, then reset to zero, and repeated.
737  Using Arduino / Programming Questions / Re: count character on: April 07, 2013, 11:04:34 pm
A homework question?

Code:
byte CountString( const char *c_Str )
  {
    byte b_Return = 0;
    while( c_Str[ b_Return ] != '\0' ) ++b_Return;
    return b_Return;
  }

char char1[20] = "love";
byte countchar = CountString( char1 ) + 1;
738  Using Arduino / Programming Questions / Re: PROGMEM usage on: April 07, 2013, 12:38:31 am
Yeah, moving the pointer could be bad, however using the pointer value is fine:

Code:
strcpy_P( buff, inProgmem + 5 );
 Serial.print( "Read inProgmem[ 5 ] from flash: " );
 Serial.println( buff );

it uses the same principle involved in using struct members to offset a pointer:

Code:
struct RGB{
  int red;
  int green;
  int blue;
};

RGB Colours[] PROGMEM = {
     { 0, 0, 0 },
     { 255, 0, 0 },
     { 2, 255, 0 },
     { 0, 0, 255 },
     { 255, 0, 255 },
     { 255, 255, 255 },     
  };

void GetColour( RGB &r_Out, int i_Index )
  {
    const RGB &r_Colour = *(Colours + i_Index);
   
    r_Out.red = pgm_read_word( &r_Colour.red );
    r_Out.green = pgm_read_word( &r_Colour.green );
    r_Out.blue = pgm_read_word( &r_Colour.blue );
    return;
  }
739  Using Arduino / Programming Questions / Re: PROGMEM usage on: April 06, 2013, 11:10:31 pm
Just try printing the array as normal, it should do some undefined bahaviour/nothing/everything.

Code:
char inRam[] = "String in SRAM";

char inProgmem[] PROGMEM = "String in FLASH";

void setup() {

 char buff[ 20 ];
 
 Serial.begin(9600);
 
 Serial.print( "Read inRam from ram: " );
 Serial.println( inRam );
 
 strcpy_P( buff, inProgmem );
 
 Serial.print( "Read inProgmem from flash: " );
 Serial.println( buff );
 
 Serial.print( "Read inProgmem from ram: " );
 Serial.println( inProgmem );
}

void loop() {}
740  Using Arduino / Programming Questions / Re: PROGMEM usage on: April 06, 2013, 10:46:57 pm
strcpy_P is the PROGMEM version of strcpy.

If you pass it cmd_hello, it does the  'pgm_read_XX' handling for you.

cmd_hello is a pointer stored in ram pointing to its data stored in flash, you only need special functions to access the data, not the pointer.

741  Using Arduino / Programming Questions / Re: variable declaration on: April 06, 2013, 07:23:16 pm
I've always wished for -

Code:
typedef const uint8_t   pin_t;


What's that ?

typedef creates an alias to a decorated type.
This one says, alias pin_t with a type 'constant uint8_t'

So later on you can use the type in your code:

Code:
pin_t pin13 = 13; //This is a const type, it cannot be changed.

Similar to:

Code:
const unsigned char pin13 = 13;
//or
const byte pin13 = 13;
//or
const uint8_t pin13 = 13;

Actually, uint8_t is a typedef provided by the standard:
Code:
typedef unsigned char uint8_t;

You can also use a type which is guaranteed to be constant:

Code:
enum{ pin13 = 13 };
//...
digitalWrite( pin13, HIGH );

742  Using Arduino / Programming Questions / Re: How to generate a piano sound using SPI on: April 04, 2013, 11:00:19 pm
I would suggest finding the model of your DAC, then locate the data sheet and search for examples on the forums and google, there is a good chance someone has done a write up on it.
Once you have a working example you can then focus on producing specific sounds.
743  Using Arduino / Programming Questions / Re: PROGMEM and F() macro as parameters for function on: April 04, 2013, 09:35:05 pm
There are a couple of things you can do.

the macro F is defined as
Code:
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))

So it already uses PSTR(), and was designed to prevent the ambiguities. The macro casts the progmem pointer to a type __FlashStringHelper* which allows functions to overload based on a type that is incompatible with normal char*.

So instead of PSTR(), just use F().

To solve the problem for global data, i.e. prog_uchar test2[] PROGMEM = { "Test 2" } ; you could use a macro as well:
Code:
#define PGMT( pgm_ptr ) ( reinterpret_cast< const __FlashStringHelper * >( pgm_ptr ) )

Here is a test case:
Code:
char test2[] PROGMEM = { "Test 2" } ;

#define PGMT( pgm_ptr ) ( reinterpret_cast< const __FlashStringHelper * >( pgm_ptr ) )

void setup()
{
  Serial.begin( 9600);
  Serial.println( "Start");
 
  Serial.print( "1: ");
  WriteSlave( "Test 1");
  Serial.print( "2: ");
  WriteSlave( PGMT( test2 ), 6);
  Serial.print( "3: ");
  WriteSlave( F("Test 3"), 6);
  Serial.println("");
  delay(5000); 
}

void loop(){}

void WriteSlave( const char *pData )
{
  Serial.write( pData );
  Serial.println("");
}

void WriteSlave( const __FlashStringHelper* pData, const int size)
{
  char buffer[ size + 1 ]; //Size array as needed.
  Serial.print( "-> __FlashStringHelper -> ");
  memcpy_P( buffer, pData, size);
  buffer[ size ] = '\0';  //protect from odd sized strings ( a short string will reveal old chars )
  WriteSlave( buffer, size);
}

As you can see, even more ambiguities are removed by having different counts of parameters ( "Test 1" is a null-terminated literal, so serial.print doesn't need a count.

But we can go one step further and remove the count from the progmem side of things:

Code:
char test2[] PROGMEM = { "Test 2" } ;

#define PGMT( pgm_ptr ) ( reinterpret_cast< const __FlashStringHelper * >( pgm_ptr ) )

void setup()
{
  Serial.begin( 9600);
  Serial.println( "Start");
 
  Serial.print( "1: ");
  WriteSlave( "Test 1");
  Serial.print( "2: ");
  WriteSlave( PGMT( test2 ));
  Serial.print( "3: ");
  WriteSlave( F("Test 3"));
  Serial.println("");
  delay(5000); 
}

void loop(){}

void WriteSlave( const char *pData )
{
  Serial.write( pData );
  Serial.println("");
}

void WriteSlave( const __FlashStringHelper* pData )
{
  char buffer[ 20 ]; //Size array as needed.
  int cursor = 0;
  prog_char *ptr = ( prog_char * ) pData;

  Serial.print( "-> __FlashStringHelper -> ");
  while( ( buffer[ cursor ] = pgm_read_byte_near( ptr + cursor ) ) != '\0' ) ++cursor;
  WriteSlave( buffer );
}
744  Using Arduino / Microcontrollers / Re: AVR vs ARM on: April 04, 2013, 09:07:37 am
The Uno and Mega are minimal different because the internal counters are not equal clocked. (capacitor inaccuracy).

Maybe so, but the Mega pays a price for having more code space, some of the branching instructions take 1 additional cycle over smaller AVR's. You make use of these instructions too ( CALL, RET, RETI, ... ) so a difference in execution time is expected.

745  Using Arduino / Programming Questions / Re: where I can find the hex file? on: April 02, 2013, 06:17:22 pm
Open the IDE preferences, then select verbose mode.
When you compile or upload ( whatever option you select ), it will display the path to the temporary folder.

The output window should finish with something like this:
Quote
C:\Users\Elvis\AppData\Local\Temp\build8351408690549800379.tmp/pun.cpp.hex
Binary sketch size: 8,864 bytes (of a 258,048 byte maximum)

This is on windows, I presume the other platforms are the same.
746  Using Arduino / Programming Questions / Re: Classes and objects inside them that can be used my all members of a class on: April 02, 2013, 05:15:31 am
Your class code is not valid, it needs a name, and only constructors and destructors have no return type. Everything else is marked with a type, functions that do not return data use the type 'void'

Code:
#ifndef MYLIB_H
#define MYLIB_H

class myLib
{
   private:
             int myVar;
   public:
             myLib();
             void myLibUpdate();
};
#endif

Code:
#include "myLib.h"
#include "PID.h"

myLib::myLib()
{
   PID myPID(input,output,value,kp,ki,kd,DIRECT); // This is what I am trying and its not working
}


void myLib::myLibUpdate()
{
   myPID.compute();
}

Also, if PID is a separate library, use #include <PID.h> and place it in your sketch file too.
747  Using Arduino / Programming Questions / Re: struct within struct and pointers on: April 02, 2013, 05:07:07 am
No, that code is not safe; your use of temporaries is wrong.

To see the full effect of your mistake, add a destructor to struct A:
Code:
  ~A(){
     a = 0;
     b = 0;
     c = 0;
  } 

This test case shows how temporaries are destroyed at the end of the expression that created it. You simply create a bad pointer.
Code:
struct A {
  int a;
  int b;
  int c;
 
  ~A(){
     a = 0;
     b = 0;
     c = 0;
  } 
};

struct B {
  int test;
  const A *a;
};

void printbytes( uint8_t *u, int len ){
 while( len-- ) Serial.print( *u++, HEX );
}

void setup() {
  Serial.begin(9600);
 
  B b;
  b.test = 1;
  // I'm not sure if this is safe but I think it is.
  b.a = (const A[]) {
    {1, 2, 3},
    {1, 3, 4}
  };
 
  // or...
  B c = {
    2,
    (const A[]) {
      {1, 3, 4},
      {3, 3, 1},
      {1, 2, 3}
    }
  };
 
  Serial.print( "Data b: " );
  printbytes( ( uint8_t* ) b.a, 12 );
  Serial.print( "\r\nData c: " );
  printbytes( ( uint8_t* )c.a, 18 );
}

void loop() {
}
748  Using Arduino / Programming Questions / Re: combine vars ? on: March 23, 2013, 03:54:09 am
If your numbers are decimal ( base 10 ),

then simply:
Code:
#define BASE 10
int A = 2;
int B = 3;

int C = A * BASE + B;
749  Using Arduino / Programming Questions / Re: combine vars ? on: March 23, 2013, 12:38:36 am
More info is needed, but here is a guess:

You maybe want two integers stored together, a simple bit shift might do.
Code:

uint8_t A;
uint8_t B;

uint16_t C =( A << 8 ) | B;
750  Using Arduino / Programming Questions / Re: Programming logic question on: March 21, 2013, 01:09:14 am
Maybe this might help: ( x & y ) == z

With a cast to prevent int promotion, this compiles smaller anyway ( untested )
Code:
void loop(){}

uint8_t x;
uint8_t y;

void setup(){
 
  x = analogRead(0);
  y = analogRead(0);
  const uint8_t z = 2;
 
  //if( x == y && x == z ){
   
  if( uint8_t( x & y ) == z ){
    x = analogRead(0);
  }
  y = analogRead(0);
}
Pages: 1 ... 48 49 [50] 51 52 ... 121