Show Posts
Pages: 1 ... 23 24 [25] 26 27 ... 52
361  Using Arduino / Programming Questions / Re: Text Array on: January 08, 2013, 08:58:21 pm
If we're going to throw things out there that don't directly answer the question then I'll post this one -

Code:
uint32_t sNames[] = {
      '\0nuS'   // 0
    , '\0noM'   // 1
    , '\0euT'   // 2
    , '\0neW'   // 3
    , '\0uhT'   // 4
    , '\0irF'   // 5
    , '\0taS'   // 6
};

Serial.print((char*)&sNames[1]);


Oh, now I just feel silly ...


EDIT:  Then again this is more memory efficient than my original post!
362  Using Arduino / Programming Questions / Re: Text Array on: January 08, 2013, 12:34:19 am
Code:
char* sNames[] = {
      "Sun" // 0
    , "Mon" // 1
    , "Tue" // 2
    , "Wed" // 3
    , "Thu" // 4
    , "Fri" // 5
    , "Sat" // 6
};

Serial.print(sNames[1]);
363  Using Arduino / Programming Questions / Re: How works digital input/output on: January 07, 2013, 10:02:41 pm
Don't forget 'millis()' and 'micros()'
364  Using Arduino / Programming Questions / Re: Arduino Class C++ on: December 31, 2012, 02:20:04 pm
Constructors and destructors

Try doing a few simple experiments such as

Code:
struct blinker_t
{
     blinker_t() { Serial.println("Hello ...");   };
    ~blinker_t() { Serial.println("Good bye!!!"); };
};

void loop()
{}

void setup()
{
    Serial.begin(9600);

    blinker_t temp;
}

We haven't even done anything with 'temp' other than create an instance of it!

Move 'blinker_t temp' from local scope of 'setup' to the global scope.

Code:
struct blinker_t
{
     blinker_t() { Serial.println("Hello ...");   };
    ~blinker_t() { Serial.println("Good bye!!!"); };
};

blinker_t temp;

void loop()
{}

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


Same thing here, we haven't done anything with 'temp' other than declare an instance of it in the global scope!

Moving it into 'loop'

Code:
struct blinker_t
{
     blinker_t() { Serial.println("Hello ...");   };
    ~blinker_t() { Serial.println("Good bye!!!"); };
};

void loop()
{
    blinker_t temp;
}

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

Does anything surprise you?
365  Using Arduino / Programming Questions / Re: a common cathode seven segment display library on: December 30, 2012, 10:41:27 pm
Further ...

Code:
void display(int pattern)
{
    digitalWrite(pin_a, ((0b01000000 & pattern) ? ON : OFF));
    digitalWrite(pin_b, ((0b00100000 & pattern) ? ON : OFF));
    digitalWrite(pin_c, ((0b00010000 & pattern) ? ON : OFF));
    digitalWrite(pin_d, ((0b00001000 & pattern) ? ON : OFF));
    digitalWrite(pin_e, ((0b00000100 & pattern) ? ON : OFF));
    digitalWrite(pin_f, ((0b00000010 & pattern) ? ON : OFF));
    digitalWrite(pin_g, ((0b00000001 & pattern) ? ON : OFF));
}

void sevenseg(int val)
{
    switch ( val )
    {
        case 0:  display(0b01111110);   break;    // 0
        case 1:  display(0b00110000);   break;    // 1
        case 2:  display(0b01101101);   break;    // 2
        case 3:  display(0b01111001);   break;    // 3
        case 4:  display(0b00110011);   break;    // 4
        case 5:  display(0b01011011);   break;    // 5
        case 6:  display(0b01011111);   break;    // 6
        case 7:  display(0b01110000);   break;    // 7
        case 8:  display(0b01111111);   break;    // 8
        case 9:  display(0b01110011);   break;    // 9
    }
}

tuxduino,

<http://universalindent.sourceforge.net>
366  Using Arduino / Programming Questions / Re: a common cathode seven segment display library on: December 30, 2012, 10:30:59 pm
Using 'switch' can simplify your code slightly,


Code:
void sevenseg(int val)
{
    switch ( val )
    {
        case 0:     zero();     break;
        case 1:     one();      break;
        case 2:     two();      break;
        case 3:     three();    break;
        case 4:     four();     break;
        case 5:     five();     break;
        case 6:     six();      break;
        case 7:     seven();    break;
        case 8:     eight();    break;
        case 9:     nine();     break;
    }
}
367  Using Arduino / Programming Questions / Re: a common cathode seven segment display library on: December 30, 2012, 07:43:37 pm
And that was what his very first question of the posted code was also concerned with.

A little advanced?  Yes, surely, but I threw it out there to see how receptive he was to learning based on smaller version of the same code he had implemented.

Let's see how things go from here ...

EDIT: Oh, and I agree with you concerning more meaningful symbol names, but we had to start somewhere and I chose something along the lines of those he was using.
368  Using Arduino / Programming Questions / Re: a common cathode seven segment display library on: December 30, 2012, 07:31:18 pm
Correct!  Read from left to right the left most bit is unused with the other 'A', 'B', 'C', etc. to 'G' as the right most bit.

The secret (not really secret) is that the 'Arduino Language' is in fact, surprise, C++!  And the compiler is in fact a version of the GNU C++ compiler.
369  Using Arduino / Programming Questions / Re: a common cathode seven segment display library on: December 30, 2012, 06:58:53 pm
Ask all the questions you need.

Collectively they are and array of literal values in binary specification, note the leading '0b'.  Specifying them in binary form allows then to be visualized easier.

As to the rest of it it is very worth learning how it works.

Please see this reference page <http://www.cplusplus.com/doc/tutorial/operators/> and read about

'?:'    conditional operator
','     comma operator
'&'     bitwise operator, specifically 'and'
'>>'    bitwise operators, shift-right
'>>='  compound assignment operator

and finally this page <http://www.cplusplus.com/doc/tutorial/control/> and the section "The for loop"
370  Using Arduino / Programming Questions / Re: I need help with two similar Functions. Testing an Int for text result on: December 30, 2012, 03:17:59 pm
Then again perhaps you don't really want a C-string pointer returned as the result of either of these functions.
371  Using Arduino / Programming Questions / Re: I need help with two similar Functions. Testing an Int for text result on: December 30, 2012, 03:16:22 pm
The macro version expands inline using more memory with each expansion thus the following may be of preference.

Code:
inline char* CLUTCHCHECK(int state) {
    static char* szOUT = "Out";
    static char* szIN  = "In";

    return ((0 == (state)) ? szOUT : szIN);
}

inline char* NEUTRALCHECK(int state) {
    static char* szTRUE   = "TRUE";
    static char* szFALSE  = "FALSE";
   
    return ((0 == (state)) ? szTRUE : szFALSE);
}
372  Using Arduino / Programming Questions / Re: I need help with two similar Functions. Testing an Int for text result on: December 30, 2012, 03:15:42 pm
Code:
#define CLUTCHCHECK(N)  ((0 == (N)) ? "Out" : "In")
#define NEUTRALCHECK(N) ((0 == (N)) ? "TRUE" : "FALSE")

int Clutch  = 0;
int Nt      = 0;

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

void loop()
{
// Clutch = buf[CLUTCH] & 0x10;       //clutch     must = IN OR OUT   if clutch = 0 it is out if clutch = 1 it is in

  const char*   szClutch = CLUTCHCHECK(Clutch);
  Serial.println(Clutch);
  Serial.println(szClutch);
  Serial.println();
 
  Clutch = ((Clutch == 0) ? 1 : 0);


// Nt = buf[NEUTRAL] & 0x02;          //neutral    MUST = TRUE OR FALSE (2 = false) (0 = true)

  const char*   szNeutral = NEUTRALCHECK(Nt);
  Serial.println(Nt);
  Serial.println(szNeutral);
  Serial.println();
 
  Nt = ((Nt == 0) ? 2 : 0);
}
373  Using Arduino / Programming Questions / Re: I need help with two similar Functions. Testing an Int for text result on: December 30, 2012, 01:46:06 pm
Code:
#define CLUTCHCHECK(N) ((0 == (N)) ? "Out" : "In")
#define NEUTRALCHECK(N) ((0 == (N)) ? "TRUE" : "FALSE")
374  Using Arduino / Programming Questions / Re: a common cathode seven segment display library on: December 30, 2012, 08:36:23 am
Congratulations!

What kind of comments are you looking for?

Now that you've posted that I'll post this one; compiles but otherwise untested.

Code:
#define ENTRY_COUNT(ARRAY)  (sizeof(ARRAY) / sizeof(ARRAY[0]))

class digit_t
{
    union {
        uint8_t _pins[7];
        struct  { uint8_t _A, _B, _C, _D, _E, _F, _G; };
    };

    digit_t();

public:
    digit_t(uint8_t pinA, uint8_t pinB, uint8_t pinC, uint8_t pinD, uint8_t pinE, uint8_t pinF, uint8_t pinG)
        : _A(pinA), _B(pinB), _C(pinC), _D(pinD), _E(pinE), _F(pinF), _G(pinG)
    {
        for ( int i = ENTRY_COUNT(_pins); i--; )
        {
            pinMode(_pins[i], OUTPUT);
        }
    }

    void display(uint8_t const value)
    {
        const uint8_t   segments[] =
        {
          //     ABCDEFG
              0b01111110    // 0
            , 0b00110000    // 1
            , 0b01101101    // 2
            , 0b01111001    // 3
            , 0b00110011    // 4
            , 0b01011011    // 5
            , 0b01011111    // 6
            , 0b01110000    // 7
            , 0b01111111    // 8
            , 0b01110011    // 9
        };

        for ( uint8_t mask = 0b01000000, i = 0; mask; mask>>= 1, i++ )
        {
            digitalWrite(_pins[i], ((segments[value] & mask) ? HIGH : LOW));
        }
    }
};

digit_t     digit(13, 12, 11, 10, 9, 8, 7);

void loop()
{
    int     value;
    value = analogRead(0);
    value = map(value, 0, 1023, 0, 9);  // remap the sensor readings to 0-9
    digit.display(value);               // display the sensor readings on seven segment display
}

void setup()
{}
375  Using Arduino / Programming Questions / Re: I'm not exactly sure how to put this...I'm new to this on: December 30, 2012, 01:59:19 am
I recommend you keep your note event data together, this keep them in sync.

Also the count of events can be determined, and applied, at compile time,

Code:
#include "pitches.h"

#define ENTRY_COUNT(ARRAY)    (sizeof(ARRAY) / sizeof(ARRAY[0]))

struct event_t
{
    int _note;
    int _length;
    int _pin;
};

int button = 2;
int led1 = 13;
int led2 = 12;
int led3 = 11;
int led4 = 10;

int speaker = 8;

event_t sequence[] =
{
      { NOTE_C4, 4, led1 }
    , { NOTE_G3, 8, led2 }
    , { NOTE_G3, 8, led2 }
    , { NOTE_A3, 4, led3 }
    , { NOTE_G3, 4, led2 }
    , {       0, 4, led4 }
    , { NOTE_B3, 4, led1 }
    , { NOTE_C4, 4, led1 }
};


void setup()
{
    Serial.begin(9600);
    
    pinMode(button, INPUT_PULLUP);
    pinMode(led1, OUTPUT);
    pinMode(led2, OUTPUT);
    pinMode(led3, OUTPUT);
    pinMode(led4, OUTPUT);
    pinMode(speaker, OUTPUT);
}

void loop()
{
    int state = digitalRead(button);
    Serial.println(state);

    delay(5);
    
    if ( state == 0 )
    {
        int noteDuration;
        int pauseBetweenNotes;

        for ( int i = 0; i < ENTRY_COUNT(sequence); i++)
        {
            noteDuration        = 1000 / sequence[i]._length;
            pauseBetweenNotes   = noteDuration * 1.30;
            
            digitalWrite(sequence[i]._pin, HIGH);

            tone(8, sequence[i]._note, noteDuration);

            digitalWrite(sequence[i]._pin, LOW);
            delay(pauseBetweenNotes);
        }
    }
}
Pages: 1 ... 23 24 [25] 26 27 ... 52