Show Posts
Pages: 1 2 3 [4] 5 6 ... 10
46  Forum 2005-2010 (read only) / Syntax & Programs / Re: 2-dimensional arrays? on: January 05, 2007, 02:57:24 pm
try this...
Code:
int foobar[8][8] = {
    {5, 7, 2, 3, 5, -4, 3, 8},      // this is foobar[0][...]
    {12, 8, 6, 2, -9, 5, 4, 3},    // this is foobar[1][...]
    //... etc ...
};

Then foobar[0][1] == 7, and foobar[1][0] == 12, just to give you an idea of the order of the dimensions.
47  Forum 2005-2010 (read only) / Syntax & Programs / Re: Compiled file sizes on: January 02, 2007, 05:06:34 pm
Actually, on second thought, don't try that patch on Arduino 0007.  I am certain it will not work because I am further into the project now and ran into some issues.  Some time this week I hope to have an equivalent patch ready for Arduino 0007.  When I do I will post it on this forum so people can find what I broke and start yelling at me!  smiley
48  Forum 2005-2010 (read only) / Syntax & Programs / Re: Compiled file sizes on: January 02, 2007, 02:10:02 pm
This is a problem that a couple of us (myself and nrolland) are working on for inclusion in the next release, Arduino 0008.  Currently a lot of code from the runtime library gets linked in, whether you call it or not.  The following patch was developed for Arduino 0006, and I have no idea whether it will work in Arduino 0007 or not (maybe you can try it and let us know... follow the instructions and you can uninstall it if it doesn't work).

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1164904468
49  Forum 2005-2010 (read only) / Syntax & Programs / Re: global variable for multiple objects? on: December 28, 2006, 11:16:22 am
A good way to share variables between instances of a class in C++ is to make them static.  For example:

Code:
class Button {
public:
    // blah blah blah...

private:
    int  fred;
    static int barney;
};

int Button::barney = 17;

Button recBtn = Button(7);
Button playBtn = Button(8);

In this contrived example, recBtn and playBtn each have their own, independent copy of "fred", but they share a single copy of "barney".  Note that you must declare each static variable inside the class, and define them outside the class.  Hope this helps.

- Don
50  Forum 2005-2010 (read only) / Syntax & Programs / Re: modify setup() from a class on: December 24, 2006, 11:08:37 am
I think newcomers to Arduino will find it quite helpful and revealing to study the startup code in:

    lib/targets/arduino/wiring.c

For example, if you look at main(), you see a bunch of hardware initialization stuff, and at the very end you see:

    setup();
    
    for (;smiley-wink
        loop();

Then it becomes very apparent that there is nothing special about what you can do in setup() vs loop().
51  Forum 2005-2010 (read only) / Syntax & Programs / Re: Question about memory allocations on: December 17, 2006, 02:33:29 pm
I forgot to mention another possibility, which is to store the strings in program memory instead of RAM.  Take a look at these functions...

http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html
52  Forum 2005-2010 (read only) / Syntax & Programs / Re: Question about memory allocations on: December 17, 2006, 12:42:19 pm
The data in this program could be made a lot smaller by using bit masks instead of strings.  For one thing, the spaces are not necessary, because they appear every 8 characters.  It would be possible to eliminate all of the spaces simply by keeping count of how many of the other characters have been loaded into the shift registers.

The only remaining characters are 'o', 'g', and 'r'.  Since there are only 3 possible characters, you need only 2 bits to encode each, not an entire 8-bit character.  Each group of 8 symbols could thus be represented in a 16-bit unsigned int.

This is the kind of situation where I have found it helpful to write a program that runs on my PC that reads in the human readable o/g/r symbols, converts them to the correct bit patterns, and generates the compressed data structure as a header file that can be included and used by the rendering program.  Then you just download the compressed program + data into your Arduino.

You will need to learn about bitwise operations in C++ such as shift-left (<<) shift-right (>>), bitwise AND (&), bitwise OR (|), etc, in order to package and unpackage groups of bits from an integer.
53  Forum 2005-2010 (read only) / Syntax & Programs / Re: Question about memory allocations on: December 14, 2006, 11:20:22 am
If you are willing to post your code, and/or a description of the problem you are solving, maybe we can get a better idea of how to optimize for memory usage.  A lot of times there are ways to use a different algorithm, or simply to represent the same information using fewer bytes of memory.
54  Forum 2005-2010 (read only) / Syntax & Programs / Re: Question about memory allocations on: December 12, 2006, 05:48:12 pm
If you are allocating memory using malloc(), operator new, etc, yes you do have to explicitly free it.  The Arduino environment is not just *like* C, it *is* C++.  I was confused on this point at first, because the programming language used in the Arduino IDE is described as a language called "Processing", though it turns out that Processing is really just C++ with a certain style of class library bolted on top.

On the other hand, I would be very careful with dynamic allocation of memory in such a constrained memory space.   Instead of using things like linked lists, I would tend to reorganize my code to use arrays, and to very carefully budget out the less than 1K of RAM you have available!
55  Forum 2005-2010 (read only) / Syntax & Programs / Re: simple soft-button issue. on: December 08, 2006, 03:37:43 pm
When I read your code literally, it looks like you are looping as fast as possible, polling digital input pin 12.  If that pin is LOW, you are rapidly toggling the output pin 11 HIGH/LOW.  In fact, it is probably so rapid that it appears to be on all the time.  Two things occur to me:
1. Instead of toggling every time you see pin 12 low, you probably want to toggle only when it goes from high to low.
2. Even that probably won't work right, because real mechanical switches have a tendency to produce noise for a millisecond or so while they are transitioning between open and closed, or closed and open.  So you may want to add a delay(1) on each loop, notice when you are transitioning from input pin 12 being high to low, and then start a countdown of maybe 50 loops (or 0.05 seconds).  Only when pin 12 has been low for 0.05 seconds, immediately after having just been high, should you toggle the output state.

Oh, and instead of doing this:

    if (softValue == 0) {
      softValue = 1;
    } else {
      softValue = 0;
    }

you can do this, which will do the same thing:

    softvalue = !softvalue;

Likewise, because HIGH==1 and LOW==0 (see lib\targets\arduino\wiring.h), you can replace:

  if (softValue == 1) {
    digitalWrite(flashLED, HIGH);
  } else {
    digitalWrite(flashLED, LOW);
  }

with:

   digitalWrite (flashLed, softValue);

I'm not sure if my earlier suggestion is phrased in an understandable way.  Feel free to post again with questions and I'll do my best to help!

- Don
56  Forum 2005-2010 (read only) / Syntax & Programs / Re: PID Control on: December 10, 2006, 05:30:28 pm
I assume you are talking about proportional-integral-derivative feedback controller.  This kind of thing could be done with Arduino, depending on the type of sensors and actuators you have available, and the kind of system you are trying to control.  For some applications, PID might be overkill; you might be able to get away with a simple thermostat model: if the vat of goo is too cold, turn on the heater... if it is too warm, turn it off... lather, rinse, repeat.

Why not post more details of the problem you are trying to solve.  That will help get the conversation going!   smiley
57  Forum 2005-2010 (read only) / Syntax & Programs / Re: avrlib on: December 08, 2006, 03:40:42 pm
Can you give an example of which files you mean?
58  Forum 2005-2010 (read only) / Syntax & Programs / Re: reading - writing multiple pins simultaneously on: November 30, 2006, 04:30:28 pm
This is really cool!  I've been playing around a little bit with using PORTB and PORTD, and apparently the compiler is set up to recognize this as an alias for a hardware port.  Here is a simple code example along with its disassembly (obtained using objdump.exe, part of the Arduino distribution):

    PORTD |= BINARY(0,0,0,0,0,1,0,1);
   4:      82 b3             in      r24, 0x12      ; 18
   6:      85 60             ori      r24, 0x05      ; 5
   8:      82 bb             out      0x12, r24      ; 18

This example will turn on output pins 0 and 2, but leave all the other pins at their current value.
The compiler-generated code reads the hardware port 0x12 into the register r24, then does a bitwise-OR with 5, i.e. the value that BINARY(0,0,0,0,0,1,0,1) expands to, then writes the result back to hardware port 0x12.  If you wanted to toggle the existing values, you could do:

   PORTD ^= BINARY( /*whatever*/ );    // use XOR to toggle bits

And to answer macsimski's question, you would do:

    someVariable = PORTB;
59  Forum 2005-2010 (read only) / Syntax & Programs / Re: reading - writing multiple pins simultaneously on: November 30, 2006, 01:17:00 pm
You could define a C++ macro to convert from binary to hex for you:

#define BINARY(a,b,c,d,e,f,g,h)  (a<<7|b<<6|c<<5|d<<4|e<<3|f<<2|g<<1|h)

Then you could use a line of code like this:

PORTD = BINARY(1,1,1,1,1,1,1,1);

The C++ compiler will expand the macro, then notice that all the values are constant, and pre-calculate the correct value (known as "constant folding").
60  Forum 2005-2010 (read only) / Syntax & Programs / Re: Error with mathematical calculation on: November 30, 2006, 01:27:21 pm
Does Arduino support the functions exp (raise e to a power) and log (natural log)?  If so, you can raise A to the B power using the following formula:

float pow (float A, float B)
{
    return exp (B * log(A));
}
Pages: 1 2 3 [4] 5 6 ... 10