Show Posts
Pages: 1 2 [3] 4 5 ... 52
31  Using Arduino / Programming Questions / Re: Error tring to send an array to function on: June 22, 2013, 11:45:21 pm
Why hardcode values that can be determined at compile time -

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

int array[][3]  =
{
      { 1, 1, 1 }
    , { 1, 1, 1 }
    , { 1, 1, 1 }
    , { 1, 1, 1 }
};

const size_t    kROWS       = ARRAY_ENTRIES(array);
const size_t    kCOLUMNS    = ARRAY_ENTRIES(array[0]);

EDIT: Removed earlier post as I thought I'd made a mistake.  Looked it over and realized it was correct so ...
32  Using Arduino / Programming Questions / Re: Error tring to send an array to function on: June 22, 2013, 09:56:06 pm
Why pass the address of a global array to a function?
33  Using Arduino / Programming Questions / Re: for (int i =... easy peasy... on: June 20, 2013, 12:20:18 am
Indeed it did, corrected above.  I really should be more careful when I'm not feeling well.
34  Using Arduino / Programming Questions / Re: for (int i =... easy peasy... on: June 19, 2013, 02:37:03 pm
Is -

Code:
lastBright[i] = lastBright[i] / 100 * fadespeed;

... meant to be ...

Code:
lastBright[i] = ((lastBright[i] / 100) * fadespeed);

... or its actaul evaluation ...

Code:
lastBright[i] = (lastBright[i] / (100 * fadespeed));


And from your above post was this what you meant -

Code:
   static int lastBright[16] = { 0 };                       // set the lastbright variable

    // main detect, light and fade routine
    for ( int i = 0; i < 16; i++ )
    {
        if ( mappedValue[i] < sensitivity )                 // check to see if the ir is detecting above the base level
        {
            unsigned long currentMillis = millis();
            if ((lastBright[i] > 0) && (currentMillis - previousMillis > interval)) // check to see if the last brightness is more than 0 and the interval time has passed
            {
                lastBright[i] = lastBright[i] / 100 * fadespeed;    // Dim the light til it reaches 0
            }

            previousMillis = currentMillis;
        }
        else
        {
            lastBright[i] = maxBright;                  //  Set the brightness to full
        }

        Tlc.set(led[i], lastBright[i]);                 // set the brightness of the led1
        Tlc.update();                           // send the data to the tlc
    }
35  Using Arduino / Programming Questions / Re: would like some feedback if possible on code. on: June 18, 2013, 09:42:56 pm
This is your code without the unnecessary extra braces.
Does it still do what you think it does?

Code:
#include <SPI.h>
#include <NewPing.h>

#define TRIGGER_PIN      2
#define ECHO_PIN         4
#define MAX_DISTANCE    30

int slave       = 10;
int flexpin     = A0;
int flexpin1    = A1;
int flexpin2    = A2;
int flexpin3    = A3;
int flex[20];
int flex1[20];
int flex2[20];
int flex3[20];
int flexsum     = 0;
int flexsum1    = 0;
int flexsum2    = 0;
int flexsum3    = 0;
int val         = 0;
int val1        = 0;
int val2        = 0;
int val3        = 0;
int val4        = 0;
int cm;

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

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

    SPI.begin();

    pinMode(slave, OUTPUT);
}
void setpot(int reg, int level)
{
    digitalWrite(slave, LOW);

        SPI.transfer(reg);
        SPI.transfer(level);

    digitalWrite(slave, HIGH);
}

void loop()
{
    for ( int x = 0; x < 20; x++ )
    {
        flex        = analogRead(flexpin);
        flex1       = analogRead(flexpin1);
        flex2       = analogRead(flexpin2);
        flex3       = analogRead(flexpin3);

        flexsum     = flexsum + analogRead(flexpin);
        flexsum1    = flexsum1 + analogRead(flexpin1);
        flexsum2    = flexsum2 + analogRead(flexpin2);
        flexsum3    = flexsum3 + analogRead(flexpin3);

        delayMicroseconds(14);
    }

    flexsum = flexsum / 20;
    flexsum1 = flexsum1 / 20;
    flexsum2 = flexsum2 / 20;
    flexsum3 = flexsum3 / 20;

    // ---

    Serial.println(flexsum);
    Serial.println(flexsum1);
    Serial.println(flexsum2);
    Serial.println(flexsum3);

    delay(500);

    // ---

    delay(40);

    unsigned int uS = sonar.ping();
    int cm  = (uS / US_ROUNDTRIP_CM);
    Serial.print("Ping: ");
    Serial.print(uS / US_ROUNDTRIP_CM);
    Serial.println("cm");


    int val = cm;
    val     = map(val, 5, 25, 0, 200);
    val     = constrain(val, 5, 200);
    setpot(0, val);

    int val1 = flexsum;
    if ( val1 > 630 )
    {
        int val1    = map(val1, 730, 830, 62, 127);
        setpot(1, val1);
    }
    else
    {
        int val2    = flexsum1;
        val2        = map(val2, 740, 860, 61, 10);
        setpot(1, val2);
    }

    int val3 = flexsum3;
    if ( val3 > 570 )
    {
        int val3    = map(val3, 570, 770, 62, 127);
        setpot(2, val3);
    }
    else
    {
        int val4    = flexsum2;
        val4        = map(val4, 630, 820, 61, 10);
        setpot(2, val4);
    }
}

long mic2sec(long microseconds)
{
    return microseconds / 29 / 2;
}
36  Using Arduino / Programming Questions / Re: Using Explicit on: June 18, 2013, 05:50:09 pm
In order for global instances to work on all Arduino API compatible implementation you really should implement the 'begin' method.

Some Arduino API implementations  do API initialization within 'main's 'init' method meaning it ould undo the work of 'pinMode' as specified within your constructor.

By doing it this way and only using documented API function your library should be usable across many "API compatible" boards on the market.

Code:
   ...

    Morse(uint8_t const pin)
        : _pin(pin)
    {}

    void begin()
    {
        pinMode(_pin, OUTPUT);
    }
    ...
37  Using Arduino / Programming Questions / Re: Using Explicit on: June 16, 2013, 11:32:00 pm
Don't see it as a problem myself.

Code:
class morse_t
{
    const uint8_t   _pin;

public:
    morse_t(uint8_t const pin)
        // use initialization list, 'const's must be init'd first
        : _pin(pin)
    {}

    void begin()
    {
        pinMode(_pin, OUTPUT);
    }

    void dot();
    void dash();
};

morse_t     morse0(13);     // valid code
morse_t     morse1 = 13;    // valid code

void morse_t::dot()
{   }

void morse_t::dash()
{   }


void loop()
{    }

void setup()
{
    morse0.begin();
    morse1.begin();
}
38  Using Arduino / Programming Questions / Re: achieving multiple functions simultaneously on: June 16, 2013, 12:49:01 pm
Remove the use of 'delay' and instead note the time using 'millis' and see if the amount of time formality specified to 'delay' has passed or not.  If it has not passed do something else.  If it has passed do what was needed at that time and then the something else.
39  Using Arduino / Programming Questions / Re: expressing a statement most compact method on: June 16, 2013, 10:35:29 am
Yes I know by it got posed prematurely by accident then I lost my internet connection for the evening and I was unable to remove, correct it.

My apologies
40  Using Arduino / Programming Questions / Re: expressing a statement most compact method on: June 16, 2013, 12:18:42 am
Code:
const uint8_t pins[] = { 11, 10,  9,  8 };
for ( size_t i = sizeof(pins); i--; )
{
    val |= digitalRead(pins[i]) << 1;
}
41  Using Arduino / Programming Questions / Re: LED Light Sequence -HELP on: June 15, 2013, 11:35:58 pm
As I believe that in starting to learn programming it can be very useful to see an implementation of something you've specified I give you this.

In return I expect you to research what you see and asking questions until you understand how it works.

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

const uint8_t   pinLED_1    = 5;
const uint8_t   pinLED_2    = 6;
const uint8_t   pinLED_3    = 7;
const uint8_t   pinLED_4    = 8;

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

void toggleLED(uint8_t pin, unsigned long tmsOn)
{
    digitalWrite(pin, LED_ON);
    delay(tmsOn);

    digitalWrite(pin, LED_OFF);
}

void loop()
{
    const uint8_t   pinLEDS[] =
    {
          pinLED_1, pinLED_2, pinLED_1, pinLED_2
        , pinLED_3, pinLED_4, pinLED_3, pinLED_4
    };

    for ( size_t i = 0; i < ARRAY_SIZEOF(pinLEDS); i++ )
    {
        toggleLED(pinLEDS[i], 500UL);
    }
}

void setup()
{
    const uint8_t   pinLEDS[]   = { pinLED_1, pinLED_2, pinLED_3, pinLED_4 };

    for ( size_t i = ARRAY_SIZEOF(pinLEDS); i--; )
    {
        pinMode(pinLEDS[i], OUTPUT);
    }
}
42  Using Arduino / Programming Questions / Re: RGB and LightSensor Code on: June 15, 2013, 05:52:31 pm
Your code cleaned up and in a compilable and consistent form where comments match function definitions.

Code:
int lightPin    =  0;   //PhotoResistor Pin

int greenPin    = 11;
int bluePin     = 10;
int redPin      =  9;

boolean sensorState = false;

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

    pinMode(greenPin, OUTPUT);
    pinMode(bluePin, OUTPUT);
    pinMode(redPin, OUTPUT);

    digitalWrite(greenPin, HIGH);
    digitalWrite(bluePin,  HIGH);
    digitalWrite(redPin, HIGH);
}

void loop()
{
    setColor(0, 255, 0);    // green
    delay(5000);

    setColor(0, 0, 255);    // blue
    delay(10000);

    setColor(255, 0, 0);    // red
    delay(15000);
}


void setColor(int red, int green, int blue)
{
    analogWrite(redPin, red);
    analogWrite(greenPin, green);
    analogWrite(bluePin, blue);

    int threshold = 300;
    if ( (sensorState == true) && (analogRead(lightPin) > threshold) )
    {
        Serial.println("sensor blocked");

        sensorState = false;
        delay(50);
    }
    else if ( sensorState == false )
    {
        Serial.println("sensor open");
    }
}
43  Using Arduino / Programming Questions / Re: 6 Leds with fade and lots of fun on: June 15, 2013, 12:00:33 pm
TIm,

You're experimenting and that is good thing but I'm going to spoil your project just a bit and in the process you'll perhaps learn a few things for your next experiment.

The key to learning is asking questions followed by a search for the answers.

You see something you don't understand seek the answer till you do.

Code:
/*
   Fade

   This example shows how to fade multiple LED's using an array of pins to which
   LED's are attached and analogWrite() function.

   This example code is in the public domain.
*/

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

const uint8_t   pinsLEDS[]      = { 2, 3, 4, 5, 6, 7 };
int             brightness[]    = { 0, 0, 0, 0, 0, 0 };
int             deltaFade       = 5;
size_t          indexElement    = 0;

void loop()
{
    analogWrite(pinsLEDS[indexElement], brightness[indexElement]);

    brightness[indexElement] += deltaFade;
    brightness[indexElement] %= 256;

    ++indexElement;
    indexElement %= ARRAY_SIZEOF(pinsLEDS);

    delay(10UL);
}

void setup()
{
    for ( size_t i = 0; i < ARRAY_SIZEOF(pinsLEDS); i++ )
    {
        pinMode(pinsLEDS[i], OUTPUT);
    }
}
44  Using Arduino / Programming Questions / Re: pushbutton counter help on: June 14, 2013, 12:51:12 am
See if you get anything out this -

Code:
const uint8_t   pushButtonPin   =  2;

struct button_t
{
    const uint8_t   _pin;
   
    uint8_t         _count;
    uint8_t         _statePrevious;
};

button_t        button = { pushButtonPin,  0, LOW };

uint8_t button_count(struct button_t* pButton)
{
    uint8_t     state;

    state = digitalRead(pushButtonPin);
    if ( state != pButton->_statePrevious )
    {
        if ( state == HIGH )
        {
            pButton->_count++;
        }
    }

    pButton->_statePrevious = state;
   
    return pButton->_count;
}

void funct2(void)   {}
void funct1(void)   {}
void funct0(void)   {}

void loop()
{
    switch ( button_count(&button) )
    {
        case 0:
            // ... bever pressed, or was 'reset' ...
            funct0();
            break;

        case 1:
            // ... pressed once ...
            funct1();
            break;

        case 2:
            // ... pressed twice ...
            funct2();
            break;

        // ... more as required ...

        default:
            // ... if not a handled 'case' reset 'button's '_count'er value ...
            button._count = 0;
            break;
    }
}

void setup()
{
    pinMode(button._pin, INPUT);
}

45  Using Arduino / Programming Questions / Re: More efficent coding on: June 12, 2013, 10:28:04 am
True enough but bad habits can lead to long debugging sessions thus wasting time when one forgets and goes to use out of range integer literals without thinking.
Pages: 1 2 [3] 4 5 ... 52