Show Posts
Pages: [1] 2 3 ... 52
1  Using Arduino / Programming Questions / Re: Variables scope on: December 04, 2013, 10:12:59 pm
By default, avr-gcc will initialize variables to zero (or the equivalent, depending on type), so giving them an explicit initial value of zero does nothing but consume flash storage.

True for global variables only but NOT for local variables.  Local variables should ALWAYS be initialized by the programmer before any use other than assignment!
2  Using Arduino / Programming Questions / Re: advantage of voltile keyword on: July 20, 2013, 12:08:08 am
'voltile' tells the compiler that hardware, or interrupt code, likely changed the variable while it wasn't looking so it doesn't cache it and rereads it when it is referenced again instead of using it's cached copy.
3  Using Arduino / Programming Questions / Re: getting specific bits from bytes.How? on: July 19, 2013, 05:18:37 pm
Difficult to understand - hardly!  The code is short and easy to learn from.  If something is not understood ask.  That is after all what this forum is for!

The code is a working solution but obviously NOT meant to simply drop in without understanding it.  I recommend no one use code they don't understand and it was posted with the intention that someone here learns something.

I'm beginning to get very annoyed with the way people "teach" around here.
4  Using Arduino / Programming Questions / Re: getting specific bits from bytes.How? on: July 19, 2013, 01:13:11 pm
Reply 11 gives you correct working code.

No magic numbers instead uses ANSI supplied constant for the number of bits in a char.

No overhead of a function call.

All temporary variables were limited to the minimum scope for the 'for' loop.
5  Using Arduino / Programming Questions / Re: getting specific bits from bytes.How? on: July 18, 2013, 10:20:44 pm
Code:
uint8_t temp[] =
{
      0b10000000
    , 0b00100000
    , 0b00000000
    , 0b00010000
    , 0b10000100
    , 0b01001100
    , 0b10000011
    , 0b01000111
};

uint8_t value = 0;

for ( uint8_t bit = 1 << (CHAR_BIT - 1), i = 0; bit; bit >>= 1, i++ )
{
    value |= (bit & temp[i]);
}

EDIT:

'export' is a keyword and my compiler doesn't like it so used 'value' as name instead.
6  Using Arduino / Programming Questions / Re: Switch Case Using Touchscreen on: July 18, 2013, 07:13:13 pm
Some useful functionality might include -

    • point_t class to handle point (yes something different than that of the touches lib)

    • rect_t class specifying a rect with perhaps a 'point_in_rect' method.

    • a button class that makes use of 'point_t' and 'rect_t' for hit testing implementing the touch point down was within the buttons 'rect' and the touch point up was also within the button rect to determine a successful button press.


These don't have to be complicated but will make for more modular, readable, maintainable and debuggable code.
7  Using Arduino / Programming Questions / Re: Switch Case Using Touchscreen on: July 18, 2013, 11:16:32 am
Simplified, on one level at least!

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

const int frequencies[] = { 1000, 750, 500, 250 };

if ( (p.x > 110) && (p.x < 235) )
{
    if ( (p.y > 5) && (p.y < 95) )
    {
        ifrq++;
        ifrq %= ARRAY_ENTRIES(frequencies);

        frq = frequencies[ifrq];
    }
}

You should be able to generalize your button hit testing and similarly simplify hit testing through out your code.
8  Using Arduino / Programming Questions / Re: Setting struct in a public constant of a class on: July 17, 2013, 07:45:01 pm
How about a little more concrete example of something you plan on doing as a starting point around which to base an answer around.
9  Using Arduino / Programming Questions / Re: Turn relay for time on: July 17, 2013, 06:22:01 pm
Congratulations!  Wanting is often a great start for learning something new.

Tutorial at - <http://arduino.cc/en/Tutorial/HomePage>
10  Using Arduino / Programming Questions / Re: BIG PROGRAM: Bath Overflow Error With /*...*/ When Preprocessing !! on: July 17, 2013, 01:06:20 pm
Never been a fan of "comment enclosure" to "hide" code from the compiler for much the same reason.

Code:
#if 0
    … code to be remove from compilation …
#endif
11  Using Arduino / Programming Questions / Re: Setting struct in a public constant of a class on: July 16, 2013, 10:55:10 pm
I'm not sure you know what you're doing or even think you know what you're doing, and even if you do you haven't communicated your intentions or how you plan to use this class so that we can properly assist.
12  Using Arduino / Programming Questions / Re: LOW = HIGH and HIGH = LOW on: July 16, 2013, 06:48:39 pm
If it causes you problems there is always a simple code 'fix'.

Code:
const uint8_t   pinLED_1    = 2;
const uint8_t   pinLED_2    = 3;
const uint8_t   pinLED_3    = 4;
const uint8_t   pinLED_4    = 5;
const uint8_t   pinLED_5    = 6;

const uint8_t   LED_OFF     = HIGH;
const uint8_t   LED_ON      = LOW;

void setup()
{
    pinMode(pinLED_1, OUTPUT);
    pinMode(pinLED_2, OUTPUT);
    pinMode(pinLED_3, OUTPUT);
    pinMode(pinLED_4, OUTPUT);
    pinMode(pinLED_5, OUTPUT);
}

void loop()
{
    digitalWrite(pinLED_1, LED_ON);
    digitalWrite(pinLED_2, LED_ON);
    digitalWrite(pinLED_3, LED_ON);
    digitalWrite(pinLED_4, LED_ON);
    digitalWrite(pinLED_5, LED_ON);
    delay(1000);

    digitalWrite(pinLED_1, LED_OFF);
    digitalWrite(pinLED_2, LED_ON);
    digitalWrite(pinLED_3, LED_ON);
    digitalWrite(pinLED_4, LED_ON);
    digitalWrite(pinLED_5, LED_ON);
    delay(1000);

    digitalWrite(pinLED_1, LED_OFF);
    digitalWrite(pinLED_2, LED_OFF);
    digitalWrite(pinLED_3, LED_ON);
    digitalWrite(pinLED_4, LED_ON);
    digitalWrite(pinLED_5, LED_ON);
    delay(1000);

    digitalWrite(pinLED_3, LED_OFF);
    digitalWrite(pinLED_4, LED_ON);
    digitalWrite(pinLED_5, LED_ON);
    delay(1000);

    digitalWrite(pinLED_4, LED_OFF);
    digitalWrite(pinLED_5, LED_ON);
    delay(1000);

    digitalWrite(pinLED_5, LED_OFF);
    delay(2000);

    digitalWrite(pinLED_5, LED_ON);
    delay(1000);

    digitalWrite(pinLED_4, LED_ON);
    delay(1000);

    digitalWrite(pinLED_3, LED_ON);
    delay(1000);

    digitalWrite(pinLED_2, LED_ON);
    delay(1000);

    digitalWrite(pinLED_1, LED_ON);
    delay(1000);
}
13  Using Arduino / Programming Questions / Re: 4 Buttons - 4 Relays - Help needed with code on: July 15, 2013, 11:19:11 am
Code changed above so that array members are initialized using circuit_t constructor.

EDIT:
Quote
I did swap uint8_t for a byte.

Why?  'digitalRead',  'digitalWrite' and others are declared as 'int digitalRead(uint8_t pin)' not 'byte'.  Although 'byte' is 'typedef uint8_t byte'.
14  Using Arduino / Programming Questions / Re: 4 Buttons - 4 Relays - Help needed with code on: July 14, 2013, 06:53:01 pm
Off the top of my head not compiled and completely untested.

Code:
// <http://forum.arduino.cc/index.php?topic=177410.0>

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

const uint8_t   pinRELAY_1  = 1;
const uint8_t   pinRELAY_2  = 8;
const uint8_t   pinRELAY_3  = 7;
const uint8_t   pinRELAY_4  = 0;

const uint8_t   pinBUTTON_1 = A0;
const uint8_t   pinBUTTON_2 = A1;
const uint8_t   pinBUTTON_3 = A2;
const uint8_t   pinBUTTON_4 = A3;

const uint8_t   BUTTON_UP   = LOW;
const uint8_t   BUTTON_DOWN = HIGH;

struct circuit_t
{
private:
    const uint8_t   _pinRELAY;
    const uint8_t   _pinBUTTON;
    const uint8_t   _transition;

    uint8_t         _state;

public:
    circuit_t(uint8_t pinRELAY, uint8_t pinBUTTON, uint8_t transition = BUTTON_UP)
        : _pinRELAY(pinRELAY)
        , _pinBUTTON(pinBUTTON)
        , _transition(transition)
    {
        _state = BUTTON_UP;
    }
   
    void begin()
    {
        pinMode(_pinRELAY, OUTPUT);
        pinMode(_pinBUTTON, INPUT);
    }

    bool process()
    {
        uint8_t  state = digitalRead(_pinBUTTON);
        if ( state != _state )
        {
            if ( state == _transition )
            {
                digitalWrite(_pinRELAY, state);
            }
        }
       
       return _state = state;
    }
};

circuit_t   circuits[]      =
{
      circuit_t(pinRELAY_1, pinBUTTON_1)
    , circuit_t(pinRELAY_2, pinBUTTON_2)
    , circuit_t(pinRELAY_3, pinBUTTON_3)
    , circuit_t(pinRELAY_4, pinBUTTON_4)
};

void loop()
{
    bool    f;
    for ( size_t i = 0; i < ARRAY_ENTRIES(circuits); i++ )
    {
        f = circuits[i].process();
    }
}

void setup()
{
    for ( size_t i = ARRAY_ENTRIES(circuits); i--; )
    {
        circuits[i].begin();
    }
}


EDIT: Code changes for newer IDE's uses class constructor for array initialization.
15  Using Arduino / Programming Questions / Re: Avoid multiple switch case and if else statements on: July 06, 2013, 11:54:32 am
Read the thread and you'll figure it out.
Pages: [1] 2 3 ... 52