Show Posts
Pages: 1 ... 30 31 [32] 33 34 ... 52
466  Using Arduino / Programming Questions / Re: Help with Combining 2 Codes into 1 on: August 04, 2012, 05:00:55 pm
The constants are readable, unmodifiable (at run time) synonyms for numerical values.  They allow for a more human readable code than hardcoded numbers often seen in source code.  Which would you rather see in source code.

Code:
digitalWrite(13, HIGH);

or

Code:
digitalWrite(pinLED_ON_BOARD, LED_ON);

me I'd much rather read the second.

You can specify both 'pinLED_ON_BOARD' and 'LED_ON' specifying the following previous to there use.

Code:
const uint8_t    pinLED_ON_BOARD    = 13;

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

digitalWrite(pinLED_ON_BOARD, LED_ON);

Much easier than "guessing" what '13' is!


As to a tutorial -

<http://arduino.cc/en/Tutorial/Knob>
467  Using Arduino / Programming Questions / Re: Help with Combining 2 Codes into 1 on: August 04, 2012, 12:01:48 am
I'm going to punt (for now) and suggest you read the following (yes the whole page) and come back and we can discuss it if you still don't get what's happening and why.

<http://www.cplusplus.com/doc/tutorial/constants/>
468  Using Arduino / Programming Questions / Re: Help with Combining 2 Codes into 1 on: August 03, 2012, 09:37:04 pm
Is pinMode in void setup needed for these 2:

const uint8_t           pinPotentiometer    = 0;
const uint8_t           pinServo            = 9;

or No, as one is taken care of by the analogRead(),
and the other by the servo.attach()?

I believe you can keep constants in flash while hard-coded indexes use RAM.
But it's cycles slower through flash. Printing bytes to serial from flash, the text 'flows'.



While I'm not entirely sure what you're try to say I do believe it has nothing to do with the quoted code.

Code:
const uint8_t           pinPotentiometer    = 0;
const uint8_t           pinServo            = 9;

These define compile time constant that take up no memory as variables on the stack, nor do they take up room in any EPROM or Flash memory space.

They are compiled directly into the 'machine' code the source code references and compiles to.
469  Using Arduino / Programming Questions / Re: Help with Combining 2 Codes into 1 on: August 03, 2012, 09:24:17 pm
If you'll look at the 'analogRead' examples you'll note none of them do a 'setMode' for the analog pins so it appears during whatever setups up the board before it gets to 'setup' or 'loop' does it for you.  So the answer is NO you do NOT need to do so yourself.

Next if you'll look at the source for the examples you'll see the same thing there as well.  Or looking at the  Servo library source for 'attach' you'll note it does a 'setMode' to 'OUTPUT' when called using the pin number passed in.

So to me it looks like things are good to go!
470  Using Arduino / Programming Questions / Re: Help with Combining 2 Codes into 1 on: August 03, 2012, 12:35:03 am
No attempt was made to compile or test the following -

Code:
// potentiometer wiper (middle terminal) connected to analog pin 0
// outside leads to ground and +5V

#include <Servo.h>

// --- constants

const uint8_t           pinPotentiometer    = 0;
const uint8_t           pinServo            = 9;

const unsigned long     ONE_SECOND          = 1000UL;
const unsigned long     FIVE_SECONDS        = 5UL * ONE_SECOND;


// -- variables

Servo servo;


// --- subroutines

void reportPot()
{
    int     val;

    val = analogRead(pinPotentiometer);
    Serial.println(val);
}

void setSpeed(int speed)
{
    // speed is from 0 to 100 where 0 is off and 100 is maximum speed
    // the following maps speed values of 0-100 to angles from 0-180,
    // some speed controllers may need different values, see the ESC instructions

    int     angle;

    angle = map(speed, 0, 100, 0, 180);
    servo.write(angle);   
}


void arm()
{
    // arm the speed controller, modify as necessary for your ESC 

    setSpeed(0);


    // some speed controllers may need longer

    delay(ONE_SECOND);
}


// --- required 'setup' and 'loop'

void loop()
{
    // read a potentiometer and display the result to the console

    reportPot();
   

    // sweep up from 0 to to maximum speed in 20 seconds

    for ( int speed = 0; speed <= 100; speed += 5 )
    {
        setSpeed(speed);
        delay(ONE_SECOND);
    }


    // sweep back down to 0 speed.

    for ( int speed = 95; speed > 0; speed -= 5 )
    {
        setSpeed(speed);
        delay(ONE_SECOND);
    }


    // stop the motor for a time

    setSpeed(0); 
    delay(FIVE_SECONDS);
}


void setup()
{
    // initialize the serial port for debugging output to console

    Serial.begin(9600);

    servo.attach(pinServo);

    arm(); 
}
471  Using Arduino / Programming Questions / Re: 3x3x3 led cube program slow down and stop on: July 30, 2012, 08:12:36 pm
It's always useful to post complete modified code when discussing cause and effect results.

Code:
void loop ()
{
... IT WON"T WORK HERE AS THE ARRAY IS BUILT FROM POSSIBLY DYNAMIC DATA

    // light each face
    byte planes[] = {cube.getLevels()+1,cube.getLevels(),cube.getLevels()*2,1};

...

    // diamond and box
    static byte diamond[] = {0,4,  1,1, 1,3, 1,4, 1,5, 1,7,   2,4};
    static byte box[] = {
        2,0, 2,1, 2,2, 2,3, 2,5, 2,6, 2,7, 2,8,
        1,0, 1,2, 1,6, 1,8,
        0,0, 0,1, 0,2, 0,3, 0,5, 0,6, 0,7, 0,8
    };

...

    // helicopter effect
    static byte topSeq[8] = {0,3,6,7,8,5,2,1};
    static byte botSeq[8] = {8,5,2,1,0,3,6,7};

... IT WON"T WORK HERE AS THE ARRAY IS BUILT FROM POSSIBLY DYNAMIC DATA

            byte seq[] = {2,topSeq[s], 1,4, 0,botSeq[s]};

...

}

472  Using Arduino / Programming Questions / Re: 3x3x3 led cube program slow down and stop on: July 30, 2012, 07:27:55 pm
I think you're doing just fine.  You're learning, not giving up and that's good!
473  Using Arduino / Programming Questions / Re: 3x3x3 led cube program slow down and stop on: July 30, 2012, 07:17:02 pm
Code:
static byte diamond[] = {0,4,  1,1, 1,3, 1,4, 1,5, 1,7,   2,4};
static byte box[] = {
        2,0, 2,1, 2,2, 2,3, 2,5, 2,6, 2,7, 2,8,
        1,0, 1,2, 1,6, 1,8,
        0,0, 0,1, 0,2, 0,3, 0,5, 0,6, 0,7, 0,8
    };
    
    // helicopter effect
static byte topSeq[8] = {0,3,6,7,8,5,2,1};
static byte botSeq[8] = {8,5,2,1,0,3,6,7};

<http://en.wikipedia.org/wiki/Static_variable>
474  Using Arduino / Programming Questions / Re: 3x3x3 led cube program slow down and stop on: July 30, 2012, 06:57:20 pm
Better yet would be to make them 'static"
475  Using Arduino / Programming Questions / Re: const + struct + array on: July 29, 2012, 12:13:20 pm
Code:
const size_t    N_SLAVES    = 3;

struct STATIONS
{
    int     a;
    int     b;
    float   c;
};

const STATIONS station[N_SLAVES] = {
      { 1, 2, 3.1f }        // station[0]
    , { 1, 2, 3.1f }        // station[1]
    , { 1, 2, 3.1f }        // station[2]
};

int     a = station[0].a;
int     b = station[0].b;
float   c = station[0].c;
476  Using Arduino / Programming Questions / Re: Very long Delay on: July 26, 2012, 03:52:37 pm
Untested or compiled but something along the line of -

Code:
const uint8_t       pinWATER_PUMP_RELAY         = 13;

const uint8_t       WATER_PUMP_OFF              = LOW;
const uint8_t       WATER_PUMP_ON               = HIGH;

const unsigned long ONE_SECOND                  = 1000UL;
const unsigned long ONE_MINUTE                  = 60UL * ONE_SECOND;
const unsigned long ONE_HOUR                    = 60UL * ONE_MINUTE;
const unsigned long TWELVE_HOURS                = 12UL * ONE_HOUR;
const unsigned long WATER_PUMP_TIME_ON          = 5UL  * ONE_MINUTE;
const unsigned long WATER_PUMP_TIME_NEXT_CYCLE  = TWELVE_HOURS - WATER_PUMP_TIME_ON;

void setPump(uint8_t state)
{
    digitalWrite(pinWATER_PUMP_RELAY, state);
}

void cycleWaterPump()
{
    setPump(WATER_PUMP_ON);

    delay(WATER_PUMP_TIME_ON);

    setPump(WATER_PUMP_OFF);
}

void loop()
{
    cycleWaterPump();
    delay(WATER_PUMP_TIME_NEXT_CYCLE);
}

void setup()
{
    pinMode(pinWATER_PUMP_RELAY, OUTPUT);

    setPump(WATER_PUMP_OFF);
}


Or a slightly different way to pass the time -

Code:
const uint8_t       pinWATER_PUMP_RELAY         = 13;

const uint8_t       WATER_PUMP_OFF              = LOW;
const uint8_t       WATER_PUMP_ON               = HIGH;

const unsigned long ONE_SECOND                  = 1000UL;
const unsigned long ONE_MINUTE                  = 60UL * ONE_SECOND;
const unsigned long ONE_HOUR                    = 60UL * ONE_MINUTE;
const unsigned long TWELVE_HOURS                = 12UL * ONE_HOUR;
const unsigned long WATER_PUMP_TIME_ON          = 5UL  * ONE_MINUTE;
const unsigned long WATER_PUMP_TIME_NEXT_CYCLE  = TWELVE_HOURS - WATER_PUMP_TIME_ON;


// time in milliseconds to trigger next cycle

unsigned long       tmNextTrigger;


// See <http://arduino.cc/playground/Code/TimingRollover>

bool hasTimeExpired(const unsigned long& tmsTarget)
{
    return ((long)(millis() - tmsTarget) >= 0);
}

void setPump(uint8_t state)
{
    digitalWrite(pinWATER_PUMP_RELAY, state);
}

void cycleWaterPump()
{
    setPump(WATER_PUMP_ON);

    delay(WATER_PUMP_TIME_ON);

    setPump(WATER_PUMP_OFF);
}

void loop()
{
    if ( hasTimeExpired(tmNextTrigger) )
    {
        cycleWaterPump();

        tmNextTrigger += WATER_PUMP_TIME_NEXT_CYCLE;
    }
}

void setup()
{
    tmNextTrigger   = millis();

    pinMode(pinWATER_PUMP_RELAY, OUTPUT);

    setPump(WATER_PUMP_OFF);
}
477  Using Arduino / Programming Questions / Re: Initializing global variables - where? on: July 19, 2012, 12:01:46 am
Slightly expanded structure to illustrate initialization of instance of global structures -

Code:
struct fooType
{
    byte    bar;
    float   tab;
};

fooType foo = { 1, 12.05f };
478  Using Arduino / Programming Questions / Re: Problem: subraction giving very strange results on: June 05, 2012, 04:56:09 pm
No the Linker will strip what the program doesn't use.
479  Using Arduino / Programming Questions / Re: BBEdit v10 and syntax highlighting on: June 03, 2012, 10:19:27 pm
Depending upon your version of BBEdit the following should be used as a rough guide.

BBEdit->Preferences

Select "Languages" from the list on the left.

Click the '+' under the "Custom Extension Mappings" pane.

Select the "Suffix" side of the new entry and enter extension used by your version the Arduino IDE.

Click on the "Languages" side and select "C++" form the pop-up menu.

You should be good to go!
480  Using Arduino / Programming Questions / Re: Creating an array on: May 27, 2012, 10:57:44 pm
I don't understand why you want to do this and think your description of what you think you did makes any sense either.

Code:
const size_t    ARRAY_LENGTH    = 1500;

int array[ARRAY_LENGTH];

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

    for ( size_t index = 0; index < ARRAY_LENGTH; ++index )
    {
        array[index]    = index;
    }
}

void loop()
{
    for ( size_t index = 0 ; index < ARRAY_LENGTH; ++index )
    {
        Serial.println(array[index]);
    }
}
Pages: 1 ... 30 31 [32] 33 34 ... 52