Show Posts
Pages: 1 [2] 3 4 ... 52
16  Using Arduino / Programming Questions / Re: Microcontroler logic on: July 05, 2013, 11:53:11 pm
On the miniature level the button bounces like a trampoline making multiple hits before coming to a solid hit - all in microseconds.

Lookup button "debounce" and the debouncing tutorial here -

<http://arduino.cc/en/Tutorial/Debounce>
17  Using Arduino / Programming Questions / Re: Avoid multiple switch case and if else statements on: July 05, 2013, 11:26:15 pm
Almost anyone can make a magic number laden, whitespace poor, hard to read, debug and modify mess of code.

There - done.

But where is the lesson in that?

Oh, and per the thread title no if (well there is one), else or switch/case statements.

Code:
void blink(unsigned long tms){
    digitalWrite(13, HIGH);
    delay(tms);
    digitalWrite(13, LOW);
}
void send(int n)
{
    const uint8_t morse_encoding[] = {
          0b00011111, 0b00001111, 0b00000111, 0b00000011, 0b00000001
        , 0b00000000, 0b00010000, 0b00011000, 0b00011100, 0b00011110
        , 0b00010101
    };

    for ( int mask = (n < 10) ? 16 : 32; mask; mask >>= 1 ) {
        blink((morse_encoding[n] & mask) ? 300 : 100);
        if ( 1 != mask )
        {
            delay(100);
        }
    }
}
void loop(){
    float fahrenheit = (((((analogRead(A0) * 5.0) / 1024.0) - 0.5) * 100;) * 9.0 / 5.0) + 32.0;
    send(int(fahrenheit) / 10);
    delay(300);
    send(int(fahrenheit) % 10);
    delay(700);
}
void setup(){
    pinMode(13, OUTPUT);
}
18  Using Arduino / Programming Questions / Re: Avoid multiple switch case and if else statements on: July 05, 2013, 05:26:12 pm
Wasn't much additional work then ...

Code:
const float         AREF_VOLTAGE                    = 5.0;              // 5 volt source reference

const uint8_t       pinLED                          = 13;

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

const unsigned long DURATION_BASE                   = 100UL;
const unsigned long DURATION_DOT                    = 1 * DURATION_BASE;
const unsigned long DURATION_DASH                   = 3 * DURATION_BASE;
const unsigned long DURATION_INTRA_CHAR_SPACING     = 1 * DURATION_BASE;
const unsigned long DURATION_INTER_CHAR_SPACING     = 3 * DURATION_BASE;
const unsigned long DURATION_INTER_WORD_SPACING     = 7 * DURATION_BASE;

void blink(unsigned long tms)
{
    digitalWrite(pinLED, LED_ON);

    delay(tms);

    digitalWrite(pinLED, LED_OFF);
}

void send(char ascii)
{
    const uint8_t morse_encoding[] =
    {
        // --- Numeric, 5 bit encoding

          0b00011111    //  0 - '0', "-----"
        , 0b00001111    //  1 - '1', ".----"
        , 0b00000111    //  2 - '2', "..---"
        , 0b00000011    //  3 - '3', "...--"
        , 0b00000001    //  4 - '4', "....-"
        , 0b00000000    //  5 - '5', "....."
        , 0b00010000    //  6 - '6', "-...."
        , 0b00011000    //  7 - '7', "--..."
        , 0b00011100    //  8 - '8', "---.."
        , 0b00011110    //  9 - '9', "----."


        // --- Punctuation, 6 bit enciding

        , 0b00010101    // 10 - '.', ".-.-.-"
    };

    const uint8_t   MASK_LAST       = 0b00000001;
    const size_t    PATTERN_INDEX   = isdigit(ascii) ? '0' - ascii : 10;
    const uint8_t   PATTERN         = morse_encoding[PATTERN_INDEX];

    uint8_t         mask            = isdigit(ascii) ? 0b00010000 : 0b00100000;
    for ( ; mask; mask >>= 1 )
    {
        blink((PATTERN & mask) ? DURATION_DASH : DURATION_DOT);
        if ( MASK_LAST != mask )
        {
            delay(DURATION_INTRA_CHAR_SPACING);
        }
    }
}

void loop()
{
    float   voltage     = (analogRead(A0) * AREF_VOLTAGE) / 1024.0;
    float centigrade    = (voltage - 0.5) * 100;
    float fahrenheit    = (centigrade * 9.0 / 5.0) + 32.0;
    int tens            = int(fahrenheit) / 10;
    int ones            = int(fahrenheit) % 10;

    send('0' + tens);
    delay(DURATION_INTER_CHAR_SPACING);

    send('0' + ones);
    delay(DURATION_INTER_WORD_SPACING);
}

void setup()
{
    pinMode(pinLED, OUTPUT);
}
19  Using Arduino / Programming Questions / Re: Avoid multiple switch case and if else statements on: July 05, 2013, 01:07:52 am
Untested possible starting shell code ...

Code:
// Turn temperature reading into morse numbers. TMP36.
// <http://forum.arduino.cc/index.php?topic=175656.0>

const float         AREF_VOLTAGE                    = 5.0;              // 5 volt source reference

const uint8_t       pinLED                          = 13;

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

const unsigned long DURATION_BASE                   = 100UL;
const unsigned long DURATION_DOT                    = 1 * DURATION_BASE;
const unsigned long DURATION_DASH                   = 3 * DURATION_BASE;
const unsigned long DURATION_INTRA_CHAR_SPACING     = 1 * DURATION_BASE;
const unsigned long DURATION_INTER_CHAR_SPACING     = 3 * DURATION_BASE;
const unsigned long DURATION_INTER_WORD_SPACING     = 7 * DURATION_BASE;

void blink(unsigned long tms)
{
    digitalWrite(pinLED, LED_ON);

    delay(tms);

    digitalWrite(pinLED, LED_OFF);
}

void blink(char ascii)
{
    const uint8_t morse_encoding[] =
    {
        // --- Numeric, 5 bit encoding

          0b00011111    //  0 - '0', "-----"
        , 0b00001111    //  1 - '1', ".----"
        , 0b00000111    //  2 - '2', "..---"
        , 0b00000011    //  3 - '3', "...--"
        , 0b00000001    //  4 - '4', "....-"
        , 0b00000000    //  5 - '5', "....."
        , 0b00010000    //  6 - '6', "-...."
        , 0b00011000    //  7 - '7', "--..."
        , 0b00011100    //  8 - '8', "---.."
        , 0b00011110    //  9 - '9', "----."


        // --- Punctuation, 6 bit enciding

        , 0b00010101    // 10 - '.', ".-.-.-"
    };

    const uint8_t   MASK_LAST       = 0b00000001;
    const size_t    PATTERN_INDEX   = isdigit(ascii) ? '0' - ascii : 10;
    const uint8_t   PATTERN         = morse_encoding[PATTERN_INDEX];

    uint8_t         mask            = isdigit(ascii) ? 0b00010000 : 0b00100000;
    for ( ; mask; mask >>= 1 )
    {
        blink((PATTERN & mask) ? DURATION_DASH : DURATION_DOT);
        if ( MASK_LAST != mask )
        {
            delay(DURATION_INTRA_CHAR_SPACING);
        }
    }
}

void loop()
{
    float   voltage  = (analogRead(A0) * AREF_VOLTAGE) / 1024.0;
    float centigrade = (voltage - 0.5) * 100;
    float fahrenheit = (centigrade * 9.0 / 5.0) + 32.0;

    // ... YOUR CODE HERE ...

    // ... CONVERT TEMPERATURE INTO INDIVISIBLE DIGITS '0' - '9' or '.' INTO ...
    // ... ASCII CHARACTERS THAT ARE THEN PASSED TO 'blink' ONE AT A TIME ...
    // ... SEPARATED BY A CALL TO 'delay(DURATION_INTER_CHAR_SPACING)' ...
    // ... FOLLOWED BY A 'delay(DURATION_INTER_WORD_SPACING)' BEFORE DROPPING OUT OF 'loop'
}

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

    pinMode(pinLED, OUTPUT);
}
20  Using Arduino / Programming Questions / Re: Avoid multiple switch case and if else statements on: July 04, 2013, 03:48:23 pm
I think it should be something along the line of:

Code:
float voltage  = (analogRead(A0) * AREF_VOLTAGE) / 1024.0;
float centigrade = (voltage - 0.5) * 100;
float fahrenheit = (centigrade * 9.0 / 5.0) + 32.0;

... and a global const with ...

Code:
const float AREF_VOLTAGE = REMBER_TO_REPLACE_WITH_VOLTAGE_REF_VALUE;

using whatever your voltage reference value is.
21  Using Arduino / Programming Questions / Re: Extrapolationg code for multiple analogue inputs isnt working on: July 02, 2013, 04:53:23 pm
Update:
lloyddean: I tried your code and I am still getting the same problem as before. I get a response every once in a while. I hooked it all up and continuously hit the speaker and the LED strip lit up at random times. So the fundamentals of the code are right, however there seems to be some underlying problem with the extension of all 5 sensors. I think it has something to do with the delay function. I looked at DVDDoug's example of the "blink without delay" code but the problem I see with using that method is that I need it to blink only once after being prompted by the speaker voltage (as apposed to continuous blinking given in the example). This sporadic and random success from the tests I have done is encouraging but It still is not responding correctly to EVERY speaker voltage that I generate by hitting it.

Not at all unexpected as it IS YOUR code wrapped in a simple loop using arrays.

Now that that is established you have less code to debug and fix.
22  Using Arduino / Programming Questions / Re: Extrapolationg code for multiple analogue inputs isnt working on: July 02, 2013, 12:45:31 pm
We all have different ideas as HOW to assist you.  Mine is to provide simplified code in the hope you;ll be curious and explore what is presented.

One of my  points is smaller simplified code is easier to read and thus maintain.

Another is that using named symbols in place of hardcoded values makes code easier to read than bad or incorrect comments.

Code:
// Read about arrays at <http://www.Cplusplus.com/doc/tutorial/arrays/>

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

const uint8_t   pinLED_STRIP_1  = 1;
const uint8_t   pinLED_STRIP_2  = 2;
const uint8_t   pinLED_STRIP_3  = 3;
const uint8_t   pinLED_STRIP_4  = 4;
const uint8_t   pinLED_STRIP_5  = 5;

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

const int       TIME_LED_ON     = 10UL;

const uint8_t   pinsLEDS[]      = { pinLED_STRIP_1, pinLED_STRIP_2, pinLED_STRIP_3, pinLED_STRIP_4, pinLED_STRIP_5 };

void setup()
{
    for ( size_t i = 0; i < ARRAY_ENTRIES(pinsLEDS); i++ )
    {
        pinMode(pinsLEDS[i], OUTPUT);
    }
}

void loop()
{
    const uint8_t   pinsSPEAKERS[]      = { A0, A1, A2, A3, A4 };
    const int       SENSOR_TRIP_POINT   = 10;
   
    // loop over all "speakers" listed in the array 'pinsSPEAKERS' ...
    // ... setting state of corresponding "LED" in the array 'pinsLEDS' ...
    // ... based upon the "trip value" specified using the constant value 'SENSOR_TRIP_POINT' ...

    for ( size_t i = 0; i < ARRAY_ENTRIES(pinsSPEAKERS); i++ )
    {
        // sample speaker[i]

        int value = analogRead(pinsSPEAKERS[i]);
        if ( value > SENSOR_TRIP_POINT )
        {
            digitalWrite(pinsLEDS[i], LED_ON);
            delay(TIME_LED_ON);

            digitalWrite(pinsLEDS[i], LED_OFF);
        }
        else
        {
            digitalWrite(pinsLEDS[i], LED_OFF);
        }
    }
}
23  Using Arduino / Programming Questions / Re: Extrapolationg code for multiple analogue inputs isnt working on: July 01, 2013, 04:20:15 pm
Your current code can be made shorter and simpiler to read (aiding in debugging actually)

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

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

const uint8_t   LEDstrip1       = 1;        // led strip attatched to output pin 1
const uint8_t   LEDstrip2       = 2;        // led strip attatched to output pin 2
const uint8_t   LEDstrip3       = 3;        // led strip attatched to output pin 3
const uint8_t   LEDstrip4       = 4;        // led strip attatched to output pin 4
const uint8_t   LEDstrip5       = 5;        // led strip attatched to output pin 5

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

const uint8_t   pinsLEDS[]      = { LEDstrip1, LEDstrip2, LEDstrip3, LEDstrip4, LEDstrip5};
const uint8_t   pinsAnalog[]    = { A0, A1, A2, A3, A4 };

const int       dmsLED_ON       = 10UL;     // delta milli-seconds , 100th of a second

void setup()
{
    for ( size_t i = 0; i < ARRAY_ENTRIES(pinsLEDS); i++ )
    {
        pinMode(pinsLEDS[i], OUTPUT);
    }
}

void loop()
{
    const int   SENSOR_TRIP_POINT   = 10;
   
    for ( size_t i = 0; i < ARRAY_ENTRIES(pinsAnalog); i++ )
    {
        // read value from speaker

        int value = analogRead(pinsAnalog[i]);
        if ( value > SENSOR_TRIP_POINT )
        {
            // if the sensor has been struck and creates a voltage
           
            digitalWrite(pinsLEDS[i], LED_ON);
            delay(dmsLED_ON);

            digitalWrite(pinsLEDS[i], LED_OFF);
        }
        else
        {
            // if sensor not activiated, leave LEDstrip off

            digitalWrite(pinsLEDS[i], LED_OFF);
        }
    }
}
24  Using Arduino / Programming Questions / Re: Initalizing many boolean statements on: June 26, 2013, 10:47:43 pm
I'd recommend the Arduino type 'boolean' over 'bool' as a 'boolean' is a single byte and a 'bool' is 2 bytes.

I'd aslo recommend the usage of some type of symbol for pin specification over raw numbers that are hard to go back and change or even read.

Code:
const size_t    PIN_START               =  0;
const size_t    PIN_RANGE               = 54;
const size_t    PIN_TRAP                = PIN_START + 2;

boolean         statePins[pinRANGE] = { false };

void setup()
{
    for ( size_t i = PIN_START; i < PIN_RANGE; ++i )
    {
        pinMode(i, OUTPUT);
    }

    Serial.begin(9600);
}

void loop()
{
    int input = Serial.read();
    if ( input == PIN_TRAP )
    {
        statePins[PIN_TRAP] = !statePins[PIN_TRAP];
        digitalWrite(PIN_TRAP, statePins[PIN_TRAP] ? HIGH : LOW);
    }
}
25  Using Arduino / Programming Questions / Re: "return" function not working "void loop()" on: June 26, 2013, 12:40:20 am
Code:
#include <Arduino.h>

int main(void)
{
    init();

#if defined(USBCON)
    USBDevice.attach();
#endif
   
    setup();
   
    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }
       
    return 0;
}

26  Using Arduino / Programming Questions / Re: structs in libraries on: June 26, 2013, 12:22:48 am
When writing constructors remember to use inita;ization lists instead setting member variables in the body of the constructor.

Code:
srcModes_t::srcModes_t() : maxVal(1023), scale(1), srcSnk(0), cvCc(0)
{   }
27  Using Arduino / Programming Questions / Re: storing values on: June 25, 2013, 04:25:15 pm
Code:
int globalVariable      = 0x0D;

void loop()
{    }

void setup()
{
    int localVariable   = 0x0D;
}
28  Using Arduino / Programming Questions / Re: Arduino C++ programing mini computer on: June 24, 2013, 06:17:28 pm
<http://mx.mouser.com/new/beagleboardorg/beaglebone/?cm_mmc=google-_-ppc-_-americas-_-TexasInstruments&gclid=CIP_r7zp_bcCFS9dQgodlwcANA>
29  Using Arduino / Programming Questions / Re: First Program on: June 23, 2013, 10:15:04 pm
This code fragment does the very same thing -

Code:
minutes += ((seconds %= 60) ? 0 : 1);


What type is 'minute'?
30  Using Arduino / Programming Questions / Re: First Program on: June 23, 2013, 02:36:36 am
Must admit I'm not understanding what you're on about so this maybe wrong but ...

Code:
// ... somewhere in global space ...

uint8_t seconds = 0; // range 0 - 255, 60 seconds in a minute
uint8_t minutes = 0; // range 0 - 255, 60 minutes in an hour
uint8_t hours = 0; // range 0 - 255, 24 hours in a day
uint16_t days = 0; // range 0 - 65535, (roughly) 365.25 days in a year


// ... somewhere in function, perhaps even 'loop', space ...

seconds++;
minutes += ((seconds %= 60) ? 0 : 1);
hours   += ((minutes %= 60) ? 0 : 1);
days    += ((hours   %= 24) ? 0 : 1);

Pages: 1 [2] 3 4 ... 52