Loading...
  Show Posts
Pages: 1 2 [3] 4 5 ... 11
31  Forum 2005-2010 (read only) / Syntax & Programs / Re: OneWire device address to String on: May 28, 2010, 12:55:02 am
Just off the top of me head....
Code:
char *addr2str(DeviceAddress deviceAddress)
{
     static char return_me[18];
     static char *hex = "0123456789ABCDEF";
     uint8_t i, j;

     for (i=0, j=0; i<8; i++)
     {
          return_me[j++] = hex[deviceAddress[i] / 16];
          return_me[j++] = hex[deviceAddress[i] & 15];
     }
     return_me[j] = '\0';

     return (return_me);
}
The programmer does need to make a copy of the string
returned before calling this function again.

It compiles so I'll leave it up to the reader to make it run correctly!  :-)
32  Forum 2005-2010 (read only) / Syntax & Programs / Re: h:16: error: expected ',' or '...' before numeric on: April 28, 2010, 01:33:26 am
It's probably best not to go defining single letter #define's
because in WProgram.h, at line 16, there is this:
Code:
uint16_t makeWord(byte h, byte l);

Your
Code:
#define h 29
...
#define l 31
causes that line to be interpreted as this:
Code:
uint16_t makeWord(byte 29, byte 31);
which isn't legal C/C++

Change those #define a  22 to something more meaningful like
#define pinA 22
etc...

BTW, the Arduino IDE puts a "#include <WProgram.h>" into your
program for you.  Apparently it puts it in a less-than-wonderful place
for your program.
33  Forum 2005-2010 (read only) / Syntax & Programs / Re: Inefficient use of If / Else If? on: April 17, 2010, 01:22:06 pm
If your boundaries were more like 0-99, 100-199, 200-299, etc... then you
could do something like this:
Code:
smoothPotValue =  25 * (10 - (analogRead(potPin)/100) );
but this would give you a zero value for actualPotValue >= 1000;
If that bothers you then you could follow that line with this:
Code:
if (smoothPotValue == 0)
    smoothPotValue = 25;
34  Forum 2005-2010 (read only) / Syntax & Programs / Re: Exponent of E on: April 14, 2010, 01:47:15 am
Wow!  I don't what I ate but I was having some serious brain flatulence when I wrote the above post.

Variables of type float allow for numbers as small as 1.0*10^-45 (but they are "subnormal").
Still, ignoring the bad "magnitude" of these numbers the lessons are still correct.

Sorry 'bout that.  smiley-sad
35  Forum 2005-2010 (read only) / Syntax & Programs / Re: Exponent of E on: April 13, 2010, 03:57:27 pm
I've come in a little late but...
@PaulS -- Sorry sir, but you're giving the wrong impression here.
The value 2e-16 is NOT zero on the Arduino.  Try this
Code:
{
   float x;
   x = 2.0e-16;
   Serial.println(x, 18);
}
Mine prints  0.000000000000000200

Unfortunately, the Arduino print functions do not properly handle small
numbers and allow them to be printed in scientific notation.Variables of
type float allow for numbers as small as 1.0*10^-149, or 1.0e-149.

Keep in mind that if i add 1.0 to 1.0e-149 (10**-149), the resulting
number will be 1.0 simply because the type float only permits about
6 significant digits. That 1.0e-149 portion gets chopped off because
retaining it is beyond the precision of float.  As a matter of fact,

     x = 1.0e-140 + 1.0e-149;

will get you 1.0e-140 but

     x = 1.0e-145 + 1.0e-149;

will have a value of x being 1.0001e-145 .

Maybe this will help people dealing with small numbers.
The curious may wish to read the 'IEEE_floating-point_standard'
and 'Significant_digits' pages on Wikipedia.org.
36  Forum 2005-2010 (read only) / Syntax & Programs / Re: pwm output o'reilly book on: April 11, 2010, 03:31:33 pm
Unless, of course, 'i' has been declared 'unsigned char' in which case the loop will never stop.  But  the LED will turn off completely.  :-D
37  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help with a countdown program for 7 segment LEDs on: August 20, 2009, 02:36:54 am
I hope this won't confuse you.  I used the MsTimer2 as it lets a function be
called at whatever time interval you want-- and you don't have to fiddle
with timer registers and the like.  It makes things so much easier.

Oh yeah, something I forgot about.  Guess what happens when unsigned
char's are decremented past zero?   They don't go negative because
they are unsigned, right?  They go to 255 (the maximum value that can
be held by a single byte or unsigned char.   Thusly, for a down counter
I changed the declarations of seconds and minutes to simply 'char',
which is signed.   I did that so you can test for when the numbers go
negative.  Do you follow that?

Well, here's the code.  There are ways to do things a little better but
I thought I was changing your code quite a bit so we'll go with this for
now.   Feel free to ask me questions about this.  I hope you'll understand
how it works.
Code:
#include "LedControl.h"        // Include the LedControl library
#include "MsTimer2.h"

LedControl lc=LedControl(12,11,10,1); // Set up an LED object on pins 12,11,10

volatile unsigned char tick;
char seconds = 0;
char minutes = 60;     // Inititialise actual values for m,s
// int mins_increase = 0;


void display_time () {                     // Function to display the time
    lc.setDigit(0,0,seconds % 10,false);   // Light up 1's seconds
    lc.setDigit(0,1,seconds / 10,false);   // Light up 10's seconds
    lc.setDigit(0,2,minutes % 10,false);   // Light up 1's minutes
    lc.setDigit(0,3,minutes / 10,false);   // Light up 10's minutes
}

void decrement_time() {
    seconds--;                      // We're counting down
    if (seconds < 0) {                  // If seconds have rolled over
        seconds = 59;                  //    Reset seconds
        minutes--;                  //    and decrement the minutes
        if (minutes < 0) {            // If minutes have rolled over
            minutes = 60;            //    reset time to starting value
          seconds = 0;
        }
    }
    tick++;                             // indicate that the time has been updated
}

void setup () {
    lc.shutdown(0,false);            // Turn on the LEDs
    lc.setIntensity(0,15);            // Set intensity to full
    display_time();                  // Show initial time

    // You might want to add code here that will wait for a
    // button to be pushed before starting your 60-minute timer.
    // You might also want to modify this program to stop when
    // time reaches 00:00 and have a beeper go off.

    MsTimer2::set(1000, decrement_time);// every second decrement_time() is called
    MsTimer2::start();
}


void loop () {
    if (tick) {         // If a tick has occurred
      tick = 0;         //    reset indicator that we have a new time
      display_time();  //    and show it off!
    }
}
38  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help with a countdown program for 7 segment LEDs on: August 18, 2009, 12:52:18 pm
Oh, another thing:
The variables seconds_ones and tens as well as minutes_ones and tens
don't have to global variables, they can/should be local to your display_time()
function.
39  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help with a countdown program for 7 segment LEDs on: August 18, 2009, 12:47:38 pm
To get back to some old issues here.
1) Why is display_time() back in your interrupt handler?
2) You've gone back decrementing your seconds and minutes
     but still testing for them being greater than zero.
3) What's the difference between:
Code:
   seconds_ones = seconds % 10;           // Get the 1's digit of seconds
    if (seconds>=10) {                // If seconds greater than 10
        seconds_tens = seconds / 10;         // Get 10's digit of seconds
    }
    else {
        seconds_tens = 0;
    }
and
Code:
   seconds_ones = seconds % 10;
    seconds_tens = seconds / 10;
   True, the former is faster (because no division is evaluated) but it's
    only faster 1/6 of the time!  And this is not an application that needs
    millisecond optimizations.
4) Have you looked at the MsTimer2 library?  Have it call a routine every
    1,000-milliseconds that will decrement your time variables and then
    display the time on your LEDs?  Using this library you don't have to
    worry about twiddling any registers as the library does that for you.
40  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help with a countdown program for 7 segment LEDs on: August 15, 2009, 11:56:12 pm
   >where should the missing 'if' statement go?

Think about it.  If you decrement 'minutes' do you always want the
minutes to reset to zero?  That's what your code is doing.

Or maybe you need to determine when minutes goes negative...  If
'minutes' goes negative, do you reset to 59 or, maybe, do you stop
the clock?  You have to decide how you want your countdown clock
to act.

   >And im also getting -- In function 'void loop()':
   >error: a function-definition is not allowed here before '{' toke

From what you posted in a Code section, I'd say your curly braces
still don't match.  For every { you *MUST* have a matching }.

In Arduino, you *MUST* have a loop() function (which you have) and
you *MUST* have a setup() function.  You appear to be missing the latter.
In setup() you might want to initilize your display to 60:00.  Your choice!

And don't forget to initialize your 'tick' variable.
41  Forum 2005-2010 (read only) / Syntax & Programs / Re: Help with a countdown program for 7 segment LEDs on: August 15, 2009, 02:00:04 pm
1) You have too many curly braces at the end.
2) If you're counting DOWN, why are you testing for seconds
    to be greater than 59?  You should be looking to see if the
    value of seconds is < 0.  When that occurs, reset the value
      of seconds to 59.
3) At the declaration, set variable 'tick' to 0, just to be sure it's
    properly initialized.
4) Remove "display_time()" from your ISR -- you don't need
    that functionality in your ISR.  Put it in the loop() function after
      all the seconds and minutes have been accounted for.  In an ISR
    you want to get in and get out as quickly as possible.  Setting the
    'tick' variable is all you really need to do there.
5) In 'loop()' you keep reseting the minutes to 0.  I suspect you've
    forgotten an 'if' statement.
42  Forum 2005-2010 (read only) / Syntax & Programs / Re: removing delay() in DallasTemperature.cpp on: July 26, 2009, 03:24:28 pm
You could save the current millis() value and add 1000 (although 750
will be fine) to it after  the StartConversion has been commanded.
Afterwards, the Arduino can continue doing what it needs to do,
periodically checking the value of millis() to see if the delay has
expired, and read the temperature then.   The temperature value
will be retained by the 18x20 until the Arduino has a chance to read it.
43  Forum 2005-2010 (read only) / Syntax & Programs / Re: Structures, pointers to structures, and bytes on: July 18, 2009, 12:33:10 am
I'm a bit surprised if that last statement compiled since it is
syntactically incorrect.  Since structure1 is NOT a pointer to
the structure then the reference to the data1 member should
be:
Code:
structure1.data1 = 100;

The most recent version of avr-gcc on Linux did not like your statement.
44  Forum 2005-2010 (read only) / Syntax & Programs / Re: Two dimensional array question on: July 10, 2009, 01:40:09 am
When you use arrays by name, like in your definition of color_set[],
the name is essentially a pointer.  This is why you are getting the error
as you state.

Easiest fix is to simply declare color_set as:
Code:
int *color_set[] = {color1, color2, color3, color4, color5};

*OR*

remove your individual color definitions and define color_set as
Code:
  int color_set[][3] = {
        {140,40,0}, // Orange
        {200,20,0}, // Red
        {100,255,100}, // Green
        {10,120, 30}, // Greener
        {6,1,34} // Dark
   };

// either way will allow you to use color_set as below:

    printf("color1={%d,%d,%d}\n",
           color_set[0][0], color_set[0][1], color_set[0][2]);

   -Rusty-
45  Forum 2005-2010 (read only) / Syntax & Programs / Re: String array lookiup using strcmp on: June 03, 2009, 01:30:40 pm
Originally, I was going to use
Code:
sizeof(tags)/sizeof(char *)
but thought that might blow away a noob or two. :-)
Actually, that sorta thing might be better if the array elements were a
user-defined type, as defined with #typedef.

Why I used [1] instead of
  • ?   Got me.  Musta been those mental blocks
my folks gave me for Christmas one year. :-)
Pages: 1 2 [3] 4 5 ... 11